列表视图,运行时停止错误。(针对调试集中停止)

时间:2015-01-27 04:36:45

标签: android android-listview

帮帮我,我为自定义listView编写代码(扩展ArrayAdapter并使用ArrayList)。但它没有奏效。 (请用我的英语)。

class:MyArrayAdapter

    package com.example.vd;

    import java.util.ArrayList;
    import android.app.Activity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.TextView;

    public class MyArrayAdapter extends ArrayAdapter<Product>{
     private Activity context=null;
     private int layoutId;
     private ArrayList<Product> array=null;
     public MyArrayAdapter(Activity context, int layoutId,        ArrayList<Product>arremp)
    {
        super(context,layoutId,arremp);
        this.context=context;
        this.layoutId=layoutId;
        this.array=arremp;
    }
    public View getView(int pos, View view, ViewGroup viewGroup)
    {

        LayoutInflater inflater=context.getLayoutInflater();
        view=inflater.inflate(layoutId, null);
        TextView txt=(TextView)context.findViewById(R.id.textView1);
        Product pd=array.get(pos);
        txt.setText(pd.toString());
        return view;
    }

}

类MainActivity

 package com.example.vd;

    import java.util.ArrayList;

    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;


    public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArrayList<Product> array=new ArrayList<Product>();
        ListView listView1 = (ListView) findViewById(R.id.listView1);
        Product[] items = { 
                new Product(1, "Milk", 21.50), 
                new Product(2, "Butter", 15.99), 
                new Product(3, "Yogurt", 14.90), 
                new Product(4, "Toothpaste", 7.99), 
                new Product(5, "Ice Cream", 10.00)}; 
        array.add(items[0]);
        array.add(items[1]);
        array.add(items[2]);
        MyArrayAdapter adapter=new MyArrayAdapter(this,R.layout.list_view,array);
        listView1.setAdapter(adapter);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

类产品

    package com.example.vd;

    public class Product {
    private int id;
    private String name;
    private double price;

    public Product(){
        super();
    }

    public Product(int id, String name, double price) {
        super();
        this.id = id;
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {
        return this.id + ". " + this.name + " [$" + this.price + "]";
    }
}

file xml:

<RelativeLayout 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: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="com.example.vd.MainActivity" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="17dp"
    android:layout_marginTop="50dp" >
</ListView>

file xml:list_view

<?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" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />
</LinearLayout>

2 个答案:

答案 0 :(得分:2)

你应该改变

 TextView txt=(TextView)context.findViewById(R.id.textView1);

 TextView txt=(TextView)view.findViewById(R.id.textView1);
<{1}} MyArrayAdapter

中的

答案 1 :(得分:0)

MyArrayAdapter类构造函数中,您要将null array分配给array的{​​{1}}个对象。

意味着尝试在空引用的ArrayList<Product>方法中调用ArrayList的get方法:

getView

注意:为了获得更好的ListView性能,请使用public MyArrayAdapter(Activity context, int layoutId, ArrayList<Product> arremp) { ... this.array=arremp; } 类来保存视图而不是再次膨胀。请参阅以下教程:

Customizing Android ListView Items with Custom ArrayAdapter