我正在制作一个正确的马电池主食式密码生成器。我设置它是为了有一个TextView,当你点击它时,它切换活动如下:
TextView moreInfo = (TextView) findViewById(R.id.moreinfo);
moreInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Description.class);
startActivity(intent);
};
});
但是,当我运行该程序并单击更多信息textview时,我收到此错误:
07-14 12:34:15.930 2295-2295/com.example.bobmo.correcthorsebatterystaple E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bobmo.correcthorsebatterystaple, PID: 2295
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bobmo.correcthorsebatterystaple/com.example.bobmo.correcthorsebatterystaple.Description}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setClickable(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setClickable(boolean)' on a null object reference
at com.example.bobmo.correcthorsebatterystaple.Description.onCreate(Description.java:16)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
......在电话上,它说节目已经停止了。有没有什么办法解决这一问题?谢谢!
答案 0 :(得分:2)
TextView moreInfo = (TextView) findViewById(R.id.moreinfo);
findViewById
返回null,因为它没有在当前活动内容视图中找到该视图,您可能没有设置正确的布局,或者该布局不包含任何ID为R.id.moreinfo
的视图