在旋转我得到"无法破坏活动的onSaveInstanceState ...&QUOT后无法执行此操作; [英] On Rotation I get "Unable to destroy activity ... Can not perform this action after onSaveInstanceState"
问题描述
感谢您的任何帮助。
我只在配置我的code来处理旋转的开始:我已经实现的onDestroy(),但还没有任何的onSaveInstanceState()或onRetainNonConfigurationInstance(),因为我搞清楚,如果什么都不会造成问题销毁和旋转后重新创建并需要保存的东西。
I am only at the very beginning of configuring my code to handle rotation: I have implemented onDestroy(), but not yet any onSaveInstanceState() or onRetainNonConfigurationInstance() because I am figuring out what will not cause problems if destroyed and recreated after rotation and what needs to be saved.
在我的应用程序使用被换用户pressing按钮,运行一个AsyncTask的服务,和3个片段。
In my application I use a Service that runs an AsyncTask, and 3 fragments that get swapped on user pressing buttons.
我用的是兼容性库管理片段。
I use the compatibility library for managing the fragments.
我怀疑我遇到的错误是相关的片段;
I suspect the error I encounter is related to the fragments;
要具体,我想preFER片段被破坏并重新创建轮换,而这preserving他们
(因为这是我的希望,这将让我的code简单...)
当我旋转模拟器我得到的称号错误。
when I rotate the emulator I get the error in the title.
这是主要活动的code:
This is the code of the main activity:
public class Quotes extends FragmentActivity {
private String url2;
//public String responseBody;
public static ArrayList<Stock> lt;
public static ArrayList<Stock> best;
public static ArrayList<Stock> worst;
public static ArrayList<Stock> fav;// =new ArrayList<Stock>();
public static LocalService mService;
boolean mBound;// = false;
public MyAdapter myAdap;
public Messenger messenger;
public Messenger messenger2;
public Grab g;
public static Handler handler;
public static Handler handler2;
public Intent intent;
public ListView lv;
public dettagliofragment fragment;
public listafragment fragmentlista ;
public static boolean tablet;//=true;
public static WorstBest worstbest;
public static favouritesFragment fragmentfavourites;
public static FragmentActivity fragmentActivity;
public static int stack;
public static ArrayList<Integer> stackArray; // stackArray =new ArrayList<Integer>();
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
stackArray =new ArrayList<Integer>();
fav =new ArrayList<Stock>();
tablet=true;
mBound = false;
fragmentActivity = this;
setContentView(R.layout.splash);
url2=getString(R.string.url2);
g = new Grab(url2);
String s = g.vai();
//Log.e("", s);
Log.e("", "passo oltre l'invocazione al grab");
lt = new MyParser(s).parseResp();
fragmentlista = new listafragment();
fragmentfavourites= new favouritesFragment() ;
worstbest = new WorstBest();
/////////////////primo message handler
handler=new Handler() {
@Override
public void handleMessage(Message msg) {
Message mess=msg;
Log.e("","MESSAGGIO RICEVUTO");
lt = mService.ritira();
Log.e("what?", Integer.toString(mess.what));
if( mess.what==0){
worst=mService.ritiraWorst();
best=mService.ritiraBest();
fav=mService.ritiraFav();
setContentView(R.layout.main);
View view1=(View)findViewById(R.id.view1);
View view2=(View)findViewById(R.id.view2);
if(view2==null) {tablet=false;};
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.view1, fragmentlista);
if(tablet){fragmentTransaction.add(R.id.view2, worstbest);}
fragmentTransaction.commit();
g.fermaGrab();
Log.e("", "fermograb");
};
}
};
messenger = new Messenger(handler);
intent = new Intent(this, LocalService.class);
intent.putExtra("messenger", messenger);
/////////////////secondo message handler
handler2=new Handler() {
@Override
public void handleMessage(Message msg) {
Message mess=msg;
Log.e("","SECONDO MESSAGGIO RICEVUTO");
lt = mService.ritira();
worst=mService.ritiraWorst();
best=mService.ritiraBest();
fav=mService.ritiraFav();
if(lt==null){Log.e("","lt è nullo");};
if(fragmentlista==null){Log.e("","fragmentlista è nullo");};
fragmentlista.prendiLista(lt);
worstbest.prendiListaWorst(worst);
worstbest.prendiListaBest(best);
if(fav.size()>0)fragmentfavourites.prendiLista(fav);
};
};
messenger2 = new Messenger(handler2);
intent.putExtra("messenger2", messenger2);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
public static void layoutSchermo(int conf){
if(conf==1){
FragmentManager fragmentManager = fragmentActivity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
if(!tablet){fragmentTransaction.replace(R.id.view1, fragmentfavourites);}
if(tablet){fragmentTransaction.replace(R.id.view2, fragmentfavourites);}
fragmentTransaction.addToBackStack(null);
stack=fragmentTransaction.commit();
stackArray.add(stack);
Log.e("","stack = "+ Integer.toString(stack));
}
if(conf==2){
FragmentManager fragmentManager = fragmentActivity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
if(!tablet){fragmentTransaction.replace(R.id.view1, worstbest);}
if(tablet){fragmentTransaction.replace(R.id.view2, worstbest);}
fragmentTransaction.addToBackStack(null);
stack= fragmentTransaction.commit();
stackArray.add(stack);
Log.e("","stack = "+ Integer.toString(stack));
}
}
@Override
public void onBackPressed(){
FragmentManager fragmentManager = fragmentActivity.getSupportFragmentManager();
if(stackArray.size()>0)
{for(int i=1;i<stackArray.size();i++){
fragmentManager.popBackStackImmediate(stackArray.get(i), FragmentManager.POP_BACK_STACK_INCLUSIVE);}
}
super.onBackPressed();
}
protected void onStop() {
Log.e("onstop","passo per onstop");
mService.stop();
g.fermaGrab();
super.onStop();
}
protected void onPause() {
Log.e("onpause","passo per onpause");
mService.stop();
g.fermaGrab();
super.onPause();
}
protected void onDestroy() {
Log.e("ondestroy","passo per ondestroy");
mService.stop();
g.fermaGrab();
//handler.removeMessages(333);
stopService(intent);
//g=null;
FragmentManager fragmentManager = fragmentActivity.getSupportFragmentManager();
if(stackArray.size()>0)
{for(int i=1;i<stackArray.size();i++){
fragmentManager.popBackStackImmediate(stackArray.get(i), FragmentManager.POP_BACK_STACK_INCLUSIVE);}
}
super.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) { }
public static ArrayList<Stock> dammi(){
return lt;
}
public void inizia()
{
Log.e("","il serlizio è bound ? "+ mBound);
mService.prendi(lt);
mService.getNumber();
}
private ServiceConnection mConnection = new ServiceConnection() {
//@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
Log.e("", "sono in ServiceConnection");
// We've bound to LocalService, cast the IBinder and get LocalService instance
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
//inizia();
mService.prendi(lt);
mService.getNumber();
}
//@Override
public void onServiceDisconnected(ComponentName arg0) {
mBound = false;
mService = null;
}
};
祂是我的LogCat中:
Hes is my LogCat:
01-06 12:20:27.869: E/AndroidRuntime(2165): FATAL EXCEPTION: main
01-06 12:20:27.869: E/AndroidRuntime(2165): java.lang.RuntimeException: Unable to destroy activity {com.example.quotes/com.example.quotes.Quotes}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
01-06 12:20:27.869: E/AndroidRuntime(2165): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3273)
01-06 12:20:27.869: E/AndroidRuntime(2165): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3291)
01-06 12:20:27.869: E/AndroidRuntime(2165): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3489)
01-06 12:20:27.869: E/AndroidRuntime(2165): at android.app.ActivityThread.access$700(ActivityThread.java:130)
01-06 12:20:27.869: E/AndroidRuntime(2165): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
01-06 12:20:27.869: E/AndroidRuntime(2165): at android.os.Handler.dispatchMessage(Handler.java:99)
01-06 12:20:27.869: E/AndroidRuntime(2165): at android.os.Looper.loop(Looper.java:137)
01-06 12:20:27.869: E/AndroidRuntime(2165): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-06 12:20:27.869: E/AndroidRuntime(2165): at java.lang.reflect.Method.invokeNative(Native Method)
01-06 12:20:27.869: E/AndroidRuntime(2165): at java.lang.reflect.Method.invoke(Method.java:511)
01-06 12:20:27.869: E/AndroidRuntime(2165): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-06 12:20:27.869: E/AndroidRuntime(2165): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-06 12:20:27.869: E/AndroidRuntime(2165): at dalvik.system.NativeStart.main(Native Method)
01-06 12:20:27.869: E/AndroidRuntime(2165): Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
01-06 12:20:27.869: E/AndroidRuntime(2165): at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1314)
01-06 12:20:27.869: E/AndroidRuntime(2165): at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:480)
01-06 12:20:27.869: E/AndroidRuntime(2165): at com.example.quotes.Quotes.onDestroy(Quotes.java:201)
01-06 12:20:27.869: E/AndroidRuntime(2165): at android.app.Activity.performDestroy(Activity.java:5172)
01-06 12:20:27.869: E/AndroidRuntime(2165): at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1109)
01-06 12:20:27.869: E/AndroidRuntime(2165): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3260)
01-06 12:20:27.869: E/AndroidRuntime(2165): ... 12 more
非常感谢您的帮助!
Thank you very much for any help !!!
推荐答案
感谢您的信息。同时,我修复了这个问题。
Thanks for your info. Meanwhile, I fixed this issue.
什么是造成问题是一个需要被解放时,Activty是因为旋转摧毁了一个服务。
What was causing the problem was a Service that needed to be unbound when the Activty was destroyed because of rotation.
至于我能理解当活动被破坏的片段不被破坏。不过,我想AP preciate对此发表任何评论。
As far as I could understand the Fragments are not destroyed when the activity is destroyed. However, I would appreciate any comment on this.
于是,我重新初始化上的的onCreate()
方法的片段被破坏后重新建立的Activty的时候,这是不是造成麻烦的应用程序。
So, I reinitialized the Fragments on the onCreate()
method when the Activty is created again after destruction, and this is not causing trouble to the application.
protected void onDestroy() {
mService.stop();
stopService(new Intent(this, LocalService.class));
unbindService(mConnection);
g.fermaGrab();
stopService(intent);
Log.e("ondestroy","passostopService(intent)");
super.onDestroy();
}
这篇关于在旋转我得到&QUOT;无法破坏活动的onSaveInstanceState ...&QUOT后无法执行此操作;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!