0

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)
Bastaix
  • 835
  • 3
  • 12
  • 23
  • can you put whole activity code? because this code is not sufficient for us to look at the problem. – user370305 Nov 01 '11 at 17:37
  • Are you sure the extra "continue" is *always* actually present in either the intent or the bundle? – JimmyB Nov 01 '11 at 18:26
  • Why are you using `setContent(intent)`? That technique is officially deprecated and was never a particularly good idea in the first place. Just use `Views` as the contents of your tabs, and you can bypass all your problems with extras. – CommonsWare Nov 01 '11 at 18:27
  • @CommonsWare Here I have a TabActivity, which handles the tabs and a Activity which does the rest. Do you mean that everything has to be done in the same Activity? – Bastaix Nov 01 '11 at 18:48
  • in the tutorial http://developer.android.com/resources/tutorials/views/hello-tabwidget.html They also use the setContent(intent)?? – Bastaix Nov 01 '11 at 20:04
  • Even if the intent "extras" itself is non-null, that doesn't mean a particular element is or isn't present within. So "continueGame = extras.getString("continue");" may well still result in null. And, your "assert" statement later on won't do anything unless you've specifically enabled Dalvik support for assert (http://stackoverflow.com/questions/2364910/can-i-use-assert-on-android-devices). So, there may well be instances where continueGame is null (assuming you haven't turned on assert support). Basically, as Hanno stated, it seems in some cases continue is just not getting passed in. – Charlie Collins Nov 01 '11 at 20:14
  • @CharlieCollins I have added the code for the Dialog where the intent get startet. – Bastaix Nov 01 '11 at 20:48
  • @HannoBinder If have just tried to use the debugger. No matter which way I get to the Activity the extra "continue" is _always_ present in the intent – Bastaix Nov 01 '11 at 21:51

1 Answers1

0

According to the stack trace, and if your line 75 is the one from the stack trace, continueGame is null at that point.
You may want to log where the value comes from at runtime.

What about the Bundle? Do you write your data to the bundle in onSaveInstanceState()?

JimmyB
  • 12,101
  • 2
  • 28
  • 44
  • I have tried to do a debug and followed the extra "continue". I also tells me that the `continueGame` is either `newGame` or `continue` depending on what I do. So it is not `null`. But as I wrote in the question, the app works on my phone, another phone and the emulator. That is also why I think it is hard to find the error as I can not generate the exception. – Bastaix Nov 02 '11 at 12:24