setOnClickListener(new View.OnClickListener()创建java.lang.NullPointerException

时间:2014-04-02 04:08:45

标签: java android eclipse

我正在关注在线教程,但我无法弄清楚我做错了什么。 我正在尝试创建一个可点击的“注册”文本。这是我的代码

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);
        }
    });

这是logcat错误

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

,xml代码是

<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" />

第24行

 mSignUpTextView.setOnClickListener(new View.OnClickListener() {

1 个答案:

答案 0 :(得分:1)

项目 - &gt;清洁这可能会解决它。 Eclipse有时不会刷新R文件。 还要确保加载正确的xml文件(以防万一)