Android Studio运行时错误

时间:2017-04-21 14:27:06

标签: android android-studio

我是Android Studio新手。我试着执行“Hello World!”代码使用android nougat 7.1.1并得到此错误:

  

E / AndroidRuntime:致命异常:主要                     过程:com.myfirstapp.myapplication,PID:5369                     java.lang.RuntimeException:无法启动活动ComponentInfo {com.myfirstapp.myapplication / com.myfirstapp.myapplication.MainActivity}:   java.lang.NullPointerException:尝试调用虚方法'void   android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View $ OnClickListener)”   在null对象引用上                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)                         在android.app.ActivityThread.-wrap12(ActivityThread.java)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:154)                         在android.app.ActivityThread.main(ActivityThread.java:6119)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)                      引起:java.lang.NullPointerException:尝试调用虚方法'void   android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View $ OnClickListener)”   在null对象引用上                         在com.myfirstapp.myapplication.MainActivity.onCreate(MainActivity.java:22)                         在android.app.Activity.performCreate(Activity.java:6679)                         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)                         在android.app.ActivityThread.-wrap12(ActivityThread.java)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:154)                         在android.app.ActivityThread.main(ActivityThread.java:6119)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)   申请已终止。

我该怎么办?

Error

1 个答案:

答案 0 :(得分:0)

您必须在x参考中将您的xml布局中的Fab分配给您的Fab。它是说你试图将OnClickListener设置为一个没有值的对象或一个" null"值。

android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View $ OnClickListener)'在

的空对象引用上