嗨,我一直在摸不着头脑。我理解NullPtr是什么以及为什么编译器抛出异常,我只是不知道为什么。以下是相关代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button signBtn = (Button) findViewById(R.id.signInBtn);
signBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
}
});
空指针是signBtn.setOnClickListener()
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: FATAL EXCEPTION: main
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: Process: com.lukewaugh.app, PID: 10219
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lukewaugh.app/com.lukewaugh.app.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2354)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2416)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:154)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1322)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5300)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at com.lukewaugh.app.MainActivity.onCreate(MainActivity.java:30)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6088)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2416)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at android.app.ActivityThread.access$900(ActivityThread.java:154)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1322)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5300)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
01-20 20:15:02.284 10219-10219/com.lukewaugh.app E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
01-20 20:15:02.286 10219-10219/com.lukewaugh.app D/AppTracker: App Event: crash
01-20 20:15:02.337 10219-10219/com.lukewaugh.app I/Process: Sending signal. PID: 10219 SIG: 9
答案 0 :(得分:0)
(新的View.OnClickListener()方法在代码中生成空指针异常。尝试初始化View类对象。