2013-05-27 168 views
0

我已經在自定義佈局的應用中實現了一個列表視圖,所以我需要一個自定義的適配器。現在我的應用程序在試圖填充此列表視圖時不斷崩潰。自定義列表視圖適配器不工作

這是我的活動:

public class PersonalResultsActivity extends Activity { 
     private PersonalResultsAdapter resultListAdapter; 
     private ListView resultListView; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      setContentView(R.layout.personal_results); 
      resultListView = (ListView)findViewById(R.id.pers_res_list); 

      resultListAdapter = new PersonalResultsAdapter(this); 
      resultListView.setAdapter(resultListAdapter); 
     } 
    } 

這是我的適配器類:

public class PersonalResultsAdapter extends ArrayAdapter<Result> { 
     private Context context; 
     private List<Result> results; 

     private LayoutInflater mInflater; 
     private boolean mNotifyOnChange = true; 

     private String username; 

     private Student student; 
     private TestData test; 

     public PersonalResultsAdapter(Context context) { 
      super(context, R.layout.result_item_row); 

      test = LoginActivity.testData; 
      username = LoginActivity.loadPreferences(context); 
      student = test.getStudent(username); 

      results = student.getResults(); 

      this.context = context; 
      this.mInflater = LayoutInflater.from(context); 
     } 

     @Override 
     public int getCount() { 
      return results .size(); 
     } 

     @Override 
     public Result getItem(int position) { 
      return results .get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public int getPosition(Result item) { 
      return results .indexOf(item); 
     } 

     @Override 
     public int getViewTypeCount() { 
      return 1; //Number of types + 1 !!!!!!!! 
     } 

     @Override 
     public int getItemViewType(int position) { 
      return 1; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      final ViewHolder holder; 
      int type = getItemViewType(position); 
      if (convertView == null) { 
       holder = new ViewHolder(); 
       switch (type) { 
        case 1: 
         convertView = mInflater.inflate(R.layout.result_item_row,parent, false); 
         holder.date = (TextView) convertView.findViewById(R.id.pers_res_date); 
         holder.meeting = (TextView) convertView.findViewById(R.id.pers_res_meet); 
         holder.points = (TextView) convertView.findViewById(R.id.pers_res_points); 
         break; 
       } 
       convertView.setTag(holder); 
      } else { 
       holder = (ViewHolder) convertView.getTag(); 
      } 

      Time temp = results.get(position).getDate(); 
      String strDate = temp.year + "/" + temp.month + "/" + temp.monthDay; 

      holder.date.setText(strDate); 
      holder.meeting.setText(results.get(position).getMeeting().getName()); 
      holder.points.setText(results.get(position).getScore()); 
      holder.pos = position; 

      return convertView; 
     } 

     @Override 
     public void notifyDataSetChanged() { 
      super.notifyDataSetChanged(); 
      mNotifyOnChange = true; 
     } 

     public void setNotifyOnChange(boolean notifyOnChange) { 
      mNotifyOnChange = notifyOnChange; 
     } 

     static class ViewHolder { 
      TextView date; 
      TextView meeting; 
      TextView points; 
      int pos; //to store the position of the item within the list 
     } 
    } 

佈局:ListView項的

 </TableRow> 
     </TableLayout> 
     <ListView 
       android:id="@+id/pers_res_list" 
       android:background="@drawable/border" 
       android:layout_height="280dp" 
       android:layout_width="250dp" 
       android:layout_gravity="center"/> 
    </LinearLayout> 

自定義佈局:

<?xml version="1.0" encoding="utf-8"?> 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/pers_res_view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:gravity="center" 
      android:background="@color/background" 
      android:layout_gravity="center"> 

     <TextView 
       android:id="@+id/pers_res_date" 
       android:textSize="16dp" 
       android:gravity="center" 
       android:background="@drawable/border" 
       android:layout_width="100dp" 
       android:layout_height="40dp"/> 
     <TextView 
       android:id="@+id/pers_res_meet" 
       android:textSize="16dp" 
       android:gravity="center" 
       android:background="@drawable/border" 
       android:layout_width="75dp" 
       android:layout_height="40dp" 
       android:layout_toRightOf="@id/pers_res_date"/> 
     <TextView 
       android:id="@+id/pers_res_points" 
       android:textSize="16dp" 
       android:gravity="center" 
       android:background="@drawable/border" 
       android:layout_width="75dp" 
       android:layout_height="40dp" 
       android:layout_toRightOf="@id/pers_res_meet"/> 
    </RelativeLayout> 

的logcat:

05-27 13:13:44.608: E/AndroidRuntime(15189): FATAL EXCEPTION: main 
    05-27 13:13:44.608: E/AndroidRuntime(15189): android.content.res.Resources$NotFoundException: String resource ID #0x1a9 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.content.res.Resources.getText(Resources.java:247) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.widget.TextView.setText(TextView.java:3652) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at com.isf.SchoolCup.Adapter.PersonalResultsAdapter.getView(PersonalResultsAdapter.java:114) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.widget.AbsListView.obtainView(AbsListView.java:2216) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.widget.ListView.makeAndAddView(ListView.java:1774) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.widget.ListView.fillDown(ListView.java:672) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.widget.ListView.fillFromTop(ListView.java:732) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.widget.ListView.layoutChildren(ListView.java:1625) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.widget.AbsListView.onLayout(AbsListView.java:2046) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.view.View.layout(View.java:11418) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.view.ViewGroup.layout(ViewGroup.java:4224) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.view.View.layout(View.java:11418) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.view.ViewGroup.layout(ViewGroup.java:4224) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.widget.FrameLayout.onLayout(FrameLayout.java:431) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.view.View.layout(View.java:11418) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.view.ViewGroup.layout(ViewGroup.java:4224) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.view.View.layout(View.java:11418) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.view.ViewGroup.layout(ViewGroup.java:4224) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.widget.FrameLayout.onLayout(FrameLayout.java:431) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.view.View.layout(View.java:11418) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.view.ViewGroup.layout(ViewGroup.java:4224) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1628) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2585) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.os.Handler.dispatchMessage(Handler.java:99) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.os.Looper.loop(Looper.java:137) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at android.app.ActivityThread.main(ActivityThread.java:4507) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at java.lang.reflect.Method.invokeNative(Native Method) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at java.lang.reflect.Method.invoke(Method.java:511) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 
    05-27 13:13:44.608: E/AndroidRuntime(15189): at dalvik.system.NativeStart.main(Native Method) 
    05-27 13:13:44.633: E/android.os.Debug(5871): [email protected] > dumpstate -k -t -n -z -d -o /data/log/dumpstate_app_error 
    05-27 13:13:44.658: E/GlsClient-query(8461): requestFailed 
+1

你有一些logcat? –

+0

是啊,對不起,我忘了 –

+0

PersonalResultsAdapter中的第114行是什麼? –

回答

1

越來越NullPointerException我猜你在下面一行出現錯誤:

holder.points.setText(results.get(position).getScore()); 

試試這個:

holder.points.setText(Integer.toString(results.get(position).getScore())); 

出現此錯誤的原因setText(int rescourceID)要求一個recource id ...並且給出一個不在R.java中的隨機int。

+0

這樣做了!我收到一個整數,它必須是一個字符串。謝謝! –

+0

很高興。只要它是有效的資源ID,您也可以給出一個int。 –

0

您have'nt初始化對象getView()convertView爲空。 這就是爲什麼你會在這條線

holder.date = (TextView) convertView.findViewById(R.id.pers_res_date); 
+0

我在getView()的開頭初始化了持有者。還是一樣的錯誤.. –

相關問題