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
        }
     }

布局:

        </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>

日志猫:

    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
4

2 回答 2

1

我猜你在以下行得到错误:

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

试试这个:

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

出现错误是因为setText(int rescourceID)要求提供资源 ID ...并且您给出了一个随机 int ,它不在R.java.

于 2013-05-27T11:37:35.563 回答
0

当为空时,您还没有初始化持有者对象。这就是为什么你可能会在这条线上getView()convertViewNullPointerException

holder.date = (TextView) convertView.findViewById(R.id.pers_res_date);
于 2013-05-27T11:18:01.843 回答