注意:我提到了类似的问题但仍然找不到任何错误
的NullPointerException
listView.addHeaderView(header);
和
listView.setAdapter(adapter);
详细代码:
Veg v = new Veg();
v.setId(1);
v.setName("some");
v.setPrice(15.0);
List<Veg> vegData = new ArrayList<Veg>();
// dummy add for testing
vegData.add(v);
vegData.add(v);
vegData.add(v);
CustomAdapter adapter = new CustomAdapter(this,
R.layout.custom_list_rows, vegData);
listView = (ListView) findViewById(R.id.listView1);
View header = getLayoutInflater().inflate(
R.layout.custom_list_header, null);
listView.addHeaderView(header);
listView.setAdapter(adapter);
getView()部分
public class CustomAdapter extends ArrayAdapter<Veg> {
Context context;
int layoutResourceId;
List<Veg> data;
public CustomAdapter(Context context, int resource, List<Veg> data) {
super(context, resource);
this.context = context;
this.layoutResourceId = resource;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
CustomHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new CustomHolder();
holder.at = (TextView) row.findViewById(R.id.customheaderbutton1);
holder.bt = (TextView) row.findViewById(R.id.customheaderbutton2);
holder.ct = (TextView) row.findViewById(R.id.customheaderbutton3);
row.setTag(holder);
} else {
holder = (CustomHolder) row.getTag();
}
holder.at.setText(String.valueOf(data.get(position).getId()));
holder.bt.setText(data.get(position).getName());
holder.ct.setText(String.valueOf(data.get(position).getPrice()));
return row;
}
static class CustomHolder {
TextView at;
TextView bt;
TextView ct;
}
}
主要活动xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
custom_list_header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/customheaderbutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:text="marketname" />
<Button
android:id="@+id/customheaderbutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="edit" />
<Button
android:id="@+id/customheaderbutton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="+" />
</LinearLayout>
custom_list_rows.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/elementIDtext"
android:layout_width="11dp"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/elementName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.30"
android:text="@string/elementName"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/elementSummary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/elementValue"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="62dp"
android:layout_height="wrap_content"
android:text="@string/_" />
</LinearLayout>
答案 0 :(得分:1)
listView
似乎null
,这意味着findViewById()
返回了null
:它无法找到您正在寻找的控件。如果没有看到您的布局,就无法进一步调试,但请确保R.id.listView1
指的是有效元素。
答案 1 :(得分:1)
在getView中,更改
holder.at = (TextView) row.findViewById(R.id.customheaderbutton1);
holder.bt = (TextView) row.findViewById(R.id.customheaderbutton2);
holder.ct = (TextView) row.findViewById(R.id.customheaderbutton3);
到
holder.at = (TextView) row.findViewById(R.id.elementIDtext);
holder.bt = (TextView) row.findViewById(R.id.elementName);
holder.ct = (TextView) row.findViewById(R.id.elementSummary);
答案 2 :(得分:0)
用以下代码替换您的代码:
package com.example.listview;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class List extends Activity
{
private ListView listview;
Veg Veg = new Veg();
ArrayList<Veg> data = new ArrayList<Veg>();
CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
listview = (ListView)findViewById(R.id.listView);
for(int i=1;i<101;i++)
{
Veg = new Veg();
Veg.setName("Item "+i);
Veg.setID("ID "+i);
Veg.setPrice("Price "+i);
data.add(Veg);
}
View headerView = getLayoutInflater().inflate(
R.layout.custom_list_header, null);
listview.addHeaderView(headerView);
adapter = new CustomAdapter(this,R.layout.custom_list_rows,data);
listview.setAdapter(adapter);
}
class CustomAdapter extends ArrayAdapter<Veg>
{
Context con;
int Res;
ArrayList<Veg> data;
public CustomAdapter(Context context, int resource, ArrayList<Veg> data) {
super(context, resource,data);
// TODO Auto-generated constructor stub
con = context;
Res = resource;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
CustomHolder holder = null;
if (row == null) {
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(Res, parent, false);
holder = new CustomHolder();
holder.at = (TextView) row.findViewById(R.id.elementIDtext);
holder.bt = (TextView) row.findViewById(R.id.elementName);
holder.ct = (TextView) row.findViewById(R.id.elementSummary);
row.setTag(holder);
} else {
holder = (CustomHolder) row.getTag();
}
holder.at.setText(String.valueOf(data.get(position).getID()));
holder.bt.setText(data.get(position).getName());
holder.ct.setText(String.valueOf(data.get(position).getPrice()));
return row;
}
class CustomHolder {
TextView at;
TextView bt;
TextView ct;
}
}
}
此外,您必须更改您的XML,如下所示,而不是像11 dp那样给出静态宽度。
<强> custom_list_rows.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/elementIDtext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ID"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/elementName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="elementName"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/elementSummary"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="elementValue"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="btn" />
</LinearLayout>