Android的碎片重新实例化错误 [英] Android Fragment Re-Instantiation Error

查看:151
本文介绍了Android的碎片重新实例化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我有我的转换支离破碎pretty的做得很好,直到我旋转屏幕。我得到的错误有以下几种:

RuntimeException的:无法启动的活动ComponentInfo {com.ghcssoftware.gedstar / com.ghcssoftware.gedstar.GedStar}:android.support.v4.app.Fragment $ InstantiationException:无法实例片段com.ghcssoftware.gedstar.PersonTab $ PersonTabFrag:确保类名称存在,是公开的,并且有一个空的构造是公共

类的问题确实存在,是公开的,我添加了一个空的构造,在结果没有变化。纵观一些样本code,我也注意到从我的code被写入方式有些不同,虽然我没有看空构造函数之一:

1)是否有我的片段类应该声明静态任何理由因为许多样品?

2)我需要在我的片段类实现用的newInstance的?为什么会这样做只是有一个构造函数呢?例如从V14样品之一:

 公共静态类CountingFragment扩展片段{
    INT MNUM;

    / **
     *创建CountingFragment的新实例,提供NUM
     *作为参数。
     * /
    静态CountingFragment的newInstance(INT NUM){
        CountingFragment F =新CountingFragment();

        //供应NUM输入作为参数。
        捆绑的args =新包();
        args.putInt(民,NUM);
        f.setArguments(参数);

        返回F;
    }
 

我还是有点生疏了一些Java概念,因此可能会丢失一些基本的东西在这里。

道格·戈登   GHCS软件

解决方案
  

有什么理由,我的片段类应该声明静态因为许多样品?

只有当它是一个内部类的东西。既然你似乎是一个内部类 PersonTab ,那么它将需要是静态的。或者,移动 PersonTab 之外的是一个独立的Java类。

  

我需要在我的片段类实现用的newInstance的?为什么会这样做只是有一个构造函数,而不是?

这仅仅是一个工厂方法。它不需要由框架

I thought I had my conversion to fragments pretty well done until I rotated the screen. I am getting the following types of errors:

RuntimeException: Unable to start activity ComponentInfo{com.ghcssoftware.gedstar/com.ghcssoftware.gedstar.GedStar}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.ghcssoftware.gedstar.PersonTab$PersonTabFrag: make sure class name exists, is public, and has an empty constructor that is public

The class in question does exist, is public, and I added an empty constructor with no change in the results. Looking at some sample code, I do notice some differences from the way my code is written, although I don't see empty constructors either:

1) Is there any reason that my fragment class should be declared "static" as many samples are?

2) Do I need to implement the use of "newInstance" within my fragment class? Why is this done instead of just having a constructor? For example from one of the V14 samples:

public static class CountingFragment extends Fragment {
    int mNum;

    /**
     * Create a new instance of CountingFragment, providing "num"
     * as an argument.
     */
    static CountingFragment newInstance(int num) {
        CountingFragment f = new CountingFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
    }

I'm still a bit unfamiliar with some Java concepts, so may be missing something basic here.

Doug Gordon GHCS Software

解决方案

Is there any reason that my fragment class should be declared "static" as many samples are?

Only if it is an inner class of something. Since yours appears to be an inner class of PersonTab, then it will need to be static. Or, move it outside of PersonTab to be a standalone Java class.

Do I need to implement the use of "newInstance" within my fragment class? Why is this done instead of just having a constructor?

It is just a factory method. It is not required by the framework.

这篇关于Android的碎片重新实例化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