在Android应用程序中的按钮上设置onClickListener时为空指针

时间:2014-07-28 18:20:59

标签: java android onclicklistener

我试图为点击开始新活动的按钮设置onClickListener。当我尝试将监听器设置为按钮时,我得到一个Null Pointer Exception,我对此无能为力。这是一个非常简单的程序,我以前做过这个。这是我的代码:

主要活动

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {

private Button next;

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

    next = (Button) findViewById(R.id.next);
    next.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.next:
        // Signin button clicked
        startActivity(new Intent(MainActivity.this, Activity2.class));
        break;
    }
}
}

xml布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".SplashScreen" >

<Button
    android:id="@+id/next"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Next Activity" />

</LinearLayout>

logcat的

07-28 14:16:19.387: E/AndroidRuntime(11507): FATAL EXCEPTION: main
07-28 14:16:19.387: E/AndroidRuntime(11507): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.SplashScreen}: java.lang.NullPointerException
07-28 14:16:19.387: E/AndroidRuntime(11507):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
07-28 14:16:19.387: E/AndroidRuntime(11507):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
07-28 14:16:19.387: E/AndroidRuntime(11507):    at android.app.ActivityThread.access$600(ActivityThread.java:153)
07-28 14:16:19.387: E/AndroidRuntime(11507):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
07-28 14:16:19.387: E/AndroidRuntime(11507):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 14:16:19.387: E/AndroidRuntime(11507):    at android.os.Looper.loop(Looper.java:137)
07-28 14:16:19.387: E/AndroidRuntime(11507):    at android.app.ActivityThread.main(ActivityThread.java:5289)
07-28 14:16:19.387: E/AndroidRuntime(11507):    at java.lang.reflect.Method.invokeNative(Native Method)
07-28 14:16:19.387: E/AndroidRuntime(11507):    at java.lang.reflect.Method.invoke(Method.java:525)
07-28 14:16:19.387: E/AndroidRuntime(11507):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
07-28 14:16:19.387: E/AndroidRuntime(11507):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
07-28 14:16:19.387: E/AndroidRuntime(11507):    at dalvik.system.NativeStart.main(Native Method)
07-28 14:16:19.387: E/AndroidRuntime(11507): Caused by: java.lang.NullPointerException
07-28 14:16:19.387: E/AndroidRuntime(11507):    at com.example.test.SplashScreen.onCreate(SplashScreen.java:21)
07-28 14:16:19.387: E/AndroidRuntime(11507):    at android.app.Activity.performCreate(Activity.java:5133)
07-28 14:16:19.387: E/AndroidRuntime(11507):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-28 14:16:19.387: E/AndroidRuntime(11507):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)

错误在第21行,第21行是:

next.setOnClickListener(this);

3 个答案:

答案 0 :(得分:1)

编辑:错误发生在SplashScreen.java 21行,而不是MainActivity.java 21行

您确定您提供的布局是activity_main.xml吗?因为它看起来不是activity_main.xml,因为findViewById找不到next按钮。

答案 1 :(得分:0)

您发布的代码似乎是正确的,但错误堆栈表示异常是在 com.example.test / com.example.test.SplashScreen 中抛出的NOT MainActivity 。调试到该类,或在此处发布其代码。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.SplashScreen}: java.lang.NullPointerException

答案 2 :(得分:-5)

“这个”是你的活动,而不是听众。试试这个:

next.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // What do do when "next" is clicked
       }
});