新活动抛出NullPointerException

时间:2012-12-26 17:12:23

标签: android xml nullpointerexception android-activity

如何只显示另一个xml文件?我创建了一个新的活动调用about.java来执行此操作,但它与NullPointerException崩溃了。这是我的主要课程叫about.class

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case 1:
            Log.d("**MYLOG**", "Refresh clicked .. ");
            Intent intent = new Intent(this, MainActivity.class);
            this.startActivity(intent);
            return true;
        case 2:
            //setContentView(R.layout.about);
            Log.d("**MYLOG**", "About clicked .. ");
            Intent intent2 = new Intent(this, About.class);
            this.startActivity(intent2);
            return true;

这是我的about.class

public class About extends Activity {

public About() {
     Log.d("**MYLOG**", "Inside About.java ..");
     setContentView(R.layout.about);

}
}

和我的about.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="horizontal">
        <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:typeface="sans"
        android:textSize="18dp"
        android:text="About page"
        android:gravity="center"></TextView>

这是我的日志:

12-26 14:28:43.560: D/**MYLOG**(1141): Inside About.java ..
12-26 14:28:43.560: D/AndroidRuntime(1141): Shutting down VM
12-26 14:28:43.560: W/dalvikvm(1141): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
12-26 14:28:43.600: E/AndroidRuntime(1141): FATAL EXCEPTION: main
12-26 14:28:43.600: E/AndroidRuntime(1141): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{wia.home/wia.home.About}: java.lang.NullPointerException
12-26 14:28:43.600: E/AndroidRuntime(1141):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
12-26 14:28:43.600: E/AndroidRuntime(1141):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-26 14:28:43.600: E/AndroidRuntime(1141):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-26 14:28:43.600: E/AndroidRuntime(1141):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-26 14:28:43.600: E/AndroidRuntime(1141):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 14:28:43.600: E/AndroidRuntime(1141):     at android.os.Looper.loop(Looper.java:137)
12-26 14:28:43.600: E/AndroidRuntime(1141):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-26 14:28:43.600: E/AndroidRuntime(1141):     at java.lang.reflect.Method.invokeNative(Native Method)
12-26 14:28:43.600: E/AndroidRuntime(1141):     at java.lang.reflect.Method.invoke(Method.java:511)
12-26 14:28:43.600: E/AndroidRuntime(1141):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-26 14:28:43.600: E/AndroidRuntime(1141):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-26 14:28:43.600: E/AndroidRuntime(1141):     at dalvik.system.NativeStart.main(Native Method)
12-26 14:28:43.600: E/AndroidRuntime(1141): Caused by: java.lang.NullPointerException
12-26 14:28:43.600: E/AndroidRuntime(1141):     at android.app.Activity.setContentView(Activity.java:1867)

2 个答案:

答案 0 :(得分:0)

奇怪的是,日志没有指定抛出NullPointerException的行。我在你的代码中可以看到的是,你应该在onCreate中调用setContentView,而不是在构造函数中调用。试试看,看看是否能解决问题。

答案 1 :(得分:0)

始终在Activity回调中设置onCreate的内容视图:

public class About extends Activity {

     public void onCreate(Bundle saved) {
        super.onCreate(saved);
         Log.d("**MYLOG**", "Inside About.java ..");
         setContentView(R.layout.about);
     }
}

您不应该尝试使用Activity的构造函数,从Activity方法开始初始化onCreate,因为Context在此回调转发中有效