setOnClickListener导致“RuntimeException:无法启动” - NullPointerException

时间:2013-04-14 19:05:53

标签: java android

我收到了一个错误,我无法理解造成错误的原因。

这是产生错误的代码片段:

public class RssReader extends Activity {
private Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
    button1 = (Button) findViewById(R.id.b1);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            System.out.println("test");
        }
      });

    super.onCreate(savedInstanceState);
    setContentView(R.layout.rss_reader);
    }
}

虽然这是我的rss_reader.xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

</LinearLayout>

以上代码导致 NullPointerException ,但我无法理解我访问的空指针,因为我没有使用任何不安全的构造。完整的错误如下:

04-14 18:50:14.305: E/AndroidRuntime(443): FATAL EXCEPTION: main
04-14 18:50:14.305: E/AndroidRuntime(443): java.lang.RuntimeException: Unable to start              activity ComponentInfo{com.cris96.rssreader/com.cris96.rssreader.RssReader}:  java.lang.NullPointerException
04-14 18:50:14.305: E/AndroidRuntime(443):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-14 18:50:14.305: E/AndroidRuntime(443):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-14 18:50:14.305: E/AndroidRuntime(443):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-14 18:50:14.305: E/AndroidRuntime(443):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-14 18:50:14.305: E/AndroidRuntime(443):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 18:50:14.305: E/AndroidRuntime(443):  at android.os.Looper.loop(Looper.java:123)
04-14 18:50:14.305: E/AndroidRuntime(443):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-14 18:50:14.305: E/AndroidRuntime(443):  at java.lang.reflect.Method.invokeNative(Native Method)
04-14 18:50:14.305: E/AndroidRuntime(443):  at java.lang.reflect.Method.invoke(Method.java:507)
04-14 18:50:14.305: E/AndroidRuntime(443):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-14 18:50:14.305: E/AndroidRuntime(443):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-14 18:50:14.305: E/AndroidRuntime(443):  at dalvik.system.NativeStart.main(Native Method)
04-14 18:50:14.305: E/AndroidRuntime(443): Caused by: java.lang.NullPointerException
04-14 18:50:14.305: E/AndroidRuntime(443):  at com.cris96.rssreader.RssReader.onCreate(RssReader.java:14)
04-14 18:50:14.305: E/AndroidRuntime(443):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-14 18:50:14.305: E/AndroidRuntime(443):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-14 18:50:14.305: E/AndroidRuntime(443):  ... 11 more

我该如何解决?

谢谢

2 个答案:

答案 0 :(得分:11)

在注册setContentView(R.layout.rss_reader);之前,您必须致电OnClickListeners

package com.cris96.rssreader;

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

public class RssReader extends Activity {
    private Button button1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rss_reader);

        button1 = (Button) findViewById(R.id.b1);
        button1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                System.out.println("test");

            }
        });

    }

}

答案 1 :(得分:4)

你需要这样做:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rss_reader);
button1 = (Button)findViewById(R.id.b1);
button1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        System.out.println("test");
    }
  });
}

在将布局设置为Activity的内容之前,无法设置查找视图。 这就是findViewById返回null的原因。