片段InstantiationException

时间:2013-11-21 10:56:55

标签: android android-fragments android-support-library

基本上我收到了这个错误:

android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment Archenemy: make sure class name exists, is public, and has an empty constructor that is public

现在我知道有很多问题,问题是他们没有空的构造函数。我知道,你会看到。我的问题是,还有什么可能导致这个错误?

public class Archenemy_Activity extends android.support.v4.app.Fragment {
    public Archenemy_Activity() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.archenemy_layout, container, false);
    }
}

谢谢!

3 个答案:

答案 0 :(得分:0)

尝试使用new Archenemy_Activity()代替Archenemy_Activity.instantiate(.....)

答案 1 :(得分:0)

使你的构造函数如下:

public Archenemy_Activity newInstance(){
      Archenemy_Activity fragment = new Archenemy_Activity();
      return fragment;
}

当实例只使用Archenemy_Activity.newInstance();

答案 2 :(得分:0)

事实证明它无法创建类的原因是因为在尝试实例化时无法找到类。我使用以下代码:

Arch_Fragment = Fragment.instantiate(parent, "ArchenemyFragment");

但是我没有在该字符串中包含该类的完整路径(它应该是“com.example.namespace.ArchenemyFragment”我用它替换它(它会自动计算完整路径),它是一直在努力:

Arch_Fragment = Fragment.instantiate(parent, ArchenemyFragment.class.getName());