Android:无法实例化类:没有空构造函数。

3
请看下面,当我尝试运行我的“HomeFragmentListExpand”类时,在LogCat中出现了“无法实例化类:没有空构造函数”的错误提示,尽管我明确在那里有一个空构造函数。
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.app.Activity;
import android.content.Intent;
import android.util.SparseArray;
import android.widget.ExpandableListView;

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

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

    View rootView = inflater.inflate(R.layout.fragment_home, container, false);

    Intent intent = new Intent(getActivity(), HomeFragmentListExpand.class);
    startActivity(intent);

    return rootView;

}

public class HomeFragmentListExpand extends Activity {

    public HomeFragmentListExpand(){
        super();
    }


    // more efficient than HashMap for mapping integers to objects
    SparseArray<Group> groups = new SparseArray<Group>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_home);
        createData();
        ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);
        MyExpandableListAdapter adapter = new MyExpandableListAdapter(this,
                groups);
        listView.setAdapter(adapter);
        }
    public void createData() {
        for (int j = 0; j < 5; j++) {
            Group group = new Group("Test " + j);
            for (int i = 0; i < 5; i++) {
                group.children.add("Sub Item" + i);
                }
            groups.append(j, group);
        }
        }
    }

}

LogCat.........

01-10 16:41:45.143: E/AndroidRuntime(24809): FATAL EXCEPTION: main
01-10 16:41:45.143: E/AndroidRuntime(24809): java.lang.RuntimeException: Unable to instantiate activity  ComponentInfo{com.smarte.smarteproducts/com.smarte.smarteproducts.HomeFragment$HomeFragmentListExpand}: java.lang.InstantiationException: can't instantiate class com.smarte.smarteproducts.HomeFragment$HomeFragmentListExpand; no empty constructor
01-10 16:41:45.143: E/AndroidRuntime(24809):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2435)
01-10 16:41:45.143: E/AndroidRuntime(24809):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
01-10 16:41:45.143: E/AndroidRuntime(24809):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
01-10 16:41:45.143: E/AndroidRuntime(24809):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1413)
01-10 16:41:45.143: E/AndroidRuntime(24809):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 16:41:45.143: E/AndroidRuntime(24809):    at android.os.Looper.loop(Looper.java:158)
01-10 16:41:45.143: E/AndroidRuntime(24809):    at android.app.ActivityThread.main(ActivityThread.java:5789)
01-10 16:41:45.143: E/AndroidRuntime(24809):    at java.lang.reflect.Method.invokeNative(Native Method)
01-10 16:41:45.143: E/AndroidRuntime(24809):    at java.lang.reflect.Method.invoke(Method.java:525)
01-10 16:41:45.143: E/AndroidRuntime(24809):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
01-10 16:41:45.143: E/AndroidRuntime(24809):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
01-10 16:41:45.143: E/AndroidRuntime(24809):    at dalvik.system.NativeStart.main(Native Method)
01-10 16:41:45.143: E/AndroidRuntime(24809): Caused by: java.lang.InstantiationException: can't instantiate class com.smarte.smarteproducts.HomeFragment$HomeFragmentListExpand; no empty constructor
01-10 16:41:45.143: E/AndroidRuntime(24809):    at java.lang.Class.newInstanceImpl(Native Method)
01-10 16:41:45.143: E/AndroidRuntime(24809):    at java.lang.Class.newInstance(Class.java:1130)
01-10 16:41:45.143: E/AndroidRuntime(24809):    at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
01-10 16:41:45.143: E/AndroidRuntime(24809):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
01-10 16:41:45.143: E/AndroidRuntime(24809):    ... 11 more
01-10 16:46:46.023: D/Process(24809): killProcess, pid=24809
01-10 16:46:46.023: D/Process(24809): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:123 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690 

Android Manifest....

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smarte.smarteproducts"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Smartestyle" >
    <activity
        android:name="com.smarte.smarteproducts.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.smarte.smarteproducts.HomeFragment$HomeFragmentListExpand" />
</application>


你在Fragment中有一个Activity类作为内部类吗?请发布你的清单文件。我猜你可能感到困惑了。 - Raghunandan
是的,因为我不能将活动类的内容放在片段类中。 - Chris James Hancocks
1
@ChrisJamesHancocks 没有必要创建一个 activity 类的构造函数。其次,我猜你对 fragment 和 activity 的工作方式感到困惑。请发布你的清单文件。 - Raghunandan
我已经添加了清单文件。我想我有点困惑。已经有一段时间了 -_- - Chris James Hancocks
@ChrisJamesHancocks 将活动移出片段类。您可以从活动传递值到片段。此外,片段由活动托管。 - Raghunandan
显示剩余3条评论
1个回答

1

这是一个漫长的一天,我只是有些愚蠢。感谢 @Raghunandan 指出这一点。

我将 HomeFragmentListExpand 移到了自己的类中,并修改了清单文件,一切都很好。

谢谢大家。


1
你还应该删除Activity的构造函数。 - Raghunandan
我也遇到了同样的问题,虽然我的应用程序之前一直运行良好,但突然间开始出现这个错误,请问有什么原因吗? - Yasha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接