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

克里斯·詹姆斯·汉考克斯

请参阅下面的内容,我得到了一个无法实例化的类:尝试运行“ 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清单...。

<?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>

克里斯·詹姆斯·汉考克斯

这已经是漫长的一天,而我只是个愚蠢的人。感谢@Raghunandan向我指出这一点。

我将HomeFragmentListExpand移到其自己的类中,并更改了清单和所有优点。

谢谢男生/女生。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Android IntentService无法实例化类;没有空的构造函数

无法实例化类的代理。找不到无参数的构造函数

无法实例化代理...找不到无参数的构造函数

Android通过其他类java.lang.InstantiationException中的线程调用函数:无法实例化类……没有空的构造函数

Android BroadcastReceiver:无法实例化接收器-没有空的构造函数

您可以使用无类型对象实例化Java类构造函数吗?

依赖注入:在无参数构造函数中实例化具体类时,您会失去什么

复制构造函数的类实例化

从类构造函数实例化对象的集合

InstantiationException:无法实例化片段,请确保类名称存在,是公共的并且具有一个空的,构造函数为public

MassTransit.QuartzIntegration.ScheduledMessageJob:无法实例化没有空构造函数的类型(参数'ScheduledMessageJob')'

由于“ TypeError:...不是构造函数”,无法实例化UI5类

JsonMappingException:找不到适合类型[简单类型,类]的构造函数:无法从JSON对象实例化

无法实例化 HeroController 类;构造函数抛出异常;嵌套异常是 java.lang.NullPointerException

使用模板化构造函数实例化非模板类

模板化类构造函数的模板实例化

使用COM的参数化构造函数实例化类

Android视图模型类没有零参数构造函数,片段中的实例化异常

在构造函数中初始化空实例变量

空的构造函数,但实例变量仍在初始化?

当父类无法将自身作为构造函数的参数时,使用实例初始化扩展类

如何通过类类型委托构造函数的实例化?

Java:实例化没有默认构造函数的通用类

使用私有构造函数来防止类的实例化?

构造函数接受参数时如何实例化模板类

在子类实例化中使用替代超类构造函数

如何实例化使用构造函数扩展类的特征

显式实例化的类模板中的自动构造函数

字节好友实例化类,不带构造函数参数