I apologize the VERY long question but have added extra code.
I have a problem. I have just published an app that i have tested on two phones and emulator. On these devices everything works great.
But the problem is that I get some exceptions from people using the app.
I am using
intent.putExtra("continue", "newGame");
to tell if the user starts a new game or continues last saved game.
Since I am using an extension of a TabActivity
I send the intent to my TabActivity
which get this with
String continueGame = getIntent().getExtras().getString("continue");
and afterwards adding the extra again
intent.putExtra("continue", continueGame);
when I reach the activity I try
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();
}
But here I get a NullPointerException
. Here is the 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)
Hope you can help as you as usual can:)
EDIT
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);
}
}
UPDATE I start the TabActivity from a dialog
public 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();
}
}
);
}
}
Just noticed that I get a nullPointerException
in the GameActivity aswell
at home.android.yahtzee.activities.GameActivity.onCreate(GameActivity.java:36)