我有一个应用程序包含一个从本地HTML文件加载3个网页的ViewPager,它工作正常,但当我尝试在不同的活动中启动一个意图时,它会崩溃并显示以下消息:
05-15 11:33:12.533 7702-7702/com.example.android.horizontalpaging E/FragmentManager﹕ Fragment Webpage{22947acd} is not currently in the FragmentManager
05-15 11:33:12.533 7702-7702/com.example.android.horizontalpaging E/FragmentManager﹕ Activity state:
05-15 11:33:12.534 7702-7702/com.example.android.horizontalpaging D/FragmentManager﹕ Active Fragments in 3e30ef82:
05-15 11:33:12.534 7702-7702/com.example.android.horizontalpaging D/FragmentManager﹕ #0: null
05-15 11:33:12.534 7702-7702/com.example.android.horizontalpaging D/FragmentManager﹕ #1: null
05-15 11:33:12.534 7702-7702/com.example.android.horizontalpaging D/FragmentManager﹕ #2: null
05-15 11:33:12.534 7702-7702/com.example.android.horizontalpaging D/FragmentManager﹕ Added Fragments:
05-15 11:33:12.534 7702-7702/com.example.android.horizontalpaging D/FragmentManager﹕ #0: Webpage{22947acd}
05-15 11:33:12.534 7702-7702/com.example.android.horizontalpaging D/FragmentManager﹕ #1: Webpage{245bc693}
05-15 11:33:12.534 7702-7702/com.example.android.horizontalpaging D/FragmentManager﹕ #2: Webpage{1a4686d0}
05-15 11:33:12.534 7702-7702/com.example.android.horizontalpaging D/FragmentManager﹕ FragmentManager misc state:
05-15 11:33:12.534 7702-7702/com.example.android.horizontalpaging D/FragmentManager﹕ mActivity=null
05-15 11:33:12.534 7702-7702/com.example.android.horizontalpaging D/FragmentManager﹕ mContainer=null
05-15 11:33:12.535 7702-7702/com.example.android.horizontalpaging D/FragmentManager﹕ mCurState=0 mStateSaved=true mDestroyed=true
05-15 11:33:12.535 7702-7702/com.example.android.horizontalpaging D/FragmentManager﹕ mAvailIndices: [0, 1, 2]
05-15 11:33:12.535 7702-7702/com.example.android.horizontalpaging D/AndroidRuntime﹕ Shutting down VM
05-15 10:52:38.309 2589-2589/com.example.android.horizontalpaging E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.android.horizontalpaging, PID: 2589
java.lang.IllegalStateException: Fragment Webpage{3e30ef82} is not currently in the FragmentManager
at android.support.v4.app.FragmentManagerImpl.putFragment(FragmentManager.java:571)
at android.support.v4.app.FragmentStatePagerAdapter.saveState(FragmentStatePagerAdapter.java:188)
at android.support.v4.view.ViewPager.onSaveInstanceState(ViewPager.java:1270)
at android.view.View.dispatchSaveInstanceState(View.java:13669)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2859)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2865)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2865)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2865)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2865)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2865)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2865)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2865)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2865)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2865)
at android.view.ViewGroup.dispatchSaveInstanceState(ViewGroup.java:2865)
at android.view.View.saveHierarchyState(View.java:13652)
at com.android.internal.policy.impl.PhoneWindow.saveHierarchyState(PhoneWindow.java:1967)
at android.app.Activity.onSaveInstanceState(Activity.java:1366)
at android.app.ActivityGroup.onSaveInstanceState(ActivityGroup.java:66)
at android.app.TabActivity.onSaveInstanceState(TabActivity.java:112)
at android.app.Activity.performSaveInstanceState(Activity.java:1298)
at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1288)
at android.app.ActivityThread.callCallActivityOnSaveInstanceState(ActivityThread.java:3958)
at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3369)
at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3425)
at android.app.ActivityThread.access$1100(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
这是包含ViewPager的活动:
public class Azkar extends ActionBarActivity {
public static SectionsPagerAdapter mSectionsPagerAdapter;
public static ViewPager mViewPager;
String[] tabsTitle = {"أذكار الصباح","أذكار المساء","أذكار الصلاة"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.azkar);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(3-1);
mViewPager.setAdapter(mSectionsPagerAdapter);
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setTextSize(40);
tabs.setViewPager(mViewPager);
}
public class SectionsPagerAdapter extends FragmentStatePagerAdapter{
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
Bundle args = new Bundle();
switch (position){
case 0:
fragment = new Webpage();
args.putInt(Webpage.ARG_SECTION_NUMBER, position);
fragment.setArguments(args);
break;
case 1:
fragment = new Webpage();
args.putInt(Webpage.ARG_SECTION_NUMBER, position);
fragment.setArguments(args);
break;
default:
fragment = new Webpage();
args.putInt(Webpage.ARG_SECTION_NUMBER, position);
fragment.setArguments(args);
break;
}
return fragment;
}
public int getItemPosition(Object object) {
if(object==null)
return POSITION_UNCHANGED;
return POSITION_NONE;
}
@Override
public CharSequence getPageTitle(int position) {
return tabsTitle[position];
}
@Override
public int getCount() {
return 3;
}
}
}
这就是网页片段:
public class Webpage extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
private WebView webView;
public Webpage() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.webpage, container, false);
TextView pageNumber = (TextView) rootView.findViewById(R.id.pageNumber);
pageNumber.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
webView = (WebView) rootView.findViewById(R.id.webView);
int number = getArguments().getInt(ARG_SECTION_NUMBER);
String file = null;
switch (number){
case 0:
file = "morning.html";
break;
case 1:
file = "evning.html";
break;
case 2:
file = "prayer.html";
break;
default:
break;
}
webView.loadUrl("file:///android_asset/"+file);
return rootView;
}
@Override
public void onLowMemory() {
super.onLowMemory();
}
}
希望有人能找到解决方法。提前谢谢