【问题标题】:after splash i added addListenerOnButtonClick(); in mainactivity.java then app crashes飞溅后我添加了 addListenerOnButtonClick();在 mainactivity.java 然后应用程序崩溃
【发布时间】:2021-02-17 04:19:40
【问题描述】:

ma​​inactivity.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:令牌 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: FATAL EXCEPTION: main 进程:com.example.newsplash,PID:27490 java.lang.RuntimeException:无法启动活动 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(Native Method)

【问题讨论】:

    标签: android crash screen splash-screen android-appcompat


    【解决方案1】:
    1. 您的代码

    您创建了一个变量Button order,但您从未初始化它。 然后,在一个未初始化的按钮上,你调用了.setOnClickListener,这显然是做不到的。

    1. 您的堆栈跟踪:

      java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)”

    解决方案是将findViewById 用于order 并获得一个实际的按钮引用,之后您可以使用.setOnClickListener

    【讨论】:

    • @btmme 如果答案对您有所帮助,请将其标记为accepted answer,并在我的答案左侧打勾。非常感谢。
    • 好的,兄弟。完毕 。感谢您分享您的时间和知识。
    【解决方案2】:

    **是的,现在有效。谢谢 ** 公共类 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();
            }
    
    
        });
    
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多