Android splash之后,我添加了addListenerOnButtonClick();在mainactivity.java中,应用程序崩溃
mainactivity.java 包com.example.newsplash 导入androidx.appcompat.app.appcompat活动 导入android.os.Bundle;导入android.view.view;导入android.widget.Button;导入android.widget.CheckBox;导入android.widget.ToastAndroid splash之后,我添加了addListenerOnButtonClick();在mainactivity.java中,应用程序崩溃,android,crash,screen,splash-screen,android-appcompat,Android,Crash,Screen,Splash Screen,Android Appcompat,mainactivity.java 包com.example.newsplash 导入androidx.appcompat.app.appcompat活动 导入android.os.Bundle;导入android.view.view;导入android.widget.Button;导入android.widget.CheckBox;导入android.widget.Toast public class MainActivity extends AppCompatActivity {
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();
}
});
}
}
logcat2020-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:token android.os无活动。BinderProxy@3268ab02020-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:致命异常:main
进程: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(本机方法)
按钮顺序
,但从未对其进行初始化。
然后,在一个未初始化的按钮上,调用.setOnClickListener
,这显然是无法完成的
解决方案是对订单使用
findViewById
,并获得一个实际的按钮引用,之后您可以使用。setOnClickListener
**现在是了。多谢各位**
公共类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();
}
});
}
}@btmme如果答案对您有帮助,请在我的答案左侧打勾,将其标记为
已接受答案。非常感谢。好的,兄弟。完成。感谢您分享您的时间和知识。