2014-09-11 167 views
-1

我想設置一個適配器這種方式來顯示一個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?

+0

檢查在drug.get(位置)的值.getName()我認爲它是空的 – 2014-09-11 08:14:56

+0

如果'name'爲空,則表示沒有視圖在佈局'activity_main'中發現id爲'list_item_name'。你能向我們展示佈局嗎? – 2014-09-11 08:15:15

+0

你需要進行調試以確定'name'是否爲null或'drug.get(position)'爲null – 2014-09-11 08:19:01

回答

0

我認爲你的問題在這一行。

convertView = inflater.inflate(R.layout.activity_main, parent, false); 

你可能誇大了錯誤的佈局,如果是這樣的情況下,即使你把這個行:

convertView.findViewById(R.id.list_item_name); 

R.id.list_item_name是有效的,該ID是不是在該佈局。如果您致電findViewById誇大了不同的佈局,您將收到一個空項目。

希望它可以幫助

+0

我剛剛更新了我的文章,發佈了'activity_main.xml'和'list_item.xml'。 – user3797031 2014-09-11 08:32:24

相關問題