片段活动崩溃屏幕旋转 [英] Fragment activity crashes on screen rotate

查看:132
本文介绍了片段活动崩溃屏幕旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的片段活动。在OnCreate()方法中,我只需添加一个片段。在code如下公布。然而,每一个我旋转屏幕时,系统会调用的onCreate()再次,那么它崩溃的super.onCreate()语句;我想这是一个普遍的Andr​​oid的碎片问题。有人可以帮我吗?

 公共类FragActivity扩展FragmentActivity {
    @覆盖
    公共无效的onCreate(包savedState){
        super.onCreate(savedState);

        MyFragment FRAG =新MyFragment();
        getSupportFragmentManager()的BeginTransaction()代替(android.R.id.content,FRAG)
        。承诺();
    }
}
 

堆栈跟踪附:

  

了java.lang.RuntimeException:无法启动活动ComponentInfo {}:   android.support.v4.app.Fragment $ InstantiationException:无法   实例片段:确保类名称存在,是公开的,并具有   一个空的构造是公共

解决方案

那么,你的错误说,什么是错的与你的MyFragment类。请确保您有这样的:

 公共类MyFragment扩展片段
 

声明的片段类时。此外,你不应该在类中的任何构造函数。因此,请确保你没有之一。

如果您发布的code为您片段类中,我们也许能帮助你更好的。

I have a simple fragment activity. In the onCreate() method, I simply add a fragment. The code is posted below. However, each time I rotate the screen, system will call onCreate() again, then it crashes at the super.onCreate() statement; I suppose it is a general Android fragment issue. Can someone help me out?

public class FragActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedState){
        super.onCreate(savedState);

        MyFragment frag = new MyFragment();
        getSupportFragmentManager().beginTransaction().replace(android.R.id.content, frag)
        .commit();        
    }
}

The stack trace is attached:

java.lang.RuntimeException: Unable to start activity ComponentInfo{}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment: make sure class name exists, is public, and has an empty constructor that is public

解决方案

Well, as you error says, something is wrong with your MyFragment class. Make sure you have something like:

public class MyFragment extends Fragment

when declaring your fragment class. Also, you shouldn't have any constructor in the class. So make sure that you don't have one.

If you post the code for your Fragment class we might be able to help you better.

这篇关于片段活动崩溃屏幕旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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