2

如何简单地显示另一个 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)
4

2 回答 2

0

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

于 2012-12-26T19:46:10.650 回答
0

始终ActivityonCreate回调中设置内容视图:

public class About extends Activity {

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

您不应该尝试使用 的构造函数,从该方法开始Activity初始化,因为从该回调向前有效。ActivityonCreateContext

于 2012-12-26T19:46:11.070 回答