Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在列表视图中添加图像图标会使我的应用程序崩溃_Java_Android_Xml_Android Layout_Listview - Fatal编程技术网

Java 在列表视图中添加图像图标会使我的应用程序崩溃

Java 在列表视图中添加图像图标会使我的应用程序崩溃,java,android,xml,android-layout,listview,Java,Android,Xml,Android Layout,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开发人员,我正在使用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);
                }   
            });
        }
    }
以下是我的活动\u sms\u 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>

这条线造成了问题

// 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);
ArrayAdapter adapt=新的ArrayAdapter(这个,R.layout.row,R.id.YOUR_TEXT_视图,值);

您正在将arrayadapter用于listview。作为参数传递给arrayadapter构造函数的行必须是textview。请参阅

如果要自定义listview行,则需要使用扩展arrayadapter类的自定义arrayadapter。请参阅


对于自定义ArrayAdapter。

您不能简单地在该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;
    }

setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
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;
    }