将ListView添加到Fragment的错误

时间:2014-08-25 06:15:50

标签: java android xml listview android-fragments

我正在尝试将listview添加到现有的android片段中。我为listview中的项目创建了一个布局xml文件,修改了之前的fragment.xml,编写了一个新的arrayadapter并在fragment.java中添加了一些内容。但每当我调用fragment.java中的notifyDataSetChanged()方法时,我都会收到一个错误:android.content.res.Resources $ NotFoundException:字符串资源ID#0x0,然后应用程序崩溃。请在下面找到我的密码。

layout.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:paddingBottom="7dp">

    <ImageView
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:id="@+id/iv_checkIcon"
        android:layout_margin="7dp"
        android:background="#EEEEEE"
        android:contentDescription="Provider icon"
        />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_toRightOf="@id/iv_checkIcon"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp"
        android:layout_marginLeft="5dp"
        >


        <TextView
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:id="@+id/tv_checkName"
            android:text="Provider name"
            android:textSize="20dp"
            />

        <TextView
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:id="@+id/tv_checkInDate"
            android:text="Check In Date"
            android:textSize="14dp"
            android:textColor="#888888"
            android:layout_marginTop="4dp"/>

    </LinearLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_checkPoints"
        android:layout_alignParentRight="true"
        android:text="100分"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:textSize="18dp"
        android:textColor="#CF1A12"
        />

</RelativeLayout>

fragment.xml之

<FrameLayout 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"
    tools:context=".fragment.MeActivity">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/me_bg2"
        >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/fake_head"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"
            android:id="@+id/iv_fake_head"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/iv_fake_head"
            android:text="--"
            android:textSize="22dp"
            android:textColor="#ffffff"
            android:layout_marginTop="15dp"
            android:id="@+id/tv_name"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/tv_name"
            android:text="积分"
            android:textColor="#ffffff"
            android:layout_marginTop="15dp"
            android:textSize="20dp"
            android:background="@drawable/me_points_bg"
            android:id="@+id/tv_points"
            />

    </RelativeLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv_histories"
        android:layout_gravity="bottom" />

</FrameLayout>

Arrayadapter.java

public class RecordArrayAdapter extends ArrayAdapter<CheckInRecord.CheckInRec> {
    private int resourceId;
    private Context context;
    private List<CheckInRecord.CheckInRec> checkInRecList;

    public RecordArrayAdapter(Context context, int resourceId, List<CheckInRecord.CheckInRec> checkInRecList)
    {
        super(context, resourceId, checkInRecList);
        this.resourceId = resourceId;
        this.context = context;
        this.checkInRecList = checkInRecList;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        if (convertView == null){
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            convertView = inflater.inflate(resourceId, parent, false);
        }

        TextView textViewName = (TextView) convertView.findViewById(R.id.tv_checkName);
        TextView textViewCheckInDate = (TextView) convertView.findViewById(R.id.tv_checkInDate);
        TextView textViewPoints = (TextView) convertView.findViewById(R.id.tv_checkPoints);
        ImageView imageViewIcon = (ImageView) convertView.findViewById(R.id.iv_checkIcon);

        CheckInRecord.CheckInRec checkInRec = checkInRecList.get(position);
        textViewName.setText(checkInRec.providerName);
        textViewCheckInDate.setText(checkInRec.checkInDate);
        textViewPoints.setText(checkInRec.providerPoints);
        ImageLoader.getInstance().displayImage(checkInRec.providerIcon, imageViewIcon, Utility.displayImageOptions);

        return convertView;
    }

    public int getIsPrize(int position) {return (this.checkInRecList.get(position).isPrize);}

} 

fragment.java

public class MeFragment extends Fragment implements ApiRequestDelegate {


    private TextView textViewName;
    private TextView textViewPoints;
    private ProgressDialog progressDialog;

    private RecordArrayAdapter recordArrayAdapter;
    private List<CheckInRecord.CheckInRec> checkInRecList = new ArrayList<CheckInRecord.CheckInRec>();


    public MeFragment() {
        // Required empty public constructor
    }

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

        ApiManager.getInstance().checkInHistories(AppDataManager.getInstance().getUserToken(), AppDataManager.getInstance().getUserPhone(),
                Utility.getPictureSize(), this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View fragmentView =  inflater.inflate(R.layout.fragment_me, container, false);

        textViewName = (TextView) fragmentView.findViewById(R.id.tv_name);
        textViewPoints = (TextView) fragmentView.findViewById(R.id.tv_points);

        ListView listViewRec = (ListView) fragmentView.findViewById(R.id.lv_histories);

        recordArrayAdapter = new RecordArrayAdapter(this.getActivity(), R.layout.row_record, checkInRecList);
        listViewRec.setAdapter(recordArrayAdapter);

        return fragmentView;
    }

    @Override
    public void apiCompleted(ApiResult apiResult, HttpRequest httpRequest) {
        if (progressDialog!=null){
            progressDialog.dismiss();
        }

        if (!apiResult.success){
            ApiManager.handleMessageForReason(apiResult.failReason, getActivity());
            return;
        }

        CheckInRecord checkInRecord = (CheckInRecord) apiResult.valueObject;
        if (checkInRecord != null){
            textViewName.setText(checkInRecord.userName);
            textViewPoints.setText(String.format("积分%d分", checkInRecord.userPoints));
            this.checkInRecList.clear();
            this.checkInRecList.addAll(checkInRecord.checkInRecList);

            recordArrayAdapter.notifyDataSetChanged();
        }
    }
}   

1 个答案:

答案 0 :(得分:0)

确保您设置为TextView文本的checkInRec.providerNamecheckInRec.checkInDatecheckInRec.providerPoints值的类型为String并将其转换为String 39; s整数。

// cast to String if checkInRec.checkInDate is integer
textViewPoints.setText(String.valueOf(checkInRec.providerPoints));

<强> UPD:

似乎ListView位于标题视图后面。尝试使用fragment.xml属性将FrameLayout的根视图从LinearLayout更改为android:orientation="vertical"。像这样:

<LinearLayout 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:orientation="vertical"
             tools:context=".fragment.MeActivity">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/me_bg2"
        >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/fake_head"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="40dp"
            android:id="@+id/iv_fake_head"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/iv_fake_head"
            android:text="--"
            android:textSize="22dp"
            android:textColor="#ffffff"
            android:layout_marginTop="15dp"
            android:id="@+id/tv_name"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/tv_name"
            android:text="积分"
            android:textColor="#ffffff"
            android:layout_marginTop="15dp"
            android:textSize="20dp"
            android:background="@drawable/me_points_bg"
            android:id="@+id/tv_points"
            />

    </RelativeLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv_histories"
        android:layout_gravity="bottom" />

</LinearLayout>