列表视图,错误在 运行 时停止。(聚焦停止以进行调试)
List View, Error stopped when running.(focused stopped for degug)
帮帮我,我为自定义 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;
}
}
class 主活动
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);
}
}
Class 产品
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 + "]";
}
}
文件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_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>
你应该改变
TextView txt=(TextView)context.findViewById(R.id.textView1);
到
TextView txt=(TextView)view.findViewById(R.id.textView1);
在你的 MyArrayAdapter
在 getView(.....)
在 MyArrayAdapter
class 构造函数中,您将 null
array
分配给 ArrayList<Product>
.
的 array
对象
表示尝试在 getView
方法中对空引用调用 ArrayList 的 get
方法:
public MyArrayAdapter(Activity context,
int layoutId, ArrayList<Product> arremp)
{
...
this.array=arremp;
}
注意: 为了获得更好的 ListView 性能,请使用 ViewHolder
class 来保存视图而不是再次膨胀。请参阅后续教程:
帮帮我,我为自定义 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;
}
}
class 主活动
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);
}
}
Class 产品
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 + "]";
}
}
文件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_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>
你应该改变
TextView txt=(TextView)context.findViewById(R.id.textView1);
到
TextView txt=(TextView)view.findViewById(R.id.textView1);
在你的 MyArrayAdapter
在 getView(.....)
在 MyArrayAdapter
class 构造函数中,您将 null
array
分配给 ArrayList<Product>
.
array
对象
表示尝试在 getView
方法中对空引用调用 ArrayList 的 get
方法:
public MyArrayAdapter(Activity context,
int layoutId, ArrayList<Product> arremp)
{
...
this.array=arremp;
}
注意: 为了获得更好的 ListView 性能,请使用 ViewHolder
class 来保存视图而不是再次膨胀。请参阅后续教程: