简体   繁体   中英

setOnClickListener(new View.OnClickListener() creates java.lang.NullPointerException

I'm following a online tutorial but I can't work out what I'm doing wrong. I'm trying to create a clickable 'sign up' text. Here is my code

protected TextView mSignUpTextView;

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

    mSignUpTextView = (TextView)findViewById(R.id.signUpText);
    mSignUpTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(LoginActivity.this, SignUpActivity.class);
            startActivity(intent);
        }
    });

and here is the logcat error

04-02 11:55:11.683: E/AndroidRuntime(31564): FATAL EXCEPTION: main
04-02 11:55:11.683: E/AndroidRuntime(31564): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pbg.swapzy/com.pbg.swapzy.LoginActivity}: java.lang.NullPointerException
04-02 11:55:11.683: E/AndroidRuntime(31564):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
04-02 11:55:11.683: E/AndroidRuntime(31564):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
04-02 11:55:11.683: E/AndroidRuntime(31564):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
04-02 11:55:11.683: E/AndroidRuntime(31564):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
04-02 11:55:11.683: E/AndroidRuntime(31564):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 11:55:11.683: E/AndroidRuntime(31564):    at android.os.Looper.loop(Looper.java:137)
04-02 11:55:11.683: E/AndroidRuntime(31564):    at android.app.ActivityThread.main
(ActivityThread.java:4895)
04-02 11:55:11.683: E/AndroidRuntime(31564):    at java.lang.reflect.Method.invokeNative(Native Method)
04-02 11:55:11.683: E/AndroidRuntime(31564):    at java.lang.reflect.Method.invoke(Method.java:511)
04-02 11:55:11.683: E/AndroidRuntime(31564):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
04-02 11:55:11.683: E/AndroidRuntime(31564):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
04-02 11:55:11.683: E/AndroidRuntime(31564):    at dalvik.system.NativeStart.main(Native Method)
04-02 11:55:11.683: E/AndroidRuntime(31564): Caused by: java.lang.NullPointerException
04-02 11:55:11.683: E/AndroidRuntime(31564):    at com.pbg.swapzy.LoginActivity.onCreate(LoginActivity.java:24)
04-02 11:55:11.683: E/AndroidRuntime(31564):    at android.app.Activity.performCreate(Activity.java:5163)
04-02 11:55:11.683: E/AndroidRuntime(31564):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
04-02 11:55:11.683: E/AndroidRuntime(31564):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
04-02 11:55:11.683: E/AndroidRuntime(31564):    ... 11 more

and the xml code is

<TextView
    android:id="@+id/signUpText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/sign_up_text" />

Line 24 is

 mSignUpTextView.setOnClickListener(new View.OnClickListener() {

Project -> Clean this will probably fix it. Eclipse sometimes does not refresh the R file. Also make sure you load the correct xml file (just in case)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM