Exception by closing app in onSavedInstanceState

2,340 views
Skip to first unread message

Felix Wackernagel

unread,
Apr 19, 2012, 7:28:57 AM4/19/12
to ActionBarSherlock
Hey Guys,
i update my ABS to Version 4 and refactore my App. It works
everythings fine but if i click on my home button i get a
NullPointerException at super.onSaveInstanceState(Bundle). Has anybody
the same issue? I past my code at the bottom.

Greedings Felix

package com.android.droidfridge;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;

public class DroidFridge2 extends SherlockFragmentActivity
{
private ViewPager viewPager;
private TabsAdapter tabsAdapter;

@Override
protected void onCreate( Bundle savedInstanceState )
{
Log.i( DroidFridge2.class.getName(), "onCreate" );

super.onCreate( savedInstanceState );
setContentView( R.layout.droid_fridge_pager );


getSupportActionBar().setNavigationMode( ActionBar.NAVIGATION_MODE_TABS );

// label each tab
ActionBar.Tab tab1 =
getSupportActionBar().newTab().setText( "Fridge" );
ActionBar.Tab tab2 =
getSupportActionBar().newTab().setText( "Basket" );
ActionBar.Tab tab3 =
getSupportActionBar().newTab().setText( "Recipes" );

viewPager = (ViewPager) findViewById( R.id.pager );

// add activities to tabs
tabsAdapter = new TabsAdapter( this, getSupportActionBar(),
viewPager );
tabsAdapter.addTab( tab1, FridgeFragment.class );
tabsAdapter.addTab( tab2, FridgeFragment.class );
tabsAdapter.addTab( tab3, FridgeFragment.class );

if( savedInstanceState != null )
{

getSupportActionBar().setSelectedNavigationItem( savedInstanceState.getInt( "index" ) );
}
}

@Override
protected void onSaveInstanceState( Bundle outState )
{
Log.i( DroidFridge2.class.getName(), "onSaveInstanceState" );

super.onSaveInstanceState( outState );
outState.putInt( "index",
getSupportActionBar().getSelectedNavigationIndex() );
}

public static class TabsAdapter extends FragmentPagerAdapter
implements ViewPager.OnPageChangeListener, ActionBar.TabListener
{
private final Context context;
private final ActionBar actionBar;
private final ViewPager viewPager;
private final ArrayList<String> tabs = new ArrayList<String>();

public TabsAdapter( FragmentActivity activity, ActionBar actionBar,
ViewPager pager )
{
super( activity.getSupportFragmentManager() );
this.context = activity;
this.actionBar = actionBar;
this.viewPager = pager;
this.viewPager.setAdapter( this );
this.viewPager.setOnPageChangeListener( this );
}

public void addTab( ActionBar.Tab tab, Class<?> clss )
{
tabs.add( clss.getName() );
actionBar.addTab( tab.setTabListener( this ) );
notifyDataSetChanged();
}

@Override
public int getCount()
{
return tabs.size();
}

@Override
public Fragment getItem( int position )
{
return Fragment.instantiate( context, tabs.get( position ) );
}

public void onPageScrolled( int position, float positionOffset, int
positionOffsetPixels )
{
}

public void onPageSelected( int position )
{
actionBar.setSelectedNavigationItem( position );
}

public void onPageScrollStateChanged( int state )
{
}

public void onTabSelected( com.actionbarsherlock.app.ActionBar.Tab
tab, FragmentTransaction ft )
{
viewPager.setCurrentItem( tab.getPosition() );
}

public void onTabUnselected( com.actionbarsherlock.app.ActionBar.Tab
tab, FragmentTransaction ft )
{

}

public void onTabReselected( com.actionbarsherlock.app.ActionBar.Tab
tab, FragmentTransaction ft )
{

}
}
}

Mark Murphy

unread,
Apr 19, 2012, 7:42:53 AM4/19/12
to actionba...@googlegroups.com
It's impossible for there to be a NullPointerException directly on a
super.onSaveInstanceState() call -- super cannot be null. The problem
must be somewhere inside of what super.onSaveInstanceState() does.

Could you post your actual stack trace?

--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Warescription: Three Android Books, Plus Updates, One Low Price!

Felix Wackernagel

unread,
Apr 19, 2012, 7:58:41 AM4/19/12
to actionba...@googlegroups.com
I copy only the Exception:

