我为非常长的问题道歉但添加了额外的代码。
我有问题。我刚刚发布了一个应用程序,我在两部手机和模拟器上测试过。一切都很好。
但问题是我从使用该应用的人那里得到了一些例外。
我正在使用
intent.putExtra("continue", "newGame");
判断用户是开始新游戏还是继续上次保存的游戏。
由于我使用TabActivity
的扩展名,我将意图发送到TabActivity
String continueGame = getIntent().getExtras().getString("continue");
然后再添加额外的
intent.putExtra("continue", continueGame);
当我到达活动时,我尝试
String continueGame;
Bundle extras;
if (bundle == null) {
extras = getIntent().getExtras();
if (extras == null) {
continueGame = null;
} else {
continueGame = extras.getString("continue");
}
} else {
continueGame = (String) bundle.getSerializable("continue");
}
assert continueGame != null;
if (!continueGame.equals("continue")) { <===This is line 75 in GamePlayerActivity
playersGame = setNewGame();
startPlayingGame();
}
但在这里我得到了NullPointerException
。这是stack_trace
java.lang.RuntimeException: Unable to start activity ComponentInfo{home.android.yahtzee/home.android.yahtzee.activities.GameActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{home.android.yahtzee/home.android.yahtzee.activities.GamePlayerActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{home.android.yahtzee/home.android.yahtzee.activities.GamePlayerActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
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:328)
at android.widget.TabHost.addTab(TabHost.java:213)
at home.android.yahtzee.activities.GameActivity.onCreate(GameActivity.java:36)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more
Caused by: java.lang.NullPointerException
at home.android.yahtzee.activities.GamePlayerActivity.onCreate(GamePlayerActivity.java:75)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 20 more
java.lang.RuntimeException: Unable to start activity ComponentInfo{home.android.yahtzee/home.android.yahtzee.activities.GamePlayerActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
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:328)
at android.widget.TabHost.addTab(TabHost.java:213)
at home.android.yahtzee.activities.GameActivity.onCreate(GameActivity.java:36)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at home.android.yahtzee.activities.GamePlayerActivity.onCreate(GamePlayerActivity.java:75)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 20 more
java.lang.NullPointerException
at home.android.yahtzee.activities.GamePlayerActivity.onCreate(GamePlayerActivity.java:75)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
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:328)
at android.widget.TabHost.addTab(TabHost.java:213)
at home.android.yahtzee.activities.GameActivity.onCreate(GameActivity.java:36)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
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);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
Gson gson = new Gson();
Player player = gson.fromJson(getIntent().getExtras().getString("player"), Player.class);
String continueGame = getIntent().getExtras().getString("continue");
intent = new Intent().setClass(this, GamePlayerActivity.class);
intent.putExtra("player", gson.toJson(player));
intent.putExtra("continue", continueGame);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec(player.getName()).setIndicator(player.getName(),
res.getDrawable(player.getDroid()))
.setContent(intent); <-----------Line 36
tabHost.addTab(spec);
}
}
更新 我从对话框
启动TabActivitypublic class WhoIsPlayingDialog extends Dialog {
private Player playerSelected;
private DataApp app;
public WhoIsPlayingDialog(final Context context) {
super(context);
setTitle(R.string.whoIsPlayingSpinnerTitle);
setContentView(R.layout.who_is_playing_dialog);
Spinner playerSpinner = (Spinner) findViewById(R.id.whoIsPlayingSpinner);
app = (DataApp) context.getApplicationContext();
PlayerAdapter playerAdapter = new PlayerAdapter(context, R.layout.player_view, app.db.getAllPlayers());
playerAdapter.setDropDownViewResource(R.layout.player_view);
playerSpinner.setAdapter(playerAdapter);
playerSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
playerSelected = (Player) adapterView.getItemAtPosition(view.getId());
Button continueGameButton = (Button) findViewById(R.id.whoIsPlayingContinueGameButton);
if (!app.db.getGame(playerSelected.getGameId()).isNewGame()) {
continueGameButton.setVisibility(View.VISIBLE);
continueGameButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(context, GameActivity.class);
Gson gson = new Gson();
intent.putExtra("player", gson.toJson(playerSelected));
intent.putExtra("continue", "continue");
context.startActivity(intent);
dismiss();
}
});
}else {
continueGameButton.setVisibility(View.GONE);
}
}
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
(findViewById(R.id.whoIsPlayingDialogOKButton)).setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(context, GameActivity.class);
Gson gson = new Gson();
intent.putExtra("continue", "newGame");
intent.putExtra("player", gson.toJson(playerSelected));
context.startActivity(intent);
dismiss();
}
}
);
}
}
注意到我在nullPointerException
GameActivity aswell
at home.android.yahtzee.activities.GameActivity.onCreate(GameActivity.java:36)
答案 0 :(得分:0)
根据堆栈跟踪,如果您的第75行是堆栈跟踪中的第75行,那么continueGame
null
。
您可能希望在运行时记录值的来源。
Bundle
怎么样?您是否在onSaveInstanceState()
?