启动后我添加了 addListenerOnButtonClick();在 mainactivity.java 然后应用程序崩溃
after splash i added addListenerOnButtonClick(); in mainactivity.java then app crashes
mainactivity.java
包 com.example.newsplash;
进口androidx.appcompat.app.AppCompatActivity;
进口android.os.Bundle;导入 android.view.View;导入 android.widget.Button;导入 android.widget.CheckBox;导入 android.widget.Toast;
public class MainActivity extends AppCompatActivity {
CheckBox Chapathi, Dosai, Biriyani, Coffee;
Button order;`enter code here`
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButtonClick();
}
public void addListenerOnButtonClick() {
Chapathi = (CheckBox) findViewById(R.id.Chapathi);
Biriyani = (CheckBox) findViewById(R.id.Biriyani);
Dosai = (CheckBox) findViewById(R.id.Dosai);
Coffee = (CheckBox) findViewById(R.id.Coffee);
order.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick (View view)
{
int totalamount = 0;
StringBuilder result = new StringBuilder();
result.append("Selected Items:");
if (Chapathi.isChecked()) {
result.append("\nChapathi 10RS");
totalamount += 10;
}
if (Biriyani.isChecked()) {
result.append("\nBiriyani is 100RS");
totalamount += 100;
}
if (Dosai.isChecked()) {
result.append("\nDosai is 10RS");
totalamount += 10;
}
if (Coffee.isChecked()) {
result.append("\n Coffee is 10RS");
totalamount += 10;
}
result.append(("\nTotal"+totalamount + "RS"));
Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();
}
});
}
}
logcat 2020-11-05 00:33:48.523 27490-27966/com.example.newsplash D/mmscene: getHint applicationScene= com.example.newsplash,idx=0 none 2020-11-05 00:33:48.577 27490-27490/com.example.newsplash I/Choreographer: 跳过7帧!应用程序可能在其主线程上做了太多工作。 2020-11-05 00:33:51.378 27490-27490/com.example.newsplash W/ActivityThread:handleWindowVisibility:没有 activity 令牌 android.os.BinderProxy@3268ab0 2020-11-05 00:33:51.928 27490-27490/com.example.newsplash D/AndroidRuntime: 关闭 VM 2020-11-05 00:33:51.938 27490-27490/com.example.newsplash E/AndroidRuntime :致命例外:主要
进程:com.example.newsplash,PID:27490
java.lang.RuntimeException: 无法启动 activity ComponentInfo{com.example.newsplash/com.example.newsplash.MainActivity}: java.lang.NullPointerException: 尝试调用虚拟方法 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' 在空对象引用上
在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3146)
在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3296)
在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:114)
在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:74)
在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
在 android.os.Handler.dispatchMessage(Handler.java:106)
在 android.os.Looper.loop(Looper.java:226)
在 android.app.ActivityThread.main(ActivityThread.java:7224)
在 java.lang.reflect.Method.invoke(本机方法)
在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:500)
在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:913)
原因:java.lang.NullPointerException:尝试在空对象引用上调用虚方法 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
在 com.example.newsplash.MainActivity.addListenerOnButtonClick(MainActivity.java:36)
在 com.example.newsplash.MainActivity.onCreate(MainActivity.java:23)
在 android.app.Activity.performCreate(Activity.java:7337)
在 android.app.Activity.performCreate(Activity.java:7328)
在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3126)
在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3296)
在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:114)
在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:74)
在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994)
在 android.os.Handler.dispatchMessage(Handler.java:106)
在 android.os.Looper.loop(Looper.java:226)
在 android.app.ActivityThread.main(ActivityThread.java:7224)
在 java.lang.reflect.Method.invoke(本机方法)
- 您的代码
您创建了一个变量 Button order
但您从未对其进行初始化。
然后,在未初始化的按钮上,您调用 .setOnClickListener
,这显然无法完成。
您的堆栈跟踪:
java.lang.NullPointerException: 尝试在空对象引用
上调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
解决方案是对 order
使用 findViewById
并获得实际的按钮参考,然后您可以使用 .setOnClickListener
**是的,现在有效了。谢谢你 **
public class MainActivity 扩展 AppCompatActivity {
CheckBox Chapathi, Dosai, Biriyani, Coffee;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButtonClick();
}
public void addListenerOnButtonClick() {
Chapathi = (CheckBox) findViewById(R.id.Chapathi);
Biriyani = (CheckBox) findViewById(R.id.Biriyani);
Dosai = (CheckBox) findViewById(R.id.Dosai);
Coffee = (CheckBox) findViewById(R.id.Coffee);
button = (Button) findViewById(R.id.order);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick (View view)
{
int totalamount = 0;
StringBuilder result = new StringBuilder();
result.append("Selected Items:");
if (Chapathi.isChecked()) {
result.append("\nChapathi 10RS");
totalamount += 10;
}
if (Biriyani.isChecked()) {
result.append("\nBiriyani is 100RS");
totalamount += 100;
}
if (Dosai.isChecked()) {
result.append("\nDosai is 10RS");
totalamount += 10;
}
if (Coffee.isChecked()) {
result.append("\n Coffee is 10RS");
totalamount += 10;
}
result.append(("\nTotal"+totalamount + "RS"));
Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();
}
});
}
}
mainactivity.java
包 com.example.newsplash;
进口androidx.appcompat.app.AppCompatActivity;
进口android.os.Bundle;导入 android.view.View;导入 android.widget.Button;导入 android.widget.CheckBox;导入 android.widget.Toast;
public class MainActivity extends AppCompatActivity {
CheckBox Chapathi, Dosai, Biriyani, Coffee;
Button order;`enter code here`
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButtonClick();
}
public void addListenerOnButtonClick() {
Chapathi = (CheckBox) findViewById(R.id.Chapathi);
Biriyani = (CheckBox) findViewById(R.id.Biriyani);
Dosai = (CheckBox) findViewById(R.id.Dosai);
Coffee = (CheckBox) findViewById(R.id.Coffee);
order.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick (View view)
{
int totalamount = 0;
StringBuilder result = new StringBuilder();
result.append("Selected Items:");
if (Chapathi.isChecked()) {
result.append("\nChapathi 10RS");
totalamount += 10;
}
if (Biriyani.isChecked()) {
result.append("\nBiriyani is 100RS");
totalamount += 100;
}
if (Dosai.isChecked()) {
result.append("\nDosai is 10RS");
totalamount += 10;
}
if (Coffee.isChecked()) {
result.append("\n Coffee is 10RS");
totalamount += 10;
}
result.append(("\nTotal"+totalamount + "RS"));
Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();
}
});
}
}
logcat 2020-11-05 00:33:48.523 27490-27966/com.example.newsplash D/mmscene: getHint applicationScene= com.example.newsplash,idx=0 none 2020-11-05 00:33:48.577 27490-27490/com.example.newsplash I/Choreographer: 跳过7帧!应用程序可能在其主线程上做了太多工作。 2020-11-05 00:33:51.378 27490-27490/com.example.newsplash W/ActivityThread:handleWindowVisibility:没有 activity 令牌 android.os.BinderProxy@3268ab0 2020-11-05 00:33:51.928 27490-27490/com.example.newsplash D/AndroidRuntime: 关闭 VM 2020-11-05 00:33:51.938 27490-27490/com.example.newsplash E/AndroidRuntime :致命例外:主要 进程:com.example.newsplash,PID:27490 java.lang.RuntimeException: 无法启动 activity ComponentInfo{com.example.newsplash/com.example.newsplash.MainActivity}: java.lang.NullPointerException: 尝试调用虚拟方法 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' 在空对象引用上 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3146) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3296) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:114) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:74) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:226) 在 android.app.ActivityThread.main(ActivityThread.java:7224) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:500) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:913) 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚方法 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' 在 com.example.newsplash.MainActivity.addListenerOnButtonClick(MainActivity.java:36) 在 com.example.newsplash.MainActivity.onCreate(MainActivity.java:23) 在 android.app.Activity.performCreate(Activity.java:7337) 在 android.app.Activity.performCreate(Activity.java:7328) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3126) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3296) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:114) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:74) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1994) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:226) 在 android.app.ActivityThread.main(ActivityThread.java:7224) 在 java.lang.reflect.Method.invoke(本机方法)
- 您的代码
您创建了一个变量 Button order
但您从未对其进行初始化。
然后,在未初始化的按钮上,您调用 .setOnClickListener
,这显然无法完成。
您的堆栈跟踪:
java.lang.NullPointerException: 尝试在空对象引用
上调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
解决方案是对 order
使用 findViewById
并获得实际的按钮参考,然后您可以使用 .setOnClickListener
**是的,现在有效了。谢谢你 ** public class MainActivity 扩展 AppCompatActivity {
CheckBox Chapathi, Dosai, Biriyani, Coffee;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButtonClick();
}
public void addListenerOnButtonClick() {
Chapathi = (CheckBox) findViewById(R.id.Chapathi);
Biriyani = (CheckBox) findViewById(R.id.Biriyani);
Dosai = (CheckBox) findViewById(R.id.Dosai);
Coffee = (CheckBox) findViewById(R.id.Coffee);
button = (Button) findViewById(R.id.order);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick (View view)
{
int totalamount = 0;
StringBuilder result = new StringBuilder();
result.append("Selected Items:");
if (Chapathi.isChecked()) {
result.append("\nChapathi 10RS");
totalamount += 10;
}
if (Biriyani.isChecked()) {
result.append("\nBiriyani is 100RS");
totalamount += 100;
}
if (Dosai.isChecked()) {
result.append("\nDosai is 10RS");
totalamount += 10;
}
if (Coffee.isChecked()) {
result.append("\n Coffee is 10RS");
totalamount += 10;
}
result.append(("\nTotal"+totalamount + "RS"));
Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();
}
});
}
}