片段在通知viewpager时当前不在FragmentManager中

时间:2014-12-02 15:49:42

标签: android android-fragments android-viewpager android-fragmentactivity

我正面临一个问题,它会通知viewPager关于新数据集。 我有两个相同活动的viewpager,如果有任何新数据到达我想通知

public class DetailActivity extends FragmentActivity
{
    private ViewPager dishPager,restaurantPager;
    public DishPager dishPagerAdapter;
    public RestaurantPager restaurantPagerAdapter;
    public int dishPageNumber;
    ModelManager objModelManager;
    PersistentStoreHelper persistentStoreHelper;
    DishDetailPageModel objDetailPageModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
            new IntentFilter("Broadcasting_from_DetailModel"));

        dishPager = (ViewPager) findViewById(R.id.pager);
        restaurantPager = (ViewPager) findViewById(R.id.pagerRest);
        dishPagerAdapter = new DishPager(getSupportFragmentManager());
        restaurantPagerAdapter = new RestaurantPager(getSupportFragmentManager());
        dishPager.setAdapter(dishPagerAdapter);
        restaurantPager.setAdapter(restaurantPagerAdapter);
       }
       private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {

        restaurantPagerAdapter.notifyDataSetChanged();
        dishPagerAdapter.notifyDataSetChanged();

    }
private class DishPager extends FragmentStatePagerAdapter {
   public DishPager(FragmentManager fm) {
        super(fm);
        dishPageNumber = //connected to model
    }
    @Override
    public Fragment getItem(int position) {
        String img = null;
        //data is a bundle loaded from the model
        return DishPagerFragment.create(data);
    }}
    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
    @Override
    public int getCount() {
        return dishPageNumber;
    }
   }
private class RestaurantPager extends FragmentStatePagerAdapter {
   public DishPager(FragmentManager fm) {
        super(fm);

    }
    @Override
    public Fragment getItem(int position) {
        String img = null;
        //data is a bundle loaded from the model
        return RestaurantPagerFragment.create(data);
    }}
    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
    @Override
    public int getCount() {
        return 1;
    }
   }
   }

我在每个listItemClick

上从我的MainActivity调用此DetailActivity

问题是我第一次加载DetailActivity时没有任何问题。 但是第二次加载DetailActivity时会在通知viewpager时创建问题。

我收到以下错误。

12-02 20:53:59.864: E/AndroidRuntime(14609): java.lang.IllegalStateException: Fragment 

RestaurantPagerFragment{1e750466} is not currently in the FragmentManager
12-02 20:53:59.864: E/AndroidRuntime(14609):    at android.support.v4.app.FragmentManagerImpl.saveFragmentInstanceState(FragmentManager.java:599)
12-02 20:53:59.864: E/AndroidRuntime(14609):    at android.support.v4.app.FragmentStatePagerAdapter.destroyItem(FragmentStatePagerAdapter.java:136)
12-02 20:53:59.864: E/AndroidRuntime(14609):    at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:874)
12-02 20:53:59.864: E/AndroidRuntime(14609):    at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2824)
12-02 20:53:59.864: E/AndroidRuntime(14609):    at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
12-02 20:53:59.864: E/AndroidRuntime(14609):    at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
12-02 20:53:59.864: E/AndroidRuntime(14609):    at com.hibrise.androidtomato.view.DetailActivity$1.onReceive(DetailActivity.java:130)
12-02 20:53:59.864: E/AndroidRuntime(14609):    at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)
12-02 20:53:59.864: E/AndroidRuntime(14609):    at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
12-02 20:53:59.864: E/AndroidRuntime(14609):    at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)
12-02 20:53:59.864: E/AndroidRuntime(14609):    at android.os.Handler.dispatchMessage(Handler.java:102)
12-02 20:53:59.864: E/AndroidRuntime(14609):    at android.os.Looper.loop(Looper.java:135)
12-02 20:53:59.864: E/AndroidRuntime(14609):    at android.app.ActivityThread.main(ActivityThread.java:5221)
12-02 20:53:59.864: E/AndroidRuntime(14609):    at java.lang.reflect.Method.invoke(Native Method)
12-02 20:53:59.864: E/AndroidRuntime(14609):    at java.lang.reflect.Method.invoke(Method.java:372)
12-02 20:53:59.864: E/AndroidRuntime(14609):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-02 20:53:59.864: E/AndroidRuntime(14609):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

请解决问题。 如果您在理解问题时遇到任何问题,请发表评论。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

可能会有所帮助

    public int getItemPosition(Object object) {
        if (((Fragment) object).isAdded()) {
            return PagerAdapter.POSITION_NONE;
        } else {
            return super.getItemPosition(object);
        }
    }