Prasanna
Prasanna

Reputation: 3771

Activity crashes on screen rotation caused by fragment

I have an activity that hosts 4 fragements in it. On screen rotate, the activity crashes saying the following:

04-01 16:43:46.670: E/AndroidRuntime(2401): Caused by: java.lang.InstantiationException: can't instantiate class com.example.activity.FragmentActivity$UserFragment; no empty constructor
04-01 16:43:46.670: E/AndroidRuntime(2401):     at java.lang.Class.newInstanceImpl(Native Method)
04-01 16:43:46.670: E/AndroidRuntime(2401):     at java.lang.Class.newInstance(Class.java:1319)
04-01 16:43:46.670: E/AndroidRuntime(2401):     at android.app.Fragment.instantiate(Fragment.java:574)

I have seen this question which says the fragment class must be declared public. It is declared public and it does not resolve the problem.

Following is the code I have:

public class FragmentActivity extends Activity
{
    public class TabListener implements ActionBar.TabListener
    {
        private Fragment mFragment;

        public TabListener(Fragment fragment)
        {
           mFragment = fragment;
        }

        public void onTabSelected(Tab tab, FragmentTransaction ft)
        {
           ft.add(R.id.fragmentContainer, mFragment, null);
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft)
        {
           ft.remove(mFragment);
        }

        public void onTabReselected(Tab tab, FragmentTransaction ft)
        {
        }
    }

    public class UserFragment extends Fragment
    {
       @Override
       public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
       {
         .....
         ..... 
       }
    }

    @Override
    public void onCreate(android.os.Bundle savedInstanceState)
    {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.fragment_container);

       ActionBar actionBar = getActionBar();
       actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
       Tab userTab = actionBar.newTab();
       userTab.setIcon(R.drawable.person).setTabListener(new TabListener(new UserFragment()));
       actionBar.addTab(userTab);
   }
}

Upvotes: 2

Views: 3902

Answers (6)

Avinash Joshi
Avinash Joshi

Reputation: 1308

Seeing your code it looks like you have defined all your fragments in the same class instead just create separate class file for each fragment.

that worked for me as a charm

Upvotes: 0

Johnny Z
Johnny Z

Reputation: 15449

Joe is right. You need to declare the inner Fragment class static to separate it from a instance of its encapsulating class or declare it in its own .java file.

Upvotes: 1

Bakyt
Bakyt

Reputation: 1308

1) Add to UserFragment class an emty constructor:

public class UserFragment extends Fragment
{
   public UserFragment(){}

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
   {
     .....
     ..... 
   }
}

2) Be sure that UserFragment class is a separate class (not in FragmentActivity class) and in separate *.java file (Ex: UserFragment.java)

Upvotes: 0

Joe Bowbeer
Joe Bowbeer

Reputation: 3841

UserFragment must be a static nested class in order to be re-instantiated:

public static class UserFragment extends Fragment { 
     ....
}

Upvotes: 3

Rajdeep Dua
Rajdeep Dua

Reputation: 11230

Try creating an empty constructor for the fragment

public class UserFragment extends Fragment { 
     public UserFragment(){}
     ....
}

Upvotes: 1

Janhouse
Janhouse

Reputation: 376

Did you try adding android:launchMode="singleTask" to the AndroidManifest.xml.

Example:

   <activity
        android:launchMode="singleTask"
        android:name=".MainActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Upvotes: 1

Related Questions