Android IllegalArgumentException:如果应用在后台运行了一段时间,则状态类错误 [英] Android IllegalArgumentException: Wrong state class if the app was background for a period of time
问题描述
我认为这是由于我设置了应用背景导致的内存不足。
日志如下所示:
I think it is due to low memory if I set the app background.
The log is like below:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.qingdaonews.bus/com.qingdaonews.activity.RealTime}:
java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.view.View$BaseSavedState instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/myviewpager. Make sure other views do not use the same id.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
at android.app.ActivityThread.access$600(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:5225)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)
看起来由于相同的视图ID。实际上, id'myviewpager'仅ap一次出现在xml文件中,并且仅在从后台重新打开应用程序时才会出现问题。
It looks like due to the same id of a view.In fact ,the id 'myviewpager' only appear once in the xml file and the problem only occur when the app is reopen from background.
而且我的MainActivity有一个片段,可以我使用 getSupportFragmentManager()。beginTransaction()。replace(R.id.content_frame,fragment).commit();
来替换它。活动。
没有'savedInstanceState'的超级方法,我没有使用其他东西。
And my MainActivity has one fragment which can be replaced in it.I use getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
when I start the activity.
Without super method of 'savedInstanceState',I did not use something else.
推荐答案
原因是您的片段被重新添加。
您的代码如下:
The cause is that your Fragment gets re-added. Your code looks like this:
protected void onCreate(Bundle savedInstanceState){
super(savedInstanceState);
///...
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, fragment).commit();
}
如果 savedInstanceState!= null <,则恢复关联的FragmentManager / code>通过调用
super(savedInstanceState)
,所有提交的片段也将恢复。相反,请检查您的片段是否已添加,如果没有,则仅添加:
Associated FragmentManager gets restored if savedInstanceState != null
via a call to super(savedInstanceState)
, and all the commited fragments get restored too. Instead, check if your fragment is already added, and add it only if not:
public class MyActivity extends Activity{
private MyFragment myFragment;//extends Fragment
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//your stuff
FragmentManager fm = getFragmentManager();
myFragment = (MyFragment) fm.findFragmentByTag(MyFragment.TAG);
if(myFragment == null){
myFragment = new MyFragment();
fm.beginTransaction()
.add(R.id.content_frame, myFragment, MyFragment.TAG)
.commit();
}
//other stuff
}
}
在您的 MyFragment
中,TAG是:
public static final String TAG = MyFragment.class.getSimpleName();
And in your MyFragment
, TAG is:
public static final String TAG = MyFragment.class.getSimpleName();
对于我来说,我发现添加带有TAG参数的片段是一种不错的做法。
As for me, I found it is a nice practice to add Fragments with TAG parameter provided.
这篇关于Android IllegalArgumentException:如果应用在后台运行了一段时间,则状态类错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!