Android 实例化异常:can';t实例化类;没有空构造函数

Android 实例化异常:can';t实例化类;没有空构造函数,android,android-fragments,fragment,Android,Android Fragments,Fragment,我的应用程序崩溃,我得到以下日志: 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 您的活动未

我的应用程序崩溃,我得到以下日志:

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

您的活动未定义默认构造函数。与其将标题作为构造函数参数传递,不如将其作为额外的意图传递。在您的情况下,根本不需要定义构造函数,因此可以省略它)

要开始活动:

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

ListActivity的超类意味着SlidingFragmentActivity类没有int构造函数,因此您必须调用super(),作为ListActivity类构造函数的第一条语句。 请检查一下blow代码

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

希望这能帮助你……:)

不,不,不!你永远不应该自己实例化一个活动!而且,您永远不应该在自己的活动中实现构造函数!不要那样做!住手,马上@blackbelt以这种方式返回错误如果要将数据传递给新活动,请使用intent extra,而不是构造函数中的参数…这与活动无关,而是与片段有关。活动不应该有构造函数!它只会导致上述错误。只是不要说应该有一个构造函数!我不同意,有时构造函数是完美的,并且是指定的(例如,在创建基本抽象活动类时)。当然,只要你知道你在做什么。我相应地更新了答案。@MarvinLabs,我已经按照您的方式进行了操作,但仍然得到相同的第一个logcat错误。请给出一个您需要构建活动的示例?您何时需要一个不会通过intent启动的活动实例?抽象的基本活动类无法实例化,那么为什么需要构造函数呢?\u顶部的@menu\u不要调用
new ListActivity()
,在调用该类的地方,将其删除并使用MarvinLabs中的上述示例
public ListActivity(int titleRes) {
    super();
    mTitleRes = titleRes;
}