[英]App crash with FATAL EXCEPTION with NullPointerException when I click go this activity
I created an android studio project but I found null pointer exception error and all of these error again and again when I click a button that links activity while it didn't happen to other activity who have similar code.我创建了一个 android 工作室项目,但我发现 null 指针异常错误,当我单击链接活动的按钮时,所有这些错误一次又一次地出现,而其他具有类似代码的活动却没有发生。 even I try to change something, it always shows the same error in logcat.
即使我尝试更改某些内容,它也总是在 logcat 中显示相同的错误。 I quite beginner in android development
我是 android 开发的新手
Here is my code这是我的代码
package com.example.chemicalculators;
import static java.lang.Integer.*;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.concurrent.atomic.AtomicReference;
public class CalCal5 extends AppCompatActivity {
int eNo;
String eSym;
String eName;
double eMass;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AtomicReference<EditText> eNoString = new AtomicReference<>((EditText) findViewById(R.id.editTextNumber));
ImageButton searchButton = (ImageButton) findViewById(R.id.imageButton);
Button button = (Button) findViewById(R.id.button);
final TextView outESymbol = (TextView) findViewById(R.id.elementSymbol);
final TextView outEName = (TextView) findViewById(R.id.elementName);
final TextView outEMass = (TextView) findViewById(R.id.AtomicMassValue);
searchButton.setOnClickListener(view -> {
eNo = parseInt(eNoString.get().getText().toString());
switch(eNo)
{
case 1:
eSym = "H";
eName = "Hydrogen";
eMass = 1.00797;
case 2:
eSym = "He";
eName = "Helium";
eMass = 4.0026;
case 3:
eSym = "Li";
eName = "Lithium";
eMass = 6.941;
case 4:
eSym = "Be";
eName = "Beryllium";
eMass = 9.0122;
case 5:
eSym = "B";
eName = "Boron";
eMass = 10.811;
case 6:
eSym = "C";
eName = "Carbon";
eMass = 12.0107;
default :
eSym = "-";
eName = "None";
eMass = 0.00;
}
outESymbol.setText(eSym);
outEName.setText(eName);
outEMass.setText(String.valueOf(eMass));
Toast.makeText(CalCal5.this, "Finding Success", Toast.LENGTH_SHORT).show();
});
button.setOnClickListener(view -> {
Intent p = new Intent(CalCal5.this, MainActivity.class);
startActivity(p);
});
}
}
And this is my Logcat这是我的 Logcat
2022-02-14 09:50:22.931 6477-6511/com.example.chemicalculators E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2022-02-14 09:50:22.931 6477-6511/com.example.chemicalculators E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2022-02-14 09:50:22.931 6477-6511/com.example.chemicalculators E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2022-02-14 09:50:22.931 6477-6511/com.example.chemicalculators E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2022-02-14 09:50:25.414 6477-6477/com.example.chemicalculators E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.chemicalculators, PID: 6477
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chemicalculators/com.example.chemicalculators.CalCal5}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.chemicalculators.CalCal5.onCreate(CalCal5.java:33)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
You are not calling setContentView()
to inflate layout您没有调用
setContentView()
来扩充布局
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YOUR_LAYOUT)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.