問題與TabActivity ...的Android TabActivity NullPointerException異常
我得到這個異常
java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:1651)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:656)
at android.widget.TabHost.setCurrentTab(TabHost.java:326)
at android.widget.TabHost.addTab(TabHost.java:216)
at home.android.yahtzee.activities.GameActivity.onCreate(GameActivity.java:36)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
at android.app.ActivityThread.access$1500(ActivityThread.java:132)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4196)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
與此代碼
public class GameActivity extends TabActivity {
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.tab_handler);
Gson gson = new Gson();
Player player = WhoIsPlayingDialog.playerSelected;
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost();
tabHost.setup();
tabHost.setTag(this);
TabHost.TabSpec spec = tabHost.newTabSpec(player.getName());
Intent intent = new Intent(GameActivity.this, GamePlayerActivity.class);
intent.putExtra("player", gson.toJson(player));
spec.setContent(intent);
spec.setIndicator(player.getName(), res.getDrawable(player.getDroid()));
tabHost.addTab(spec); <============ Line 36.
}
}
而且我不知道爲什麼。誰能告訴我?
謝謝你的迴應。我將刪除'tabHost.setup()'。我檢查過並且'res.getDrawable(player.getDroid())''player.getName()'不爲空。 – Bastaix
你是否在佈局文件中用'android:id =「@ + android:id/tabhost」'指定了你的TabHost? 您是否在清單文件中聲明瞭'GamePlayerActivity.class'? –
是的,這兩個問題。 – Bastaix