我想設置一個適配器這種方式來顯示一個ListView:錯誤:NullPointerException異常
public class DrugAdapter extends ArrayAdapter {
Context context;
Resources resources;
private List<DrugModel> drug;
private LayoutInflater myInflater;
public DrugAdapter(Context context, ArrayList<DrugModel> listOfDrugs) {
super(context, R.layout.activity_main, listOfDrugs);
this.context = context;
this.drug = listOfDrugs;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
resources = context.getResources();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.activity_main, parent, false);
TextView name = (TextView) convertView.findViewById(R.id.list_item_name);
TextView description = (TextView) convertView.findViewById(R.id.list_item_description);
name.setText(drug.get(position).getName()); // HERE IS WHERE THE ERROR MESSAGE OCCURS
Typeface tf = Typeface.createFromAsset(context.getAssets(), "arial.ttf");
name.setTypeface(tf);
description.setText(drug.get(position).getDescription());
return convertView;
}
}
但我收到此錯誤信息:
FATAL EXCEPTION: main java.lang.NullPointerException at com.mypharmacy.adapters.DrugAdapter.getView(DrugAdapter.java:47) at android.widget.AbsListView.obtainView(AbsListView.java:2465) at android.widget.ListView.makeAndAddView(ListView.java:1775) at android.widget.ListView.fillDown(ListView.java:678)
activity_main.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.mypharmacy.app.MainActivity">
<EditText
android:id="@+id/search_drugs_edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/search_for_a_drug"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<requestFocus/>
</EditText>
<ListView
android:id="@+id/list_of_drugs"
android:layout_below="@id/search_drugs_edittext"
android:layout_width="fill_parent"
android:layout_height="425dp"
android:minHeight="?android:attr/listPreferredItemHeight" />
</RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:minHeight="70dp" >
<TextView
android:id="@+id/list_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name: init value"
android:textColor="#008019"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/list_item_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="description: init value"
android:textColor="#0080A9"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_below="@+id/list_item_name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="54dp"
android:layout_marginTop="50dp" />
</RelativeLayout>
用哪個我無法弄清楚什麼是錯的。請問如何解決這個問題?爲什麼name
變量取值爲Null?
檢查在drug.get(位置)的值.getName()我認爲它是空的 – 2014-09-11 08:14:56
如果'name'爲空,則表示沒有視圖在佈局'activity_main'中發現id爲'list_item_name'。你能向我們展示佈局嗎? – 2014-09-11 08:15:15
你需要進行調試以確定'name'是否爲null或'drug.get(position)'爲null – 2014-09-11 08:19:01