Felix Wackernagel
unread,Apr 19, 2012, 7:28:57 AM4/19/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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 )
{
}
}
}