2
我面臨一個問題,即通知viewPager新數據集。 我有兩個viewpager在同一個活動,我想通知,如果有任何新的數據到達片段當前不在FragmentManager中,同時通知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)
請解決問題。 如果您在理解問題時有任何問題,請寫評論。 在此先感謝。