[英]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 这是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
and the xml code is 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" />
Line 24 is 第24行是
mSignUpTextView.setOnClickListener(new View.OnClickListener() {
Project -> Clean this will probably fix it. 项目->清理此问题可能会解决。 Eclipse sometimes does not refresh the R file.
Eclipse有时不刷新R文件。 Also make sure you load the correct xml file (just in case)
另外,请确保加载正确的xml文件(以防万一)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.