我的ListView无法工作......它说:"不幸的是你的应用程序停止了#34;

时间:2015-08-08 00:04:33

标签: android

我想知道它为什么告诉我:"很遗憾你的应用已经停止"?

我按照此视频中的所有步骤操作:
https://www.youtube.com/watch?v=0zQCv0Xb3pk&index=84&list=PLonJJ3BVjZW6hYgvtkaWvwAVvOFB7fkLa

主要活动中的代码

public class MainActivity extends Activity {

    ListView list;
    String[] mimititles;
    String[] description;
    int[] images = {R.drawable.m1,R.drawable.m2,R.drawable.m3,R.drawable.m4,R.drawable.m4,
            R.drawable.m5,R.drawable.m6,R.drawable.m7,R.drawable.m8,R.drawable.m9,R.drawable.m10};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


     Resources  res= getResources();
     mimititles= res.getStringArray(R.array.title);
     description = res.getStringArray(R.array.description);

     list=(ListView)findViewById(R.id.listView);

    ibrahimadapter adapter = new ibrahimadapter(this,mimititles,images,description);
    list.setAdapter(adapter);
    }



}

class ibrahimadapter extends ArrayAdapter<String>

{
    Context context;
    int[] images;
    String [] tiltearray;
    String [] descrip;

    ibrahimadapter(Context c,String[]titles,int[]imgs,String[] desc )
    {
        super(c,R.layout.single_row,R.id.textView1,titles);
        this.context=c;
        this.images=imgs;
        this.tiltearray=titles;
        this.descrip=desc;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View raw=inflate.inflate(R.layout.single_row,parent,false);

   ImageView myimage= (ImageView) raw.findViewById(R.id.imageView1);
   myimage.setImageResource(images[position]);

   TextView mytitle = (TextView) raw.findViewById(R.id.textView1);
     mytitle.setText(tiltearray[position]);

   TextView mydescription = (TextView) raw.findViewById(R.id.textView2);

     mydescription.setText(descrip[position]);

        return raw;
    }
}

我在activity_main.xml布局中的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    xmlns:android2="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android2:id="@+id/listView"
        android2:layout_width="match_parent"
        android2:layout_height="wrap_content"
        >
    </ListView>

</LinearLayout>

我在single_row.xml中的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="10dp"
        android:src="@drawable/m1" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/imageView1"
        android:layout_toRightOf="@+id/imageView1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:2)

在适配器中:

你的构造函数应该是:public ibrahimadapter(....) {.....}

getView函数更改为:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final Holder holder;
    View raw = converView;
    if(raw == null){
       raw=inflate.inflate(R.layout.single_row,parent,false);
       holder = new Holder();
       holder.imgView = (ImageView) raw.findViewById(R.id.imageView1);

       holder.myTitle = (TextView) raw.findViewById(R.id.textView1);

       holder.myDes = (TextView) raw.findViewById(R.id.textView2);


       raw.setTag(holder)
    } else {
    holder=(Holder)raw.getTag();
    }
    holder.imgView.setImageResource(images[position]);
    holder.myTitle.setText(tiltearray[position]);
    holder.myDes.setText(descrip[position]);
    return raw;
}

public static class Holder {
    ImageView imgView;
    TextView myTitle;
    TextView myDes;
}

答案 1 :(得分:1)

试试这个:
改变这个 超级(C,R.layout.single_row,R.id.textView1,标题)

对此 超级(C,R.layout.single_row,标题)

请检查每个String [] mimititles,String [] description和int []图像的项目数是否都相同。 例如,如果要在列表中显示5个项目,则必须有5个图像,5个mimititles和5个描述。 所以检查你的字符串数组文件