Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment activity crashes on screen rotate

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

like image 998
Jimmy Avatar asked Nov 09 '11 00:11

Jimmy


1 Answers

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.

like image 62
Kurtis Nusbaum Avatar answered Sep 27 '22 16:09

Kurtis Nusbaum