2013-07-28 170 views
1

我有2個自定義列表視圖,我用它來顯示不同的類別。但是,當第二個列表視圖被創建時,我從getView返回時得到這個NullPointerException。我無法弄清楚原因。getView nullPointerException自定義列表視圖Android

下面是代碼:

CustomListAdapter:

public class CustomListAdapter extends ArrayAdapter<String>{ 

    Context context; 
    String[] stringList; 
    TextView medium, small; 


    public CustomListAdapter (Context con, String[] sL) 
    { 
     super(con, R.layout.my_list, R.id.mediumTextInList, sL); 
     context = con; 

     stringList = sL; 


    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 



     LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View newRowView = inflator.inflate(R.layout.my_list, parent,false); 


     medium = (TextView) newRowView.findViewById(R.id.mediumTextInList); 
     small = (TextView) newRowView.findViewById(R.id.smallText); 

     medium.setText(stringList[position]); 

     return super.getView(position, newRowView, parent); 
    } 

}

ListActivity:

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 

     Bundle myBasket = new Bundle(); 

     myBasket.putString("name", categoryName); 

     Intent myIntent = new Intent(myFirstList.this, mySecondList.class); 

     myIntent.putExtras(myBasket); 
     startActivity(intent); 
} 

堆棧跟蹤:

07-28 17:04:46.551: E/AndroidRuntime(2290): FATAL EXCEPTION: main 
07-28 17:04:46.551: E/AndroidRuntime(2290): java.lang.NullPointerException 
07-28 17:04:46.551: E/AndroidRuntime(2290):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355) 
07-28 17:04:46.551: E/AndroidRuntime(2290):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323) 
07-28 17:04:46.551: E/AndroidRuntime(2290):  at com.umer.androiduetproject.CustomListAdapterForDeptAndSemesters.getView(CustomListAdapterForDeptAndSemesters.java:53) 
07-28 17:04:46.551: E/AndroidRuntime(2290):  at android.widget.AbsListView.obtainView(AbsListView.java:1430) 
07-28 17:04:46.551: E/AndroidRuntime(2290):  at android.widget.ListView.makeAndAddView(ListView.java:1745) 
07-28 17:04:46.551: E/AndroidRuntime(2290):  at android.widget.ListView.fillDown(ListView.java:670) 
07-28 17:04:46.551: E/AndroidRuntime(2290):  at android.widget.ListView.fillFromTop(ListView.java:727) 
07-28 17:04:46.551: E/AndroidRuntime(2290):  at android.widget.ListView.layoutChildren(ListView.java:1584) 
07-28 17:04:46.551: E/AndroidRuntime(2290):  at android.widget.AbsListView.onLayout(AbsListView.java:1260) 
07-28 17:04:46.551: E/AndroidRuntime(2290):  at android.view.View.layout(View.java:7175) 
07-28 17:04:46.551: E/AndroidRuntime(2290):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338) 
07-28 17:04:46.551: E/AndroidRuntime(2290):  at android.view.View.layout(View.java:7175) 
07-28 17:04:46.551: E/AndroidRuntime(2290):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338) 
07-28 17:04:46.551: E/AndroidRuntime(2290):  at android.view.View.layout(View.java:7175) 
07-28 17:04:46.551: E/AndroidRuntime(2290):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1140) 
07-28 17:04:46.551: E/AndroidRuntime(2290):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859) 

問候

+1

調試您的CustomListAdapterForDeptAndSemesters類的第53行。 – robotoaster

回答

2

你不應該叫super.getView,只是返回newRowView。

+0

什麼時候應該返回super.getView(),如果你想自己膨脹,我們什麼時候該返回getView –

+1

不要叫超級。特別是如果你想爲自定義葉子視圖分配值 – robotoaster

0

stringList是否有空值?可以在super.getView()中調用方法toString()

0

您可以使用這種方式。

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

    ViewHolder holder; 
    View newRowView = convertView; 

    if (convertView == null) { 
     LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     newRowView = inflator.inflate(R.layout.my_list, parent,false); 
     holder = new ViewHolder(); 
     holder.medium = (TextView) newRowView.findViewById(R.id.mediumTextInList); 
     holder.small = (TextView) newRowView.findViewById(R.id.smallText); 

     newRowView .setTag(holder); 
     } 

    else { 

      holder = (ViewHolder) newRowView .getTag(); 
      } 

     holder.medium.setText(stringList[position]); 

     return newRowView ; 
    }