运行代码时适配器错误

时间:2014-06-27 12:02:15

标签: android android-listview textview

我是Android开发的新手,我正在使用listview ..我的代码没有任何错误但是当我尝试运行它时,应用程序崩溃了。

这是我的MainActivity代码:

package com.example.view;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {

ListView lv;
ArrayList<ListViewItem> items;
CustomListViewAdapter adapter;

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

    lv = (ListView) findViewById(R.id.listView);
    items = new ArrayList<MainActivity.ListViewItem>();
    items.add(new ListViewItem(R.drawable.ic_launcher, "Item1", "Item1 Description"));
    items.add(new ListViewItem(R.drawable.ic_launcher, "Item2", "Item2 Description"));
    items.add(new ListViewItem(R.drawable.ic_launcher, "Item3", "Item3 Description"));
    items.add(new ListViewItem(R.drawable.ic_launcher, "Item4", "Item4 Description"));
    adapter = new CustomListViewAdapter(this, items);
    lv.setAdapter(adapter);
}

class ListViewItem
{
    private int thumbnailResource;
    private String title;
    private String subTitle;

    public ListViewItem()
    {

    }

    public ListViewItem(int thumbnailResource, String title, String subTitle)
    {
        this.thumbnailResource = thumbnailResource;
        this.title = title;
        this.subTitle = subTitle;
    }

    public int getThumbnailResource()
    {
        return thumbnailResource;
    }

    public void setThumbnailResource(int thumbnailResource)
    {
        this.thumbnailResource = thumbnailResource;
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String title)
    {
        this.title = title;
    }

    public String getSubTitle()
    {
        return subTitle;
    }

    public void setSubTitle(String subTitle)
    {
        this.subTitle = subTitle;
    }

}
}

这是我的适配器类:

package com.example.view;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.view.MainActivity.ListViewItem;

public class CustomListViewAdapter extends ArrayAdapter<ListViewItem>
{
LayoutInflater inflater;
ArrayList<ListViewItem> items;

public CustomListViewAdapter(Activity context, ArrayList<ListViewItem> items)
{  
    super(context, R.layout.list_item_row, items);

    this.items = items;
    this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public View getView(final int position, View convertView, ViewGroup parent)
{  

    ListViewItem item = items.get(position);
    View rowview = inflater.inflate(R.layout.list_item_row, parent, false);

    ImageView imgView = (ImageView) rowview.findViewById(R.id.imgThumbnail);
    TextView txtTitle = (TextView) rowview.findViewById(R.id.txtTitle);
    TextView txtSubTitle = (TextView) rowview.findViewById(R.id.txtSubTitle);
    CheckBox chkBox = (CheckBox) rowview.findViewById(R.id.checkbox);

    imgView.setImageResource(item.getThumbnailResource());
    txtTitle.setText(item.getTitle());
    txtSubTitle.setText(item.getSubTitle());

    return rowview;  
}
}

这是行

的xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dip" >

<ImageView   
    android:layout_width="78dip"   
    android:layout_height="78dip"   
    android:id="@+id/imgThumbnail"   
    android:layout_alignParentLeft="true"  
    android:layout_centerInParent="true"
    android:layout_marginLeft="-3dip"
    android:scaleType="centerInside">  
</ImageView>  

<TextView   
    android:textAppearance="?android:attr/textAppearanceLarge"   
    android:layout_height="wrap_content"   
    android:text="TextView"   
    android:layout_width="wrap_content"   
    android:id="@+id/txtTitle"   
    android:layout_toRightOf="@+id/imgThumbnail"
    android:layout_marginTop="6dip"  
    android:layout_marginLeft="6dip">  
</TextView>  

<TextView   
    android:layout_height="wrap_content"   
    android:text="TextView"   
    android:layout_width="wrap_content"   
    android:id="@+id/txtSubTitle"   
    android:layout_toRightOf="@+id/imgThumbnail"  
    android:layout_below="@+id/txtTitle"
    android:layout_marginTop="3dip"  
    android:layout_marginLeft="6dip">  
</TextView>

<ListView
    android1:id="@+id/listView"
    android1:layout_width="match_parent"
    android1:layout_height="wrap_content"
    android1:layout_alignLeft="@+id/imgThumbnail"
    android1:layout_alignTop="@+id/txtTitle"
    android1:layout_marginLeft="17dp" >
</ListView>

<CheckBox
    android:id="@+id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="4px"
    android:layout_marginRight="10px" />

</RelativeLayout>

这些是logcat错误

06-27 14:59:38.748: E/MoreInfoHPW_ViewGroup(11621): Parent view is not a TextView
06-27 14:59:38.773: E/AndroidRuntime(11621): FATAL EXCEPTION: main
06-27 14:59:38.773: E/AndroidRuntime(11621): Process: com.example.view, PID: 11621
06-27 14:59:38.773: E/AndroidRuntime(11621): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.example.view/com.example.view.MainActivity}:     java.lang.NullPointerException
06-27 14:59:38.773: E/AndroidRuntime(11621):    at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
06-27 14:59:38.773: E/AndroidRuntime(11621):    at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)
06-27 14:59:38.773: E/AndroidRuntime(11621):    at     android.app.ActivityThread.access$800(ActivityThread.java:157)
06-27 14:59:38.773: E/AndroidRuntime(11621):    at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
06-27 14:59:38.773: E/AndroidRuntime(11621):    at     android.os.Handler.dispatchMessage(Handler.java:102)
06-27 14:59:38.773: E/AndroidRuntime(11621):    at android.os.Looper.loop(Looper.java:157)
06-27 14:59:38.773: E/AndroidRuntime(11621):    at     android.app.ActivityThread.main(ActivityThread.java:5293)
06-27 14:59:38.773: E/AndroidRuntime(11621):    at     java.lang.reflect.Method.invokeNative(Native Method)
06-27 14:59:38.773: E/AndroidRuntime(11621):    at     java.lang.reflect.Method.invoke(Method.java:515)
06-27 14:59:38.773: E/AndroidRuntime(11621):    at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
06-27 14:59:38.773: E/AndroidRuntime(11621):    at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
06-27 14:59:38.773: E/AndroidRuntime(11621):    at dalvik.system.NativeStart.main(Native     Method)
06-27 14:59:38.773: E/AndroidRuntime(11621): Caused by: java.lang.NullPointerException
06-27 14:59:38.773: E/AndroidRuntime(11621):    at     com.example.view.MainActivity.onCreate(MainActivity.java:27)
06-27 14:59:38.773: E/AndroidRuntime(11621):    at     android.app.Activity.performCreate(Activity.java:5389)
06-27 14:59:38.773: E/AndroidRuntime(11621):    at     android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
06-27 14:59:38.773: E/AndroidRuntime(11621):    at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
06-27 14:59:38.773: E/AndroidRuntime(11621):    ... 11 more

