简体   繁体   中英

java.lang.InstantiationException: can't instantiate class ; no empty constructor

我的应用崩溃了,我得到了这个logCat:

android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment kostas.menu.rssreader.ListActivity$SampleListFragment: make sure class name exists, is public, and has an empty constructor that is public

Your activity does not define a default constructor. Instead of passing titleRes as a constructor parameter, pass it as an intent extra. In your case, you don't need to define a constructor at all, so you can omit it)

public onCreate(...) {
  titleRes = getIntent().getIntExtra("titleResId", R.string.default_title);
}

To start activity:

Intent i = new Intent(this, ListActivity.class);
i.putExtra("titleResId", R.string.my_title);
startActivity(i);

ListActivity's super class means SlidingFragmentActivity class is not having constructor with int so you have to call super() as first statement from your ListActivity's class constructor. Please look into the blow code.

public ListActivity(int titleRes) {
    super();
    mTitleRes = titleRes;
}

Hope this will help you...:)

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