在Play-store上发布应用程序,几天后在“Crashes& ANRs”中出现这些错误

时间:2014-01-31 09:34:53

标签: android android-fragments

我自己从未见过这个错误,也不知道如何重现它。 我可以就这个问题提出一些建议吗?

java.lang.RuntimeException: Unable to start activity ComponentInfo{net.domain.app/net.domain.app.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment net.domain.app.fragments.main.MainTabFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5419)
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:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment net.domain.app.fragments.main.MainTabFragment: make sure class name exists, is public, and has an empty constructor that is public
at android.support.v4.app.Fragment.instantiate(Fragment.java:413)
at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1790)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:213)
at net.domain.app.MainActivity.onCreate(MainActivity.java:104)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
... 11 more
Caused by: java.lang.InstantiationException: can't instantiate class net.domain.app.fragments.main.MainTabFragment; no empty constructor
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1130)
at android.support.v4.app.Fragment.instantiate(Fragment.java:402)
... 18 more

与以下内容有关:

net.domain.app.fragments.main.MainTabFragment: make sure class name exists, is public, and has an empty constructor that is public

此类在我尝试的任何设备上永远不会失败,Class存在,是公共的并且具有公共构造函数

主要活动中的第104行是 super.onCreate(savedInstanceState);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

这是MainTabFragment

2 个答案:

答案 0 :(得分:1)

如果我没错,你的LogCat会说“没有空的构造函数”。

基本上,当子类成为一个片段时,你需要提供空构造函数,如果你想要创建不同的那些。

正如documentation所说:

  

Fragment的所有子类都必须包含一个公共空构造函数。   框架通常会在需要时重新实例化一个片段类,   特别是在状态恢复期间,需要能够找到这个   构造函数来实例化它。如果没有空构造函数   可用时,在某些情况下会在状态期间发生运行时异常   还原

答案 1 :(得分:1)

您的Fragment没有空构造函数,这是导致错误的原因:

  

有一个空的构造函数是公共的

您可以通过更改构造函数来解决您的问题:

public MainTabFragment(LatteApplication _latte) {
    latte = _latte;
}

到此:

public MainTabFragment() {}

如果你需要在创建它时将对象传递给Fragment,请使用一组参数(你的自定义类LatteApplication需要实现Parcelable或Serializable,这里我给你一个Parcelable的例子):

LatteApplication _latte;
//initialize that object

// Then do this when creating your Fragment
Bundle args = new Bundle();
args.putParcelable("latte_application", _latte);
MainTabFragment fragment = new MainTabFragment();
fragment.setArguments(args);
//then add your Fragment to your layout.

然后在Fragment方法中,例如在onActivityCreated中,检索如下的参数:

Bundle args = getArguments();
if (args != null){
    LatteApplication _latte = args.getParcelable("latte_application");
}

您可以查看documentation以查看可以通过捆绑包传递的内容...