別の 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)