请帮助我,打开应用时我的应用会不断崩溃

时间:2018-10-26 01:21:32

标签: android-studio

E /:无法加载libgui-plugin D / AndroidRuntime:关闭VM E / AndroidRuntime:致命异常:main     进程:com.example.ayushjain.myapplication33,PID:24308     java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.ayushjain.myapplication33 / com.example.ayushjain.myapplication33.Main2Activity}:java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.Button。在空对象引用上的setOnClickListener(android.view.View $ OnClickListener)'         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2911)         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2989)         在android.app.ActivityThread.-wrap11(未知来源:0)         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1688)         在android.os.Handler.dispatchMessage(Handler.java:106)         在android.os.Looper.loop(Looper.java:164)         在android.app.ActivityThread.main(ActivityThread.java:6753)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:482)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)      原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'         在com.example.ayushjain.myapplication33.Main2Activity.onCreate(Main2Activity.java:37)         在android.app.Activity.performCreate(Activity.java:7041)         在android.app.Activity.performCreate(Activity.java:7032)         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1231)         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2864)         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2989)         在android.app.ActivityThread.-wrap11(未知来源:0)         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1688)         在android.os.Handler.dispatchMessage(Handler.java:106)         在android.os.Looper.loop(Looper.java:164)         在android.app.ActivityThread.main(ActivityThread.java:6753)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:482)         com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

1 个答案:

答案 0 :(得分:0)

您没有在执行Button.setOnClickListener之前实例化您的按钮,因此您可以通过这种方式解决

Button your_button = (Button) findViewById(R.id. your_button);

然后按如下所示添加您的点击侦听器,

your_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//do your stuff
});