04-19 11:56:58.411: E/AndroidRuntime(280): java.lang.RuntimeException: Unable to pause activity {com.android.droidfridge/com.android.droidfridge.DroidFridge2}: java.lang.NullPointerException
04-19 11:56:58.411: E/AndroidRuntime(280):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3348)
04-19 11:56:58.411: E/AndroidRuntime(280):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3305)
04-19 11:56:58.411: E/AndroidRuntime(280):     at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3288)
04-19 11:56:58.411: E/AndroidRuntime(280):     at android.app.ActivityThread.access$2500(ActivityThread.java:125)
04-19 11:56:58.411: E/AndroidRuntime(280):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2040)
04-19 11:56:58.411: E/AndroidRuntime(280):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 11:56:58.411: E/AndroidRuntime(280):     at android.os.Looper.loop(Looper.java:123)
04-19 11:56:58.411: E/AndroidRuntime(280):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-19 11:56:58.411: E/AndroidRuntime(280):     at java.lang.reflect.Method.invokeNative(Native Method)
04-19 11:56:58.411: E/AndroidRuntime(280):     at java.lang.reflect.Method.invoke(Method.java:521)
04-19 11:56:58.411: E/AndroidRuntime(280):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-19 11:56:58.411: E/AndroidRuntime(280):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-19 11:56:58.411: E/AndroidRuntime(280):     at dalvik.system.NativeStart.main(Native Method)
04-19 11:56:58.411: E/AndroidRuntime(280): Caused by: java.lang.NullPointerException
04-19 11:56:58.411: E/AndroidRuntime(280):     at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576)
04-19 11:56:58.411: E/AndroidRuntime(280):     at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617)
04-19 11:56:58.411: E/AndroidRuntime(280):     at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481)
04-19 11:56:58.411: E/AndroidRuntime(280):     at com.android.droidfridge.DroidFridge2.onSaveInstanceState(DroidFridge2.java:56)
04-19 11:56:58.411: E/AndroidRuntime(280):     at android.app.Activity.performSaveInstanceState(Activity.java:1036)
04-19 11:56:58.411: E/AndroidRuntime(280):     at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1180)
04-19 11:56:58.411: E/AndroidRuntime(280):     at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3330)
04-19 11:56:58.411: E/AndroidRuntime(280):     ... 12 more

Mark Murphy

unread,
Apr 19, 2012, 8:09:11 AM4/19/12
to actionba...@googlegroups.com
I think you are tripping over a bug in the Android Support package. See:

http://code.google.com/p/android/issues/detail?id=19917
http://code.google.com/p/android/issues/detail?id=19211

--

Felix Wackernagel

unread,
Apr 19, 2012, 8:13:19 AM4/19/12
to actionba...@googlegroups.com
Now i change my fragment with was in all three tabs and i works without an error.

package com.android.droidfridge;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.ListFragment;

import com.actionbarsherlock.app.SherlockListFragment;

public class FridgeFragment extends SherlockListFragment
{
    @Override
    public void onCreate( Bundle savedInstanceState )
    {
        super.onCreate( savedInstanceState );

        FragmentManager fm = getFragmentManager();
        if( fm.findFragmentById( android.R.id.content ) == null )
        {
            Fridge fridge = new Fridge();
            fm.beginTransaction().add( android.R.id.content, fridge ).commit();
        }
    }

    public static class Fridge extends ListFragment
    {
        private Context context = null;

        public Fridge()
        {
            setRetainInstance( true );
        }

        @Override
        public void onActivityCreated( Bundle savedInstanceState )
        {
            super.onActivityCreated( savedInstanceState );
            this.context = getActivity();

            setEmptyText( "empty" );
            setListShown( true );
        }
    }
}

Jake Wharton

unread,
Apr 19, 2012, 12:18:18 PM4/19/12
to actionba...@googlegroups.com
My patch for this was accepted into AOSP yesterday. Should be in the next support library revision.

Felix Wackernagel

unread,
Apr 20, 2012, 1:15:09 AM4/20/12
to actionba...@googlegroups.com
Thanks for the fast help. It is really interesting to see the workflow of bug tracking and fixing on android.

Best regards.

Felix

Barney Hsiao

unread,
Jul 6, 2012, 9:48:12 PM7/6/12
to actionba...@googlegroups.com
I am a novice android developer and having the same problem as Felix. I am unclear what I have to do to in order to get the patch.

Thanks,
Barney


On Thursday, April 19, 2012 9:18:18 AM UTC-7, Jake Wharton wrote:
My patch for this was accepted into AOSP yesterday. Should be in the next support library revision.

Jake Wharton

unread,
Jul 6, 2012, 9:50:18 PM7/6/12
to actionba...@googlegroups.com

Replace the android-support-v4.jar in the libs folder with the latest one from the SDK Manager.

Reply all
Reply to author
Forward
0 new messages