在ListView中添加图像图标会使我的应用崩溃



我是一个新的android开发人员,我使用ListView从数据库中显示列表视图上的所有值,这是Sqlite,我正在扩展与列表活动的主活动。下面是我的代码:

主活动

package com.example.sqliteapplication;

import java.util.List;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
    public class SmsMerge extends ListActivity {
        private ImageButton imgb;
        private TemplateOperation templateDBoperation;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sms_merge);
            templateDBoperation = new TemplateOperation(this);
            templateDBoperation.open();
            List values = templateDBoperation.getAllTemplate();
            ListView listview = (ListView)findViewById(android.R.id.list);
            // Use the SimpleCursorAdapter to show the
            // elements in a ListView
            ArrayAdapter adapter = new ArrayAdapter(this,
                    R.layout.row, values);
            listview.setAdapter(adapter);
            imgb=(ImageButton)findViewById(R.id.btnaddtemplate);
            imgb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Log.d("hy humayoon", "kdmfoskfvodfkbij");
                    Intent intent = new Intent(SmsMerge.this, SqliteActivity.class);
                    startActivity(intent);
                }   
            });
        }
    }
下面是我的activity_sms_merge.xml布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@layout/bg_player_header"
    android:orientation="vertical"
    tools:context="com.SMS.smsmerge.SmsMergeActivity$PlaceholderFragment" >
 <LinearLayout 
        android:id="@+id/player_header_bg"
        android:layout_width="fill_parent"
        android:layout_height="60dip"
        android:background="@layout/bg_player_header"
        android:layout_alignParentTop="true"
        android:paddingLeft="5dp"
        android:paddingRight="5dp">
        <TextView 
            android:id="@+id/songTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textColor="#04b3d2"
            android:textSize="16dp"
            android:paddingLeft="10dp"
            android:textStyle="bold"
            android:text="Templates"
            android:layout_marginTop="10dp"/>"
        <!-- Sttting  button -->
        <ImageButton 
            android:id="@+id/btnsetting"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:src="@drawable/settings"
            android:background="@null"
            />
        <!-- add template button -->
        <ImageButton 
            android:id="@+id/btnaddtemplate"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:src="@drawable/btn_add"
            android:background="@null"
            />
    </LinearLayout>
 <ListView
     android:id="@android:id/list"
     android:layout_width="match_parent"
     android:divider="#FFEEEE" 
     android:dividerHeight="2dp"
     android:layout_height="wrap_content"
     >
 </ListView>
</LinearLayout>

下面是Row.xml代码:

<?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:id="@+id/lblListItem"
       android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textSize="15dp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:textColor="#daac56"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"/>
</LinearLayout>

错误如下:

05-07 13:00:29.376: W/dalvikvm(25083): threadid=1: thread exiting with uncaught exception (group=0x40f199a8)
05-07 13:00:29.395: E/AndroidRuntime(25083): FATAL EXCEPTION: main
05-07 13:00:29.395: E/AndroidRuntime(25083): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.widget.AbsListView.obtainView(AbsListView.java:2207)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.widget.ListView.onMeasure(ListView.java:1162)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.view.View.measure(View.java:15609)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.view.View.measure(View.java:15609)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.view.View.measure(View.java:15609)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:850)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.view.View.measure(View.java:15609)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2191)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.view.View.measure(View.java:15609)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2165)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1249)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1443)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4879)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.view.Choreographer.doCallbacks(Choreographer.java:579)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.view.Choreographer.doFrame(Choreographer.java:548)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.os.Handler.handleCallback(Handler.java:725)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.os.Looper.loop(Looper.java:153)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.app.ActivityThread.main(ActivityThread.java:5299)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at java.lang.reflect.Method.invokeNative(Native Method)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at java.lang.reflect.Method.invoke(Method.java:511)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-07 13:00:29.395: E/AndroidRuntime(25083):    at dalvik.system.NativeStart.main(Native Method)
05-07 13:00:29.395: E/AndroidRuntime(25083): Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
05-07 13:00:29.395: E/AndroidRuntime(25083):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
05-07 13:00:29.395: E/AndroidRuntime(25083):    ... 38 more

这一行出现了问题

// elements in a ListView
            ArrayAdapter adapter = new ArrayAdapter(this,
                    R.layout.row, values);

更改为

ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.row, R.id.YOUR_TEXT_VIEW, values);

你正在为你的listview使用arrayadapter。作为参数传递给arrayadapter构造函数的行必须是textview。请参考http://developer.android.com/reference/android/widget/ArrayAdapter.html

如果你想自定义你的listview行,那么你需要使用自定义的arrayadapter来扩展arrayadapter类。请参考

http://www.vogella.com/tutorials/AndroidListView/article.html

你不能简单地在那个ArrayAdapter中应用你自己的行布局,你必须创建一个自定义适配器来制作带有图像的listview

根据下面的代码更改smsMerge类中的代码

lv = (ListView) findViewById(R.id.listView);
    lv.setVerticalFadingEdgeEnabled(true);
// Set an Adapter to the ListView
adapter = new CustomListAdapter(this);
lv.setAdapter(adapter);

,并将这个类添加为内部类

class CustomListAdapter extends BaseAdapter {
    private LayoutInflater layoutInflater;

    public CustomListAdapter(ListActivity activity) {
        layoutInflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {

        count// Set the total list item count
        return count;
    }
    @Override
    public Object getItem(int position) {
        return position;
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Inflate the item layout and set the views
        View listItem = convertView;
        int pos = position;
        List_item item;
        if (listItem == null) 
        {
            listItem = layoutInflater.inflate(R.layout.ROW, null);
            item=new List_item();
            item.iv = (ImageView) listItem.findViewById(R.id.thumb);
            //item.tvTitle = (TextView) listItem.findViewById(R.id.title);
            //item.tvDate = (TextView) listItem.findViewById(R.id.date);
            //item.arrow= (ImageView) listItem.findViewById(R.id.arrow);
        }
        else
        {
        item=(List_item)listItem.getTag();
        }
           item.iv.setImageResource(values.get(pos));// where values give us resource id  


        listItem.setTag(item);
        return listItem;
    }
}

并将这个类也添加为内部类

public class List_item
    {
        ImageView iv;
        //TextView tvTitle ;
        //TextView tvDate ;
        //ImageView arrow;
    }

最新更新