17
votes

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

5
And your fragment has a public empty constructor ?superfell
Tough to say without the whole stack trace. (Those three lines are only part of it.) Probably safest to post a healthy chunk of the log surrounding the stack trace, too, in case there's something of interest in there. What's the onCreate method of your Fragment look like?nmr
Are you referencing your Fragment subclass from a layout file? Do you have different layout files for Portrait and Landscape?nmr

5 Answers

22
votes

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.

7
votes

Your Fragment shouldn't have constructors because of how the FragmentManager instantiate it. You should have a newInstance() static method defined and pass any parameters via arguments (bundle)

For example:

public static final MyFragment newInstance(int title, String message)
{
    MyFragment fragment = new MyFragment();
    Bundle bundle = new Bundle(2);
    bundle.putInt(EXTRA_TITLE, title);
    bundle.putString(EXTRA_MESSAGE, message);
    fragment.setArguments(bundle);
    return fragment ;
}

And read these arguments at onCreate:

@Override
public void onCreate(Bundle savedInstanceState)
{
    title = getArguments().getInt(EXTRA_TITLE);
    message = getArguments().getString(EXTRA_MESSAGE);

    //...

}

This way if detached and re-attached the object state can be stored through the arguments, much like bundles attached to Intents.

1
votes

I faced the similar problem when I renamed my project package name. The fragment class is referred by xml layout and usually contains the full package name.Thats where the problem was. My fragment class name was still having old package name.

1
votes

The Fragment class also should not be an inner class, because it is instantiated out of your Activity class scope. Nested class is ok.

0
votes

Adding

static

Fixed it for me

public class MyFragment extends Fragment