Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

出现FC #40

Open
dodola opened this issue May 4, 2013 · 9 comments
Open

出现FC #40

dodola opened this issue May 4, 2013 · 9 comments
Labels

Comments

@dodola
Copy link

dodola commented May 4, 2013

Device:Google Nexus 7
Android Version:4.2.2

重现方法:点进文章后查看,后退,重复几次后可重现,View包里的几个Fragment均有此现象

Exception:

05-04 15:08:37.827: E/AndroidRuntime(10973): FATAL EXCEPTION: main
05-04 15:08:37.827: E/AndroidRuntime(10973): java.lang.RuntimeException: Unable to start activity ComponentInfo{cn.eoe.app/cn.eoe.app.ui.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment cn.eoe.app.view.WikiFragment: make sure class name exists, is public, and has an empty constructor that is public
05-04 15:08:37.827: E/AndroidRuntime(10973):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at android.app.ActivityThread.access$700(ActivityThread.java:141)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at android.os.Looper.loop(Looper.java:137)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at android.app.ActivityThread.main(ActivityThread.java:5041)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at java.lang.reflect.Method.invokeNative(Native Method)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at java.lang.reflect.Method.invoke(Method.java:511)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at dalvik.system.NativeStart.main(Native Method)
05-04 15:08:37.827: E/AndroidRuntime(10973): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment cn.eoe.app.view.WikiFragment: make sure class name exists, is public, and has an empty constructor that is public
05-04 15:08:37.827: E/AndroidRuntime(10973):    at android.support.v4.app.Fragment.instantiate(Fragment.java:405)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1767)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:208)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at cn.eoe.app.ui.base.BaseFragmentActivity.onCreate(BaseFragmentActivity.java:15)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at cn.eoe.app.ui.base.BaseSlidingFragmentActivity.onCreate(BaseSlidingFragmentActivity.java:21)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at cn.eoe.app.ui.MainActivity.onCreate(MainActivity.java:132)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at android.app.Activity.performCreate(Activity.java:5104)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-04 15:08:37.827: E/AndroidRuntime(10973):    ... 12 more
05-04 15:08:37.827: E/AndroidRuntime(10973): Caused by: java.lang.InstantiationException: can't instantiate class cn.eoe.app.view.WikiFragment; no empty constructor
05-04 15:08:37.827: E/AndroidRuntime(10973):    at java.lang.Class.newInstanceImpl(Native Method)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at java.lang.Class.newInstance(Class.java:1319)
05-04 15:08:37.827: E/AndroidRuntime(10973):    at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
05-04 15:08:37.827: E/AndroidRuntime(10973):    ... 21 more
@dodola
Copy link
Author

dodola commented May 4, 2013

Fragment 是需要一个默认构造方法的,在Fragment里添加一个无参的构造方法应该就可以

@rockybean
Copy link
Contributor

一般推荐是在fragment中使用一个newInstance的方法,然后把传入的参数放在fragment本身的argument中,例如

public static YourFragment newInstance(String someArg){
            YourFragment rtn = new YourFragment();
             Bundle args = new Bundle();
             args.putString("argKey",someArg);
            rtn.setArgument(args);
            return rtn;
}

之后可以从fragment的getArgument()中获取存储的方法,这样就可以很好的避免上面的问题了

@IceskYsl
Copy link
Member

IceskYsl commented May 4, 2013

楼上正解~ 大家可以尝试修复下这个问题,然后发个pull request〜

@com360
Copy link
Contributor

com360 commented May 7, 2013

大家快点发pull request,我等着merge呢 ;)

@mjkohoh
Copy link
Contributor

mjkohoh commented Oct 2, 2013

尝试重现bug失败。重现环境在nexus7的4,2,2版本和4.3都跑过了。都没有出现上述所说的Force Close。请问是不是已经修复这个bug了。但是我并没有找到相关提交。

@mjkohoh
Copy link
Contributor

mjkohoh commented Oct 2, 2013

好吧这个issue的确还没有被修复。查了一下文档,出现FC的原因是因为在系统需要重新实例化fragment的时候需要一个空的构造方法。(这点之前的同学已经说过了,我又重述了一遍),而我没有重现bug的原因可能是因为没有遇到系统重新实例化fragment吧。

以下是相关文档的引用

All subclasses of Fragment must include a public empty constructor. The framework will often re-instantiate a fragment class when needed, in particular during state restore, and needs to be able to find this constructor to instantiate it. If the empty constructor is not available, a runtime exception will occur in some cases during state restore.


对于@rockybean 的建议,我想说的是。这会不会对代码改动太大

@mjkohoh
Copy link
Contributor

mjkohoh commented Oct 2, 2013

另外相似的问题也在issue #48 中出现。在issue #48 中解决办法是增加无参构造函数。
我想对于一个相同的问题应该采取相同的解决方法。所以有必要讨论一下到底是采用无参构造函数还是使用newInstance方法。

下面列出同样出同样有这个bug的fragment

  • UserCollectFragment
  • UserCollectListFragment
  • UserIntroFragment
  • UserLogOutFragment
  • WikiFragment

@mjkohoh
Copy link
Contributor

mjkohoh commented Oct 11, 2013

@IceskYsl @com360 你觉得采取哪种方法修复这个bug最好

@mjkohoh
Copy link
Contributor

mjkohoh commented Oct 15, 2013

没有人愿意讨论一下的吗?
要不管理员做个决定?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants