简体   繁体   中英

FragmentActivity crashing on orientation change (MainActivity seen as Fragment)

I am using a SherlockFragmentActivity with tabs. It loads fine the first time, but when the orientation gets changed, the following error occurs:

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

I've been looking at posts about similar problems, but there is a difference: in this error it seems like MainActivity is seen as a Fragment ("unable to instantiate fragment ext.domain.MainActivity"), instead of the used Fragment class being mentioned.

It does not help to give MainActivity an empty constructor. The Fragments that I actually use are called ArticleListFragment and it is a public class with an empty constructor.

I'm running the app on Android 2.3, below is the code (stripped of most irrelevant details and still throwing the exception):

MainActivity.java:

(Edit: added the instantiation of the ArticleListFragment which is an anonymous inner type. It seems that if I remove it, it does work.)

public class MainActivity extends SherlockFragmentActivity implements ActionBar.TabListener {

    ActionBar actionBar;

    public MainActivity() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        // Create ActionBar
        actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Create Tabs
        String[] tabs = { 
                getString(R.string.tab1), 
                getString(R.string.tab2), 
                getString(R.string.tab3),
                getString(R.string.tab4)
        };

        for(String tabname : tabs) {
            ActionBar.Tab tab = actionBar.newTab();
            tab.setText(tabname);
            tab.setTabListener(this);
            actionBar.addTab(tab);
        }
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        switch(tab.getPosition()) {
        case 0:
            ArticleListFragment home = new ArticleListFragment() { 
                            public void onAttach(Activity activity) {
                                // This is some code to populate the Fragment with an HTTPRequest
                                super.onAttach(activity);
                                RequestParams paramsHome = RequestClient.getBasicRequestParams(activity);
                                populate("frontpage", paramsHome);
                            };
                        };
            ft.replace(R.id.contentframe, home);
            break;
        case 3:
            ArticleListFragment saved = new ArticleListFragment() { 
                            public void onAttach(Activity activity) {
                                // This is some code to populate the Fragment with an HTTPRequest
                                super.onAttach(activity);
                                RequestParams paramsSaved = RequestClient.getBasicRequestParams(activity);
                                populate("saved", paramsSaved);
                            };
                        };
            ft.replace(R.id.contentframe, saved);
            break;
        }
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {

    }

}

ArticleListFragment.java:

public class ArticleListFragment extends SherlockFragment {
    public ArticleListFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.fragment_articlelist, null);
        return layout;
    }
}

Any ideas why this code is wrong?

What's the full code for MainActivity ? It isn't complaining about trying to instantiate MainActivity , but an anonymous inner class (indicated by the $1 ) of MainActivity .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM