Why Bundle object is always null on onCreate()?
Asked Answered
D

5

11

I am trying get into Android programming, and for I am taken some examples from a book. In on of these example is requested to put the following code:

public class ExemploCicloVida extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        Log.i(TAG, getClassName() + " onCreate() called on: " + icicle);

        TextView t = new TextView(this);
        t.setText("Exemplo de ciclo de vida de uma Activity.\nConsulte os logs no LogCat");
        setContentView(t);
    }
}

I wonder why Bundle object is always null on this case.

Darden answered 13/11, 2011 at 3:4 Comment(0)
T
17

The bundle will be null if there is no previously-saved state.

This is mentioned in the Activity API documentation.

Tenement answered 13/11, 2011 at 3:5 Comment(0)
E
10

In my case, the reason was that the specific activity did not have a theme declared in the manifest file.

To fix this, open AndroidManifest.xml, click Application, select the crashing activity in Application Nodes and add the theme in the Theme field of Attributes. In my case, it was

@style/Theme.AppCompat.Light.DarkActionBar

but you could copy the theme from one of your other activities.

P.S.: I know this is an answer to an old question, but I've stumbled upon it while searching for a fix and didn't find a working solution so this might help others.

Englert answered 5/2, 2014 at 21:3 Comment(2)
thank you very much for your input into this!!! You saved me a lot of headaches...I had changed the theme in my previous activity but not in the activity where I was getting the null pointer! I would never have dreamed that this was the issue! Again...cead mile failte!Wore
You're very welcome! I wasted a lot of hours to figure it out so I'm glad to hear it helped someone not go through all this!Englert
S
2

Run this code and rotate the screen by pressing Ctrl+F11. The bundle will not be null.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    if (savedInstanceState != null) {
        Toast.makeText(this, savedInstanceState.getString("s"),
                Toast.LENGTH_LONG).show();
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putString("s", "hello");
}

onSaveInstanceState(Bundle) will be called. Then, the activity object is created and onCreated(Bundle) will be called with non-null Bundle savedInstanceState.

Spouse answered 13/11, 2011 at 3:28 Comment(1)
Thanks wannik. API gave me the clue and also yours, i just typed Ctrl+F11 on my very own code, and icicle was no longer null.Urumchi
H
0

I guess you would like to read the parameters getting in to your activity. Use this function:

protected String getStringExtra(Bundle savedInstanceState, String id) {
String l;
l = (savedInstanceState == null) ? null : (String) savedInstanceState
            .getSerializable(id);
if (l == null) {
    Bundle extras = getIntent().getExtras();
    l = extras != null ? extras.getString(id) : null;
}
return l;
}
Howlend answered 14/5, 2014 at 4:58 Comment(0)
C
0

First, Dave is right -- if there's no previously saved state -- like the app was just created/started, there will be no state. However, since your comments look like you're working through lifecycle examples, I also offer the below:

When getting stuff out of a bundle saved in onSaveInstanceState you are overwriting the contents of that bundle in this bit of code by calling the super of onCreate before looking for the contents of the bundle. Switch your log with your super call and it should work fine:

 @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        Log.i(TAG, getClassName() + " onCreate() called on: " + icicle);

At some point in that long line of overrides, the bundle is set to null

Circumstance answered 12/8, 2020 at 18:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.