Witam, napisałem aplikacje z przełączaniem się między activity. Aplikacja się kompiluje ale przy naciśnięciu przycisku wywala błąd:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mathtool, PID: 22050
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mathtool/com.example.mathtool.PrzekatnaProstokata}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
KOD podemnu
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;
import android.widget.Toast;
public class PrzekatnaProstokata extends AppCompatActivity {
private Button buttonpowrotprostokatprzekatna;
private Button Result_Rectangle_Diagonal;
private EditText bok_a_rectangle;
private EditText bok_b_rectangle;
private TextView result_rectangle_diagonal;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_przekatna_prostokata);
bok_a_rectangle = (EditText) findViewById(R.id.bokaprzekatnaprostokata);
bok_b_rectangle = (EditText) findViewById(R.id.bokbprzekatnaprostokata2);
getSupportActionBar().setTitle("Przekątna prostokąta");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
buttonpowrotprostokatprzekatna = (Button) findViewById(R.id.buttonpowrotprostokatprzekatna);
buttonpowrotprostokatprzekatna.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openMenuProstokat();
}
});
Result_Rectangle_Diagonal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if((bok_a_rectangle.getText().length()>0) && (bok_b_rectangle.getText().length()>0))
{
double oper1 = Double.parseDouble(bok_a_rectangle.getText().toString());
double oper2 = Double.parseDouble(bok_b_rectangle.getText().toString());
double result = Math.sqrt(Math.pow(oper1,2) + Math.pow(oper2,2));
result_rectangle_diagonal.setText(Double.toString(result));
}
else{
Toast toast= Toast.makeText(PrzekatnaProstokata.this,"Wprowadź poprawne dane !!!",Toast.LENGTH_LONG);
toast.show();
}
}
});
}
public void openMenuProstokat() {
Intent intent = new Intent(this, ProstokatMenu.class);
startActivity(intent);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/istockphoto_185005145_612x612"
tools:context=".PrzekatnaProstokata">
<TextView
android:id="@+id/textView5"
android:layout_width="409dp"
android:layout_height="50dp"
android:layout_marginStart="4dp"
android:background="#A8175B"
android:text=" Przekątna prostokąta"
android:textSize="35dp"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteY="5dp" />
<ImageView
android:id="@+id/imageView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView5"
app:srcCompat="@drawable/prostokat" />
<Button
android:id="@+id/buttonpowrotprostokatprzekatna"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="688dp"
android:text="Powrót"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.178"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:background="#459348"
android:text="d = "
android:textSize="30dp"
app:layout_constraintTop_toBottomOf="@+id/imageView10"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="56dp" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="52dp"
android:background="#671F6C"
android:text="4 * a "
android:textSize="30dp"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/imageView10"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="#3F51B5"
android:text="Bok a: "
android:textSize="25dp"
app:layout_constraintTop_toBottomOf="@+id/textView4"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="154dp" />
<TextView
android:id="@+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="#3F51B5"
android:text="Bok b: "
android:textSize="25dp"
app:layout_constraintTop_toBottomOf="@+id/bokaprzekatnaprostokata"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="154dp" />
<EditText
android:id="@+id/bokaprzekatnaprostokata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:ems="10"
android:gravity="center_horizontal"
android:inputType="number"
android:textSize="18sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView14"
tools:ignore="MissingConstraints" />
<EditText
android:id="@+id/bokbprzekatnaprostokata2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginEnd="4dp"
android:ems="10"
android:gravity="center_horizontal"
android:inputType="number"
android:textSize="18sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView15"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/button_result_diagonal_rectangle"
android:layout_width="110dp"
android:layout_height="43dp"
android:layout_marginTop="24dp"
android:background="@color/red"
android:text="Calc"
app:layout_constraintTop_toBottomOf="@+id/textView1"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="225dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="332dp"
android:layout_height="50dp"
android:layout_marginTop="40dp"
android:fontFamily="serif"
android:gravity="center_horizontal"
android:text="result"
android:textColorLink="?android:attr/editTextColor"
android:textSize="27sp"
android:textStyle="bold|italic"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.493"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bokbprzekatnaprostokata2"
tools:textStyle="bold|italic" />
</androidx.constraintlayout.widget.ConstraintLayout>
Kod menu:
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ProstokatMenu extends AppCompatActivity {
private Button buttonpowrotprostokat1;
private Button buttonpolerectangle;
private Button buttonprzekatnarectangle;
private Button buttonobwodrectangle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prostokat_menu);
getSupportActionBar().setTitle("Pole prostokąta");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
buttonpolerectangle = (Button) findViewById(R.id.buttonpolerectangle);
buttonpolerectangle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openPoleProstokat();
}
});
buttonprzekatnarectangle = (Button) findViewById(R.id.buttonprzekatnarectangle);
buttonprzekatnarectangle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openPrzekatnaProstokat();
}
});
buttonobwodrectangle = (Button) findViewById(R.id.buttonobwodrectangle);
buttonobwodrectangle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openObwodProstokat();
}
});
buttonpowrotprostokat1 = (Button) findViewById(R.id.buttonpowrotprostokat1);
buttonpowrotprostokat1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openObwodProstokat();
}
});
}
public void openPoleProstokat() {
Intent intent = new Intent(this, PoleProstokat.class);
startActivity(intent);
}
public void openPrzekatnaProstokat() {
Intent intent = new Intent(this, PrzekatnaProstokata.class);
startActivity(intent);
}
public void openObwodProstokat() {
Intent intent = new Intent(this, ObwodProstokata.class);
startActivity(intent);
}
public void openBackMenuProstokat() {
Intent intent = new Intent(this, ProstokatMenu.class);
startActivity(intent);
}
}
Proszę o pomoc co jest źle ?