java.lang.InstantiationException ::在ListView中使用自定义数组适配器

时间:2015-01-19 12:32:27

标签: android listview custom-adapter

我正在使用List和自定义数组适配器创建应用。我在运行我的应用程序时遇到以下异常。

01-19 17:52:30.645  11343-11343/com.example.innovative.list_customadapter E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.innovative.list_customadapter/com.example.innovative.list_customadapter.MyCustomAdapter}: java.lang.InstantiationException: can't instantiate class com.example.innovative.list_customadapter.MyCustomAdapter; no empty constructor
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2171)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
            at android.app.ActivityThread.access$700(ActivityThread.java:150)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5283)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.InstantiationException: can't instantiate class com.example.innovative.list_customadapter.MyCustomAdapter; no empty constructor
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1319)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
            at android.app.ActivityThread.access$700(ActivityThread.java:150)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5283)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)
01-19 17:52:39.674  11343-11343/com.example.innovative.list_customadapter I/Process﹕ Sending signal. PID: 11343 SIG: 9

以下是MyCustomAdapter.java的代码 - >

public class MyCustomAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    public MyCustomAdapter(Context context, String[] values) {
        super(context, R.layout.activity_my_custom_adapter, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.activity_my_custom_adapter, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        textView.setText(values[position]);

        String s = values[position];
        if (s.startsWith("Windows7") || s.startsWith("iPhone")|| s.startsWith("Solaris"))
        {
            imageView.setImageResource(R.drawable.abc);
        } else {
            imageView.setImageResource(R.drawable.ic_launcher);
        }

        return rowView;
    }
}

及以下是MyList Activity ...&gt;

的代码
public class MyList  extends ListActivity {
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
                "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
                "Linux", "OS/2" };
        MyCustomAdapter adapter = new MyCustomAdapter(this, values);
        setListAdapter(adapter);
    }

请有人告诉我代码中的问题在哪里。提前致谢。 :)

0 个答案:

没有答案