调用onClickListener时出现NullPointerException

时间:2017-03-22 02:13:46

标签: java android nullpointerexception

我正在制作登录系统,我不知道为什么会这样。 我该如何解决这个问题? 这是关于NullPointerException的类的一部分。

    final TextView registerButton = (TextView)findViewById(R.id.registerButton);
    registerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent registerIntent=new Intent(LoginActivity.this, RegisterActivity.class);
            LoginActivity.this.startActivity(registerIntent);
        }
    });

以下是logcat的上下文。

03-22 10:37:13.066 8500-8500/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.example.gibong.capstone-1@split_lib_dependencies_apk.apk@classes.dex: Permission denied
03-22 10:37:13.141 3897-3897/? E/SDAgentPackageStateReceiver: Not going to handle 'com.example.gibong.capstone'!
03-22 10:37:13.166 6707-6707/? E/Launcher.Model: onPackageAdded :com.example.gibong.capstone
03-22 10:37:13.296 8526-8526/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.example.gibong.capstone-1@split_lib_slice_0_apk.apk@classes.dex: Permission denied
03-22 10:37:13.341 8533-8533/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.example.gibong.capstone-1@split_lib_slice_1_apk.apk@classes.dex: Permission denied
03-22 10:37:13.391 8539-8539/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.example.gibong.capstone-1@split_lib_slice_2_apk.apk@classes.dex: Permission denied
03-22 10:37:13.461 8543-8543/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.example.gibong.capstone-1@split_lib_slice_3_apk.apk@classes.dex: Permission denied
03-22 10:37:13.516 8561-8561/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.example.gibong.capstone-1@split_lib_slice_4_apk.apk@classes.dex: Permission denied
03-22 10:37:13.566 8567-8567/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.example.gibong.capstone-1@split_lib_slice_5_apk.apk@classes.dex: Permission denied
03-22 10:37:13.616 8572-8572/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.example.gibong.capstone-1@split_lib_slice_6_apk.apk@classes.dex: Permission denied
03-22 10:37:13.666 8577-8577/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.example.gibong.capstone-1@split_lib_slice_7_apk.apk@classes.dex: Permission denied
03-22 10:37:13.716 8581-8581/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.example.gibong.capstone-1@split_lib_slice_8_apk.apk@classes.dex: Permission denied
03-22 10:37:13.771 8587-8587/? E/dex2oat: Failed to create oat file: /data/dalvik-cache/arm/data@app@com.example.gibong.capstone-1@split_lib_slice_9_apk.apk@classes.dex: Permission denied
03-22 10:37:14.511 8474-8474/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.example.gibong.capstone, PID: 8474
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gibong.capstone/com.example.gibong.capstone.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
                                                     at android.app.ActivityThread.access$1100(ActivityThread.java:221)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:158)
                                                     at android.app.ActivityThread.main(ActivityThread.java:7225)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                                                     at com.example.gibong.capstone.LoginActivity.onCreate(LoginActivity.java:35)
                                                     at android.app.Activity.performCreate(Activity.java:6876)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) 
                                                     at android.app.ActivityThread.access$1100(ActivityThread.java:221) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:158) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:7225) 
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

由于我不能说流利的英语和本网站,因为我对英语和文字风格的使用不好而道歉。

6 个答案:

答案 0 :(得分:2)

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object 

在下面一行

final TextView registerButton = (TextView)findViewById(R.id.registerButton);

您是否传递了正确的id?检查xml文件中是否存在R.id.registerButton

答案 1 :(得分:1)

Button alse扩展了TextView,所以没有问题:

final TextView registerButton = (TextView)findViewById(R.id.registerButton); 

所以,问题是您的活动在xml中找不到View(id = registerButton),请检查一下。

答案 2 :(得分:0)

错误

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.gibong.capstone.LoginActivity.onCreate(LoginActivity.java:35)

问题出在这一行

final TextView registerButton = (TextView)findViewById(R.id.registerButton);  // This is null

确保您的活动布局中的TextView具有android:id="@+id/registerButton"属性

或者检查它是TextView还是Button

答案 3 :(得分:0)

确保布局文件包含id为" registerButton"的视图。 在onCreate()中声明的 通过此代码setContentView(R.layout.activity_account);

答案 4 :(得分:0)

当您尝试引用不存在的对象时,将发生NullPointerException。

尝试这些(假设你已经为一个TextView声明了OnclickLisener,其中包含android:id =" @ + id / registerButton"在XML中)

  • 在setContentView
  • 下定义TextView registerButton = (TextView)findViewById(R.id.registerButton);

答案 5 :(得分:-1)

您尝试将TextView对象转换为Button对象,或者在xml布局中不存在registerButton id。