Andriod应用程序崩溃,并提供错误尝试在空对象引用

时间:2017-03-04 16:35:55

标签: android nullpointerexception onclicklistener runtimeexception

由于以下错误,我试图制作类似Twitter的应用,我的应用程序崩溃了:

  

java.lang.NullPointerException:尝试调用虚方法' void android.widget.TextView.setOnClickListener(android.view.View $ OnClickListener)'在空对象引用上

这是我的 LoginActivity 代码:

package com.ghayoorshan.nestedlayout1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class LoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        TextView registerScreen = (TextView) findViewById(R.id.link_to_register);
        // Button button = (Button)findViewById(R.id.link_to_register);
        // Listening to register new account link
        registerScreen.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
        // Switching to Register screen
                Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
                startActivity(i);
            }
        });
    }
}

错误发生在以下行:

registerScreen.setOnClickListener(new View.OnClickListener()...

logcat的:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ghayoorshan.nestedlayout1/com.ghayoorshan.nestedlayout1.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:2552)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2618)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:207)
        at android.app.ActivityThread.main(ActivityThread.java:5701)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
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.ghayoorshan.nestedlayout1.LoginActivity.onCreate(LoginActivity.java:18)
        at android.app.Activity.performCreate(Activity.java:6294)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2505)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2618) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:207) 
        at android.app.ActivityThread.main(ActivityThread.java:5701) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
03-04 02:41:33.060 24340-24340/com.ghayoorshan.nestedlayout1 I/Process: Sending signal. PID: 24340 SIG: 9

导致错误的原因是什么?

2 个答案:

答案 0 :(得分:1)

您的布局xml“activity_login.xml”不包含ID为link_to_register的TextView,或者其ID不同。检查“activity_login.xml”以找到确切的问题。

答案 1 :(得分:0)

@Passiondroid这是我的XML代码我觉得每件事都在这里

keytool error:java.io.FileNotFoundException: keystore.jks (Access is denied)