继续收到此错误我不知道

时间:2016-01-26 07:52:35

标签: android

    TextView drawerName = (TextView) findViewById(R.id.drawerName);
    TextView drawerEmail = (TextView) findViewById(R.id.drawerEmail);
    CircleImageView drawerAvatar = (CircleImageView) findViewById(R.id.drawerAvatar);

    drawerName.setText(personName);
    drawerEmail.setText(personEmail);
  

致命的例外:主要                                                    过程:com.mje.dev.liqourstore,PID:1537                                                    java.lang.RuntimeException:无法启动活动ComponentInfo {com.mje.dev.liqourstore / com.mje.dev.liqourstore.MainActivity}:java.lang.NullPointerException:尝试调用虚方法'void android.widget.TextView。 null对象引用上的setText(java.lang.CharSequence)'                                                        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3149)                                                        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3248)                                                        在android.app.ActivityThread.access $ 1000(ActivityThread.java:197)                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1681)                                                        在android.os.Handler.dispatchMessage(Handler.java:102)                                                        在android.os.Looper.loop(Looper.java:145)                                                        在android.app.ActivityThread.main(ActivityThread.java:6872)                                                        at java.lang.reflect.Method.invoke(Native Method)                                                        在java.lang.reflect.Method.invoke(Method.java:372)                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1404)                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)                                                     引发者:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)'                                                        在com.mje.dev.liqourstore.MainActivity.onCreate(MainActivity.java:146)                                                        在android.app.Activity.performCreate(Activity.java:6550)                                                        在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)                                                        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3102)                                                        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3248)                                                        在android.app.ActivityThread.access $ 1000(ActivityThread.java:197)                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1681)                                                        在android.os.Handler.dispatchMessage(Handler.java:102)                                                        在android.os.Looper.loop(Looper.java:145)                                                        在android.app.ActivityThread.main(ActivityThread.java:6872)                                                        at java.lang.reflect.Method.invoke(Native Method)                                                        在java.lang.reflect.Method.invoke(Method.java:372)                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1404)                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

2 个答案:

答案 0 :(得分:0)

通过将contentView设置为View,您似乎未设置Activity setContentView。此外,还请检查setContentView布局是否还有您要提取的textview

希望这会有所帮助。

答案 1 :(得分:0)

    TextView drawerName = (TextView) navigationView.getHeaderView(0).findViewById(R.id.navDrawerName);
    TextView drawerEmail = (TextView) navigationView.getHeaderView(0).findViewById(R.id.navDrawerEmail);
    CircleImageView  drawerAvatar = (CircleImageView) navigationView.getHeaderView(0).findViewById(R.id.avatar);