3
我有一個非常溫情的android應用程序,我正在努力。Android的片段旋轉導致崩潰
我有一個導航抽屜,根據用戶選擇的內容交換片段。
然而,當屏幕被旋轉的應用程序崩潰,此錯誤:
java.lang.RuntimeException: Unable to destroy activity {MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
我的片段的所有擴展的基類「BaseFragment」:
public class BaseFragment extends Fragment{
protected FragmentManager fragmentManager;
public void setFragmentManager(FragmentManager fragmentManager){
this.fragmentManager=fragmentManager;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDestroyView() {
super.onDestroyView();
Fragment f = (Fragment) fragmentManager
.findFragmentById(R.id.container);
if (f != null)
fragmentManager.beginTransaction().remove(f).commit();
}
}
我的默認片段「NewsFeedFragment」長相像這樣:
public class NewsFeedFragment extends BaseFragment{
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static NewsFeedFragment newInstance(int sectionNumber) {
NewsFeedFragment fragment = new NewsFeedFragment();
return fragment;
}
public NewsFeedFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setFragmentManager(getFragmentManager());
View rootView = inflater.inflate(R.layout.fragment_news_feed, container, false);;
return rootView;
}
}
我的主要活動如下所示:
public class MainActivity extends BaseActivity {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {@link #restoreActionBar()}.
*/
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.titlebar);
ActionBar actionBar=getActionBar();
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.news_feed_activity);
break;
case 2:
mTitle = getString(R.string.friends_activity);
break;
case 3:
mTitle = getString(R.string.events_activity);
break;
case 4:
mTitle = getString(R.string.locations_activity);
break;
case 5:
mTitle = getString(R.string.settings_activity);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle("");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.news_feed, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//if (id == R.id.action_settings) {
// return true;
//}
return super.onOptionsItemSelected(item);
}
}
正如我所說的這個錯誤只發生在旋轉。
以下是完整的錯誤輸出流:
EXCEPTION: main
Process: APP ID, PID: 8399
java.lang.RuntimeException: Unable to destroy activity {MainActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3497)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3515)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3715)
at android.app.ActivityThread.access$900(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1323)
at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1341)
at android.app.BackStackRecord.commitInternal(BackStackRecord.java:597)
at android.app.BackStackRecord.commit(BackStackRecord.java:575)
at uk.co.stephen_robinson.uni.lufelf.NewsFeedFragment.onDestroyView(NewsFeedFragment.java:43)
at android.app.Fragment.performDestroyView(Fragment.java:1898)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:954)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1044)
at android.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1881)
at android.app.Activity.performDestroy(Activity.java:5402)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1117)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3484)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3515)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3715)
at android.app.ActivityThread.access$900(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
我真的很感激一些這方面的幫助。
在此先感謝,
詹姆斯。
非常感謝Pulkit - 快速響應呢!將它標記爲接受時,我可以:) – user3238439
這是危險的,idr時,但這可以拋出IllegalStateException。 – Alexander
這就是建議的伴侶 –