Nullpointexception студия Android на Java
Below are my MainActivity and Layout file for Android calculator app. Everytime I run the app , it crashes and inside the LOGCAT of android studio, the exception occurs. LOGCAT MESSAGE BELOW : Caused by: java.lang.NullPointerException: Attempt to invoke virtual method android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference MainActivity Code Below : package com.hfad.calculatorx; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setAllListners(); } //Referencing all components TextView Display =(TextView) findViewById(R.id.Display); Button button0 =(Button) findViewById(R.id.button0); Button button1 =(Button)findViewById(R.id.button1); Button button2 =(Button)findViewById(R.id.button2); Button button3 =(Button)findViewById(R.id.button3); Button button4 =(Button)findViewById(R.id.button4); Button button5 =(Button)findViewById(R.id.button5); Button button6 =(Button)findViewById(R.id.button6); Button button7 =(Button)findViewById(R.id.button7); Button button8 =(Button)findViewById(R.id.button8); Button button9 =(Button)findViewById(R.id.button9); Button buttonClear =(Button)findViewById(R.id.buttonclear); Button buttonResult =(Button)findViewById(R.id.buttonresult); //Making OnclickListners for all Buttons View.OnClickListener Button0Listner = new View.OnClickListener() { @Override public void onClick(View view) { Display.append(Display.getText()+"0"); } }; View.OnClickListener Button1Listner = new View.OnClickListener() { @Override public void onClick(View view) { Display.append(Display.getText()+"1"); } }; View.OnClickListener Button2Listner = new View.OnClickListener() { @Override public void onClick(View view) { Display.append(Display.getText()+"2"); } }; View.OnClickListener Button3Listner = new View.OnClickListener() { @Override public void onClick(View view) { Display.append(Display.getText()+"3"); } }; View.OnClickListener Button4Listner = new View.OnClickListener() { @Override public void onClick(View view) { Display.append(Display.getText()+"4"); } }; View.OnClickListener Button5Listner = new View.OnClickListener() { @Override public void onClick(View view) { Display.append(Display.getText()+"5"); } }; View.OnClickListener Button6Listner = new View.OnClickListener() { @Override public void onClick(View view) { Display.append(Display.getText()+"6"); } }; View.OnClickListener Button7Listner = new View.OnClickListener() { @Override public void onClick(View view) { Display.append(Display.getText()+"7"); } }; View.OnClickListener Button8Listner = new View.OnClickListener() { @Override public void onClick(View view) { Display.append(Display.getText()+"8"); } }; View.OnClickListener Button9Listner = new View.OnClickListener() { @Override public void onClick(View view) { Display.append(Display.getText()+"9"); } }; View.OnClickListener ButtonClearListner = new View.OnClickListener() { @Override public void onClick(View view) { Display.setText(""); } }; View.OnClickListener ButtonResultListner = new View.OnClickListener() { @Override public void onClick(View view) { //Part left empty intentionally } }; //Setting all listners at once public void setAllListners(){ button0.setOnClickListener(Button0Listner); button1.setOnClickListener(Button1Listner); button2.setOnClickListener(Button2Listner); button3.setOnClickListener(Button3Listner); button4.setOnClickListener(Button4Listner); button5.setOnClickListener(Button5Listner); button6.setOnClickListener(Button6Listner); button7.setOnClickListener(Button7Listner); button8.setOnClickListener(Button8Listner); button9.setOnClickListener(Button9Listner); buttonClear.setOnClickListener(ButtonClearListner); buttonResult.setOnClickListener(ButtonResultListner); } } Layout File Below : <?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" tools:context=".MainActivity"> <TextView android:id="@+id/Display" android:layout_width="395dp" android:layout_height="80dp" android:layout_marginTop="60dp" android:background="@color/display_background" android:gravity="center_horizontal|bottom" android:textColor="@color/black" android:textSize="25dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.529" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:textSize="25sp" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="36dp" android:layout_marginLeft="36dp" android:layout_marginBottom="48dp" android:text="1" app:layout_constraintBottom_toTopOf="@+id/button3" app:layout_constraintStart_toStartOf="parent" /> <Button android:textSize="25sp" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="44dp" android:text="2" app:layout_constraintBottom_toTopOf="@+id/button4" app:layout_constraintEnd_toStartOf="@+id/buttonadd" app:layout_constraintStart_toEndOf="@+id/button1" /> <Button android:id="@+id/buttonadd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="36dp" android:layout_marginRight="36dp" android:layout_marginBottom="52dp" android:text="+" android:textSize="25sp" app:layout_constraintBottom_toTopOf="@+id/buttonsubtract" app:layout_constraintEnd_toEndOf="parent" /> <Button android:textSize="25sp" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="36dp" android:layout_marginLeft="36dp" android:layout_marginBottom="36dp" android:text="3" app:layout_constraintBottom_toTopOf="@+id/button5" app:layout_constraintStart_toStartOf="parent" /> <Button android:textSize="25sp" android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="40dp" android:text="4" app:layout_constraintBottom_toTopOf="@+id/button6" app:layout_constraintEnd_toStartOf="@+id/buttonsubtract" app:layout_constraintStart_toEndOf="@+id/button3" /> <Button android:textSize="25sp" android:id="@+id/buttonsubtract" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="36dp" android:layout_marginRight="36dp" android:layout_marginBottom="40dp" android:text="-" app:layout_constraintBottom_toTopOf="@+id/buttondivide" app:layout_constraintEnd_toEndOf="parent" /> <Button android:textSize="25sp" android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="36dp" android:layout_marginLeft="36dp" android:layout_marginBottom="40dp" android:text="5" app:layout_constraintBottom_toTopOf="@+id/button8" app:layout_constraintStart_toStartOf="parent" /> <Button android:textSize="25sp" android:id="@+id/button6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="40dp" android:text="6" app:layout_constraintBottom_toTopOf="@+id/button8" app:layout_constraintEnd_toStartOf="@+id/buttondivide" app:layout_constraintStart_toEndOf="@+id/button5" /> <Button android:textSize="25sp" android:id="@+id/buttondivide" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="36dp" android:layout_marginRight="36dp" android:layout_marginBottom="40dp" android:text="/" app:layout_constraintBottom_toTopOf="@+id/buttonmultiply" app:layout_constraintEnd_toEndOf="parent" /> <Button android:textSize="25sp" android:id="@+id/button7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="36dp" android:layout_marginLeft="36dp" android:layout_marginTop="40dp" android:text="7" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button5" /> <Button android:textSize="25sp" android:id="@+id/button8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="40dp" android:text="8" app:layout_constraintBottom_toTopOf="@+id/button0" app:layout_constraintEnd_toStartOf="@+id/buttonmultiply" app:layout_constraintStart_toEndOf="@+id/button7" /> <Button android:textSize="25sp" android:id="@+id/buttonmultiply" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="36dp" android:layout_marginRight="36dp" android:layout_marginBottom="36dp" android:text="*" app:layout_constraintBottom_toTopOf="@+id/buttonclear" app:layout_constraintEnd_toEndOf="parent" /> <Button android:textSize="25sp" android:id="@+id/button9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="36dp" android:layout_marginLeft="36dp" android:layout_marginTop="40dp" android:text="9" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button7" app:layout_constraintVertical_bias="0.007" /> <Button android:textSize="25sp" android:id="@+id/button0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="32dp" android:text="0" app:layout_constraintBottom_toTopOf="@+id/buttonresult" app:layout_constraintEnd_toStartOf="@+id/buttonclear" app:layout_constraintHorizontal_bias="0.506" app:layout_constraintStart_toEndOf="@+id/button9" /> <Button android:textSize="22sp" android:id="@+id/buttonclear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="36dp" android:layout_marginRight="36dp" android:layout_marginBottom="128dp" android:text="Clear" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" /> <Button android:textSize="22sp" android:id="@+id/buttonresult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="48dp" android:text="Result" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.501" app:layout_constraintStart_toStartOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
Что я уже пробовал:
Я пробовал играть с этими методами, но ничего не вышло
Richard MacCutchan
Вы можете избавиться от девяти из этих прослушивателей кнопок и использовать один общий для всех кнопок. Это значительно упростило бы ваш код.
David Crow
Прокомментируйте вызов к setAllListners()
Исчезает ли исключение?