随机碎片问题
在我的 oncreate 方法中,我遇到了这个问题
// Create new fragment and transaction
myFragment = new MyFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.downPart, myFragment);
transaction.commit();
,问题是有时它会抛出 forseclose 消息并出现此错误
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.pakagename.pak1.MyFragmentsActivity: make sure class name exists, is public, and has an empty constructor that is public
,但有时它是随机的,有时我启动我的应用程序并且运行良好,例如,它可以连续运行 10 次,运行良好,然后当我一开始就崩溃了... 之后它再次工作,
我不明白为什么有时它会崩溃,如果有其他一些应用程序在后台运行并且以某种方式减慢我的CPU或占用大量内存,是否有可能崩溃,我真的不明白这一点随机行为。
in my oncreate method I have this
// Create new fragment and transaction
myFragment = new MyFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.downPart, myFragment);
transaction.commit();
The problem is somethimes it trows forseclose message with this error
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.pakagename.pak1.MyFragmentsActivity: make sure class name exists, is public, and has an empty constructor that is public
but it is just sometimes it is random sometimes I start my app and works good, and for example it works like 10 times in row works fine and then when I start it crashes...
after that again it works
I can't get it why sometimes it just crashes, is it possible to crash if there are some other apps run in background and somehow they slow my cpu or take a lot of memory, I really do not understand this random behavior.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将
Fragment
添加到FragmentManager
,Android 将保存它们的状态,并在应用程序进程被终止以回收内存等情况下重新创建它们。“最小化”您的应用程序 (按 home)然后打开许多其他应用程序会导致此问题。当您返回时,如果如错误所述,片段类名存在、是公共的并且具有空的公共构造函数,则它只能重新创建片段。您还没有向我们展示
MyFragment
的代码,但我猜测这些条件之一不成立。特别是,MyFragment 很可能是您的活动的(非静态)内部类。内部类只能在外部类的实例中实例化,但 Android 在重新创建片段时会从另一个上下文执行此操作。这就是为什么将 Fragment 作为内部类会出现问题。要修复此问题,请确保 MyFragment 是静态内部类或其自己的类,并且构造函数是公共的(或不存在;它将使用默认构造函数)。静态内部类可以在没有外部类实例的情况下存在(并且无法访问任何实例变量)。
您可以通过在启动应用程序后按 Home 键(将调用
onSaveInstanceState
),使用 DDMS 终止进程,然后重新启动您的应用程序来轻松测试这一点。或者,尝试原来的步骤,在中间打开许多其他应用程序。If you add a
Fragment
to theFragmentManager
, Android will save their state and recreate them if the app's process is ever killed to reclaim memory, etc. 'Minimising' your app (pressing home) and then opening many other apps will cause this. When you return, it can only recreate your fragments if, as the error says, the fragment class name exists, is public, and has an empty public constructor.You haven't shown us the code for
MyFragment
, but I am guessing one of these conditions is not true. In particular, it is likely that MyFragment is a (non-static) inner class of your activity. An inner class can only be instantiated within an instance of the outer class, but Android does this from another context when it recreates your fragment. This is why it is a problem to have a Fragment as an inner class.To fix it, make sure MyFragment is either a static inner class, or its own class, and that the constructor is public (or not present; it will use the default one). A static inner class can exist without an instance of its outer class (and does not have access to any instance variables).
You can test this quite easily by pressing home after launching the app (
onSaveInstanceState
will be called), killing the process with DDMS, then relaunching your app. Alternatively, try your original steps whereby you open many other apps in-between.