我在我的应用中使用自定义布局实现了listview,因此我需要一个自定义适配器。现在我的应用程序在尝试填充此列表视图时一直在崩溃。
这是我的活动:
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
}
}
布局:
</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>
listview项目的自定义布局:
<?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): !@Dumpstate > dumpstate -k -t -n -z -d -o /data/log/dumpstate_app_error
05-27 13:13:44.658: E/GlsClient-query(8461): requestFailed
答案 0 :(得分:1)
我猜您会在以下行收到错误:
holder.points.setText(results.get(position).getScore());
试试这个:
holder.points.setText(Integer.toString(results.get(position).getScore()));
出现错误是因为setText(int rescourceID)
要求提供资源ID ...并且您提供的随机int不在R.java
中。
答案 1 :(得分:0)
当getView()
为空时,您尚未在convertView
中初始化持有者对象。
这就是为什么你可能会在这一行上获得NullPointerException
holder.date = (TextView) convertView.findViewById(R.id.pers_res_date);