タブアクティビティは現在廃止されていることがわかっているため、一部のチュートリアルでは、代わりに FragmentActivity を使用するように指示されています。私はこれを使用していますが、この行でヌルポインタ例外が発生します
tabHost.addTab(spec);
私のコードは以下です。
//MainActivity.java
public class MainActivity extends FragmentActivity {
public final static int HOME = 1;
public final static int CAMPUS = 2;
public final static int REPORTING = 3;
public final static int HELP = 4;
public final static int WISH = 5;
long transactionID = -1;
public static TabHost tabHost;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mytabs);
MyView view = null;
tabHost = (TabHost)findViewById(R.id.my_tabhost);
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, TabGroup1Activity.class);
view = new MyView(this, R.drawable.home, R.drawable.home_blue, "");
view.setFocusable(true);
spec = tabHost.newTabSpec("Home").setIndicator(view).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, TabGroup2Activity.class);
view = new MyView(this, R.drawable.home, R.drawable.home_blue, "");
view.setFocusable(true);
spec = tabHost.newTabSpec("Campus").setIndicator(view)
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, TabGroup3Activity.class);
view = new MyView(this, R.drawable.home, R.drawable.home_blue, "");
view.setFocusable(true);
spec = tabHost.newTabSpec("Reporting").setIndicator(view)
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, TabGroup4Activity.class);
view = new MyView(this, R.drawable.home, R.drawable.home_blue, "");
view.setFocusable(true);
spec = tabHost.newTabSpec("Help").setIndicator(view)
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, TabGroup5Activity.class);
view = new MyView(this, R.drawable.home, R.drawable.home_blue, "");
view.setFocusable(true);
spec = tabHost.newTabSpec("Wish").setIndicator(view).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = LayoutParams.WRAP_CONTENT;
tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = LayoutParams.WRAP_CONTENT;
tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = LayoutParams.WRAP_CONTENT;
tabHost.getTabWidget().getChildAt(3).getLayoutParams().height = LayoutParams.WRAP_CONTENT;
tabHost.getTabWidget().getChildAt(4).getLayoutParams().height = LayoutParams.WRAP_CONTENT;
tabHost.getTabWidget().getChildAt(0).getLayoutParams().width = LayoutParams.WRAP_CONTENT;
tabHost.getTabWidget().getChildAt(1).getLayoutParams().width = LayoutParams.WRAP_CONTENT;
tabHost.getTabWidget().getChildAt(2).getLayoutParams().width = LayoutParams.WRAP_CONTENT;
tabHost.getTabWidget().getChildAt(3).getLayoutParams().width = LayoutParams.WRAP_CONTENT;
tabHost.getTabWidget().getChildAt(4).getLayoutParams().width = LayoutParams.WRAP_CONTENT;
if (MyApp.getFrom().equals("Home")) {
tabHost.setCurrentTab(0);
} else if (MyApp.getFrom().equals("Campus")) {
tabHost.setCurrentTab(1);
} else if (MyApp.getFrom().equals("Reporting")) {
tabHost.setCurrentTab(2);
} else if (MyApp.getFrom().equals("Help")) {
tabHost.setCurrentTab(3);
} else if (MyApp.getFrom().equals("Wish")) {
tabHost.setCurrentTab(4);
}
int type = 0;
if (getIntent().getExtras() != null) {
if (getIntent().getExtras().containsKey("from")) {
type = getIntent().getExtras().getInt("from");
switch (type) {
case HOME:
tabHost.setCurrentTab(0);
case CAMPUS:
tabHost.setCurrentTab(1);
case REPORTING:
tabHost.setCurrentTab(2);
case HELP:
tabHost.setCurrentTab(3);
case WISH:
tabHost.setCurrentTab(4);
default:
tabHost.setCurrentTab(0);
}
}
}
}
public long getTransactionID() {
return transactionID;
}
public void setTransactionID(long l) {
transactionID = l;
}
public void switchTabSpecial(int tab) {
tabHost.setCurrentTab(tab);
}
class ChangeTabReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
tabHost = (TabHost)findViewById(R.id.my_tabhost);
tabHost.setCurrentTab(1);
}
}
private class MyView extends LinearLayout {
ImageView iv;
public MyView(Context c, int drawable, int drawableselec, String label) {
super(c);
iv = new ImageView(c);
StateListDrawable listDrawable = new StateListDrawable();
listDrawable.addState(SELECTED_STATE_SET, this.getResources()
.getDrawable(drawable));
listDrawable.addState(ENABLED_STATE_SET, this.getResources()
.getDrawable(drawableselec));
iv.setImageDrawable(listDrawable);
iv.setBackgroundColor(Color.TRANSPARENT);
iv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, (float) 0.0));
setGravity(Gravity.CENTER);
addView(iv);
}
}
}
//mytabs.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabHost
android:id="@+id/my_tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="0dp" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:padding="0dp" >
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</TabHost>
// エラー
02-05 10:57:22.301: E/AndroidRuntime(911): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ccc.loyolacollege/com.ccc.loyolacollege.MainActivity}: java.lang.NullPointerException
02-05 10:57:22.301: E/AndroidRuntime(911): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
02-05 10:57:22.301: E/AndroidRuntime(911): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-05 10:57:22.301: E/AndroidRuntime(911): at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-05 10:57:22.301: E/AndroidRuntime(911): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-05 10:57:22.301: E/AndroidRuntime(911): at android.os.Handler.dispatchMessage(Handler.java:99)
02-05 10:57:22.301: E/AndroidRuntime(911): at android.os.Looper.loop(Looper.java:137)
02-05 10:57:22.301: E/AndroidRuntime(911): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-05 10:57:22.301: E/AndroidRuntime(911): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 10:57:22.301: E/AndroidRuntime(911): at java.lang.reflect.Method.invoke(Method.java:511)
02-05 10:57:22.301: E/AndroidRuntime(911): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-05 10:57:22.301: E/AndroidRuntime(911): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-05 10:57:22.301: E/AndroidRuntime(911): at dalvik.system.NativeStart.main(Native Method)
02-05 10:57:22.301: E/AndroidRuntime(911): Caused by: java.lang.NullPointerException
02-05 10:57:22.301: E/AndroidRuntime(911): at android.widget.TabHost.addTab(TabHost.java:229)
02-05 10:57:22.301: E/AndroidRuntime(911): at com.ccc.loyolacollege.MainActivity.onCreate(MainActivity.java:38)
02-05 10:57:22.301: E/AndroidRuntime(911): at android.app.Activity.performCreate(Activity.java:4465)
02-05 10:57:22.301: E/AndroidRuntime(911): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-05 10:57:22.301: E/AndroidRuntime(911): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)