在Activity中访问窗口小部件时出现NullPointerException

时间:2013-10-09 00:43:34

标签: android android-activity nullpointerexception android-widget

我的LogCat显示NullPointerException,无论我作为主要活动运行哪个代码。我在相同或不同项目中尝试的所有活动都发生了同样的异常。

MainActivity

public class MainActivity extends Activity implements View.OnClickListener{
    private static EditText et;
    private static Button b;
    private static TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        et=(EditText)findViewById(R.id.editText1);
        Button b=(Button)findViewById(R.id.button1);
        TextView tv=(TextView)findViewById(R.id.textView1);
        b.setOnClickListener(this);

        setContentView(R.layout.activity_main);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        tv.setText(et.getText());
    }
}

activity_main.xml中

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="73dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="68dp"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="72dp"
        android:text="TextView" />

</RelativeLayout>

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.stuff"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.android.stuff.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

logcat的

10-09 05:52:58.953: E/Trace(667): error opening trace file: No such file or directory (2)
10-09 05:53:00.152: D/AndroidRuntime(667): Shutting down VM
10-09 05:53:00.172: W/dalvikvm(667): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
10-09 05:53:00.202: E/AndroidRuntime(667): FATAL EXCEPTION: main
10-09 05:53:00.202: E/AndroidRuntime(667): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.stuff/com.android.stuff.MainActivity}:   java.lang.NullPointerException
10-09 05:53:00.202: E/AndroidRuntime(667):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-09 05:53:00.202: E/AndroidRuntime(667):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-09 05:53:00.202: E/AndroidRuntime(667):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-09 05:53:00.202: E/AndroidRuntime(667):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-09 05:53:00.202: E/AndroidRuntime(667):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 05:53:00.202: E/AndroidRuntime(667):  at android.os.Looper.loop(Looper.java:137)
10-09 05:53:00.202: E/AndroidRuntime(667):  at android.app.ActivityThread.main(ActivityThread.java:4745)
10-09 05:53:00.202: E/AndroidRuntime(667):  at java.lang.reflect.Method.invokeNative(Native Method)
10-09 05:53:00.202: E/AndroidRuntime(667):  at java.lang.reflect.Method.invoke(Method.java:511)
10-09 05:53:00.202: E/AndroidRuntime(667):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-09 05:53:00.202: E/AndroidRuntime(667):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-09 05:53:00.202: E/AndroidRuntime(667):  at dalvik.system.NativeStart.main(Native Method)
10-09 05:53:00.202: E/AndroidRuntime(667): Caused by: java.lang.NullPointerException
10-09 05:53:00.202: E/AndroidRuntime(667):  at com.android.stuff.MainActivity.onCreate(MainActivity.java:23)
10-09 05:53:00.202: E/AndroidRuntime(667):  at android.app.Activity.performCreate(Activity.java:5008)
10-09 05:53:00.202: E/AndroidRuntime(667):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-09 05:53:00.202: E/AndroidRuntime(667):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-09 05:53:00.202: E/AndroidRuntime(667):  ... 11 more
10-09 05:53:03.652: I/Process(667): Sending signal. PID: 667 SIG: 9

2 个答案:

答案 0 :(得分:6)

在访问组件之前设置内容视图。另外,不要在onCreate()中再次声明类型,因为它们将成为掩盖静态变量的局部变量。

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

    et=(EditText)findViewById(R.id.editText1);
    b=(Button)findViewById(R.id.button1);
    tv=(TextView)findViewById(R.id.textView1);

    b.setOnClickListener(this);
}

答案 1 :(得分:1)

在onCreate方法中使用它:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);