2 个答案:

答案 0 :(得分:1)

你在这一行收到错误 lv.setAdapter(adapter);

我确信 activity_main.xml 布局文件中不存在ListView

您已将ListView放入activity_main.xml文件中。

您在行文件的布局中错误地添加了ListView

答案 1 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

<强> activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</LinearLayout>

<强> list_item_row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:gravity="center">

    <ImageView
        android:layout_width="78dp"
        android:layout_height="78dp"
        android:id="@+id/imgThumbnail"
        android:adjustViewBounds="true">
    </ImageView>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_marginLeft="5dp">

        <TextView
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_width="wrap_content"
            android:id="@+id/txtTitle">
        </TextView>

        <TextView
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_width="wrap_content"
            android:id="@+id/txtSubTitle"
            android:layout_marginTop="3dp">
        </TextView>
    </LinearLayout>


    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"/>

</LinearLayout>

<强> MainActivity.java

public class MainActivity extends Activity {

    ListView lv;
    ArrayList<ListViewItem> items;
    CustomListViewAdapter adapter;

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

        lv = (ListView) findViewById(R.id.listView);
        items = new ArrayList<MainActivity.ListViewItem>();
        items.add(new ListViewItem(R.drawable.ic_launcher, "Item1", "Item1 Description"));
        items.add(new ListViewItem(R.drawable.ic_launcher, "Item2", "Item2 Description"));
        items.add(new ListViewItem(R.drawable.ic_launcher, "Item3", "Item3 Description"));
        items.add(new ListViewItem(R.drawable.ic_launcher, "Item4", "Item4 Description"));
        adapter = new CustomListViewAdapter(this, items);
        lv.setAdapter(adapter);
    }

    class ListViewItem
    {
        private int thumbnailResource;
        private String title;
        private String subTitle;

        public ListViewItem()
        {

        }

        public ListViewItem(int thumbnailResource, String title, String subTitle)
        {
            this.thumbnailResource = thumbnailResource;
            this.title = title;
            this.subTitle = subTitle;
        }

        public int getThumbnailResource()
        {
            return thumbnailResource;
        }

        public void setThumbnailResource(int thumbnailResource)
        {
            this.thumbnailResource = thumbnailResource;
        }

        public String getTitle()
        {
            return title;
        }

        public void setTitle(String title)
        {
            this.title = title;
        }

        public String getSubTitle()
        {
            return subTitle;
        }

        public void setSubTitle(String subTitle)
        {
            this.subTitle = subTitle;
        }

    }

    class CustomListViewAdapter extends BaseAdapter
    {
        Activity context;
        ArrayList<ListViewItem> items;

        public CustomListViewAdapter(Activity context, ArrayList<ListViewItem> items)
        {
            this.items = items;
            this.context = context;
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int position) {
            return items.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent)
        {
            ViewHolder holder;
            if(convertView==null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.list_item_row,null,false);

                holder.imgView = (ImageView) convertView.findViewById(R.id.imgThumbnail);
                holder.txtTitle = (TextView) convertView.findViewById(R.id.txtTitle);
                holder.txtSubTitle = (TextView) convertView.findViewById(R.id.txtSubTitle);
                holder.chkBox = (CheckBox) convertView.findViewById(R.id.checkbox);

                convertView.setTag(holder);
            }else{
                holder = (ViewHolder)convertView.getTag();
            }
            holder.imgView.setImageResource( items.get(position).getThumbnailResource());
            holder.txtTitle.setText( items.get(position).getTitle());
            holder.txtSubTitle.setText( items.get(position).getSubTitle());

            return convertView;
        }

        class ViewHolder {
            ImageView imgView;
            TextView txtTitle;
            TextView txtSubTitle;
            CheckBox chkBox;
        }
    }


}