1

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

主要活动

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>

日志猫

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
4

2 回答 2

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);
}
于 2013-10-09T00:49:08.527 回答
1

在你的 onCreate 方法中使用它:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
于 2013-10-09T00:48:25.447 回答