错误ListView自定义适配器

时间:2016-11-03 17:06:01

标签: android listview android-arrayadapter

所以我用自定义对象和自定义适配器创建了这个新的列表视图。但是每当我启动应用程序时,我的活动就会崩溃。我是初学者所以请解释我的错误。

这是我的主要活动

    public class MainActivity extends ListActivity {

private ArrayList<Android> androids = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    androids.add(new Android(R.drawable.android_a, R.string.android_a, R.string.data_a));
    androids.add(new Android(R.drawable.android_a, R.string.android_b, R.string.data_b));
    androids.add(new Android(R.drawable.cupcake, R.string.name_cupcake, R.string.data_c));
    androids.add(new Android(R.drawable.donut, R.string.name_donut, R.string.data_d));
    androids.add(new Android(R.drawable.ecalir, R.string.name_eclairs, R.string.data_e));
    androids.add(new Android(R.drawable.froyo, R.string.name_froyo, R.string.data_f));
    androids.add(new Android(R.drawable.gb, R.string.name_ginderbread, R.string.data_g));
    androids.add(new Android(R.drawable.honeycomb, R.string.name_honeycomb, R.string.data_h));
    androids.add(new Android(R.drawable.ics, R.string.name_ics, R.string.data_i));
    androids.add(new Android(R.drawable.jb, R.string.name_jb, R.string.data_j));
    androids.add(new Android(R.drawable.kk, R.string.name_kitkat, R.string.data_k));
    androids.add(new Android(R.drawable.lolipop, R.string.name_lolipop, R.string.data_l));
    androids.add(new Android(R.drawable.mashmallow, R.string.name_mashmallow, R.string.data_m));
    androids.add(new Android(R.drawable.nougat, R.string.name_nougat, R.string.data_n));


    AndroidAdapter adapter = new AndroidAdapter(this, android.R.layout.simple_list_item_1, androids);
    ListView listView = (ListView) findViewById(R.id.list);
    listView.setAdapter(adapter);



}

}

这是我的自定义对象

    public class Android {

private int mImageResourceID;
private int mAndroidVersion;
private int mAndroidDetails;

public Android(){ }

public Android(int imageResourceID, int androidVersion , int androidDetails)
{
    mImageResourceID = imageResourceID;
    mAndroidVersion = androidVersion;
    mAndroidDetails = androidDetails;
}
int getImageResourceID()
{
    return mImageResourceID;
}
int getAndroidVersion()
{
    return mAndroidVersion;
}
int getAndroidDetails()
{
    return mAndroidDetails;
}

}

这是我的自定义适配器

    public class AndroidAdapter extends ArrayAdapter<Android> {

private ArrayList<Android> android;


public AndroidAdapter(Context context, int resource , ArrayList<Android>android)  {
    super(context, resource,android);
    this.android = android;
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if(v==null)
    {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.list_view , null);

    }

    Android i = android.get(position);

    if(i!= null)
    {
        ImageView image = (ImageView) v.findViewById(R.id.image_android);
        TextView androidName = (TextView) v.findViewById(R.id.android_name);
        TextView androidDetails = (TextView) v.findViewById(R.id.android_details);

        image.setImageResource(i.getImageResourceID());
        androidName.setText(i.getAndroidVersion());
        androidDetails.setText(i.getAndroidDetails());

     }
    return v;

}

}

这是我收到的错误消息

11-03 22:25:40.531 31629-31629/com.example.android.android E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android.android, PID: 31629
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.android/com.example.android.android.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
    at android.app.ActivityThread.access$1100(ActivityThread.java:229)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:7325)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
    Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
    at android.app.ListActivity.onContentChanged(ListActivity.java:243)
    at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:492)
    at android.app.Activity.setContentView(Activity.java:2400)
    at com.example.android.android.MainActivity.onCreate(MainActivity.java:24)
    at android.app.Activity.performCreate(Activity.java:6904)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
    at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:7325) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
11-03 22:25:43.121 31629-31629/com.example.android.android I/
Process: Sending signal. PID: 31629 SIG: 9

请帮助我。

1 个答案:

答案 0 :(得分:0)

您需要注意的错误很少。

  1. 在Activity类中,您需要传递list_item_layout而不是Android默认布局。

    AndroidAdapter adapter = new AndroidAdapter(this, R.layout.list_view, androids);
    
  2. 在此方法之后调用setAdapter方法。

     setListAdapter(adapter);
    
  3. 在活动的布局文件中,确保ListView ID如下所示

     android:id="@+id/android:list"
    
  4. 您没有将膨胀的视图分配给Adapter类中的任何对象。

    v = inflater.inflate(R.layout.list_view , null);
    
  5. 或者您可以使用BaseAdapter。