我认为如果我设置应用程序背景,这是由于内存不足。
日志如下:
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'只在xml文件中出现一次,只有当应用程序从后台重新打开时才会出现问题。
我的MainActivity有一个可以在其中替换的片段。我使用getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
当我开始活动时。
如果没有超级方法' savedInstanceState',我没有使用别的东西。
答案 0 :(得分:3)
原因是你的片段被重新添加。 您的代码如下所示:
protected void onCreate(Bundle savedInstanceState){
super(savedInstanceState);
///...
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, fragment).commit();
}
如果通过调用savedInstanceState != null
super(savedInstanceState)
,则会恢复关联的FragmentManager,并且所有提交的片段也会恢复。相反,请检查您的片段是否已添加,如果不是,则添加 :
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();
至于我,我发现添加带有TAG参数的碎片是一种很好的做法。
答案 1 :(得分:1)
我们遇到了内存条件不足的问题(开发设置 - >“不要保留活动”设置)和自定义视图在尝试恢复自定义视图时崩溃(覆盖View.onRestoreInstanceState()
){{3} }。视图没有重复的ID。
片段恢复方式中的错误(包含自定义视图)导致它在恢复时在运行时添加不同的视图,而不是在保存实例状态时添加的视图。 (在我们的示例中,最初添加了自定义视图的读写版本,但恢复该片段添加了该视图的只读版本。)
我怀疑Android会尝试在只读视图上设置读写视图的状态,这会破坏事情。
这是在Android 4.4.4上观察到的,而不是Android 6.0.1
我们得到的例外是:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.package.name/com.package.name.VisitActivity}: java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class com.package.name.view.CustomView$CustomViewState instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/0x465127ed. Make sure other views do not use the same id.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)