Android 列表视图,运行时错误已停止。(针对Deug已停止)
帮助我,我为自定义listView编写代码(扩展ArrayAdapter并使用ArrayList)。但它不起作用。(请帮我学习英语) 类别:MyArrayAdapterAndroid 列表视图,运行时错误已停止。(针对Deug已停止),android,android-listview,Android,Android Listview,帮助我,我为自定义listView编写代码(扩展ArrayAdapter并使用ArrayList)。但它不起作用。(请帮我学习英语) 类别:MyArrayAdapter package com.example.vd; import java.util.ArrayList; import android.app.Activity; import android.view.LayoutInflater; import android.view.View;
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;
}
}
文件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>
文件xml:list\u视图
<?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>
您应该更改
TextView txt=(TextView)context.findViewById(R.id.textView1);
到
在
getView(…)
中的MyArrayAdapter
中的MyArrayAdapter
类构造函数中,您正在将null
数组分配给ArrayList
的array
对象
表示试图调用空引用上的getView
方法中ArrayList的get
方法:
public MyArrayAdapter(Activity context,
int layoutId, ArrayList<Product> arremp)
{
...
this.array=arremp;
}
公共MyArrayAdapter(活动上下文、,
int layoutId,ArrayList arremp)
{
...
this.array=arremp;
}
注意:要获得更好的ListView性能,请使用ViewHolder
类来保存视图,而不是再次充气。请参见以下教程:
谢谢你的帮助。你可以帮我学安卓。我喜欢。但我不是每个人都在学习。如果你接受,你可以为我你的安德鲁斯联系(facebook,电子邮件)thks。谢谢你的理想。
TextView txt=(TextView)context.findViewById(R.id.textView1);
TextView txt=(TextView)view.findViewById(R.id.textView1);
public MyArrayAdapter(Activity context,
int layoutId, ArrayList<Product> arremp)
{
...
this.array=arremp;
}