2 freedom wings5 freedom_wings5 于 2016.03.13 22:40 提问

安卓,baseadapter,报错,求指教

logcat如下:
图片说明

content_main:

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

    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main" tools:context=".MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listview"/>
</LinearLayout>

listview的布局:

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textview1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textview2"/>

</LinearLayout>

baseadapter的代码:

 public class MyAdapter extends BaseAdapter
{
    Context context;
    String[] tx1;
    String[] tx2;
    MyAdapter(Context context,String[] tx1,String[] tx2)
    {
        this.context=context;
        this.tx1=tx1;
        this.tx2=tx2;
    }
    @Override
    public int getCount()
    {
        return tx1.length;
    }
    @Override
    public Object getItem(int position)
    {
        return tx1[position];
    }
    @Override
    public long getItemId(int position)
    {
        return 0;
    }

    @Override
    public View getView(int position,View v,ViewGroup viewGroup)
    {
        View view;
        ViewHolder viewHolder;
        if(v == null) {
            view = LayoutInflater.from(context).inflate(R.layout.listview, viewGroup);
            ;

            TextView t1 = (TextView) view.findViewById(R.id.textview1);
            TextView t2 = (TextView) view.findViewById((R.id.textview2));

            t1.setText(tx1[position]);
            t2.setText(tx2[position]);

            viewHolder=new ViewHolder(t1,t2);
            view.setTag(viewHolder);
        }
        else
        {
            view=v;
            viewHolder=(ViewHolder)view.getTag();
        }

        return v;
    }

    class ViewHolder
    {
        public TextView t1;
        public TextView t2;
        ViewHolder(TextView t1,TextView t2)
        {
            this.t1=t1;
            this.t2=t2;
        }
    }

}

mainactivity中与listview有关的也就是:

ListView listView;
    String[] str1={"这是1","这是2","这是3","这是4","这是5"};
    String[] str2={"this is 1","this is 2","this is 3","this is 4","this is 5"};
 listView=(ListView)super.findViewById(R.id.listview);
        MyAdapter myAdapter=new MyAdapter(this,str1,str2);
        listView.setAdapter(myAdapter);

求指教,怎么解决?

4个回答

qq_21949639
qq_21949639   2016.03.13 23:20

settext放到return上面去,

danielinbiti
danielinbiti   Ds   Rxr 2016.03.13 23:26
 if(v == null) {
            view = LayoutInflater.from(context).inflate(R.layout.listview, viewGroup);
改成
if(v == null) {
            view = LayoutInflater.from(context).inflate(R.layout.listview, null);//传入viewGroup,则布局方式的layout不对
lly4321lly
lly4321lly   2016.03.14 18:21

view = LayoutInflater.from(context).inflate(R.layout.listview, viewGroup); viewGroup是默认把父布局ListView传进去,
你inflate生成的View会被通过addView的方式add到父布局也就是ListView里面,listView是不可以addView的,
listView是AdapterView的子类,所以会报错
改成 view = LayoutInflater.from(context).inflate(R.layout.listview, null);
或者 view = LayoutInflater.from(context).inflate(R.layout.listview, viewGroup,false);都可以

lxk_1993
lxk_1993   Rxr 2016.03.18 08:26

view = LayoutInflater.from(context).inflate(R.layout.listview, viewGroup);
;

                    viewGroup 改为  null 就好了
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!