Java 将ListView添加到片段时出错
我正在尝试将listview添加到现有的android片段中。我为listview中的项目创建了一个布局xml文件,修改了前面的fragment.xml,编写了一个新的arrayadapter,并向fragment.java添加了一些内容。但是每当我在fragment.java中调用notifyDataSetChanged()方法时,我都会得到一个错误:android.content.res.Resources$NotFoundException:String resource ID#0x0,然后应用程序崩溃。请在下面找到我的代码Java 将ListView添加到片段时出错,java,android,xml,listview,android-fragments,Java,Android,Xml,Listview,Android Fragments,我正在尝试将listview添加到现有的android片段中。我为listview中的项目创建了一个布局xml文件,修改了前面的fragment.xml,编写了一个新的arrayadapter,并向fragment.java添加了一些内容。但是每当我在fragment.java中调用notifyDataSetChanged()方法时,我都会得到一个错误:android.content.res.Resources$NotFoundException:String resource 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>
layout.xml
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);}
}
公共类RecordArrayAdapter扩展了ArrayAdapter{
私有资源ID;
私人语境;
私有列表检查列表;
公共记录ArrayAdapter(上下文上下文、int-resourceId、列表checkInRecList)
{
超级(上下文、资源ID、签入列表);
this.resourceId=resourceId;
this.context=上下文;
this.checkInRecList=checkInRecList;
}
公共视图getView(int位置、视图转换视图、视图组父视图)
{
if(convertView==null){
LayoutInflater充气器=((活动)上下文)。getLayoutInflater();
convertView=充气机。充气(resourceId,父项,false);
}
TextView textViewName=(TextView)convertView.findViewById(R.id.tv_checkName);
TextView TextView checkInDate=(TextView)convertView.findViewById(R.id.tv_checkInDate);
TextView textViewPoints=(TextView)convertView.findViewById(R.id.tv_检查点);
ImageView imageViewIcon=(ImageView)convertView.findViewById(R.id.iv_复选图标);
CheckInRecord.CheckInRec CheckInRec=checkInRecList.get(位置);
textViewName.setText(checkInRec.providerName);
text查看checkInDate.setText(checkInRec.checkInDate);
textViewPoints.setText(checkInRec.providerPoints);
ImageLoader.getInstance().displayImage(checkInRec.providerIcon、imageViewIcon、Utility.displayImageOptions);
返回视图;
}
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();
}
}
}
公共类MeFragment扩展片段实现APIRestDelegate{
私有文本视图文本视图名称;
私有文本视图文本视点;
私有进程对话;
私有RecordArrayAdapter RecordArrayAdapter;
私有列表checkInRecList=new ArrayList();
公共MeFragment(){
//必需的空公共构造函数
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
ApiManager.getInstance().checkInHistories(AppDataManager.getInstance().getUserToken(),AppDataManager.getInstance().getUserPhone(),
Utility.getPictureSize(),此选项);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
//为该碎片膨胀布局
视图碎片视图=充气机。充气(R.layout.fragment\u me,容器,false);
textViewName=(TextView)fragmentView.findViewById(R.id.tv_name);
textViewPoints=(TextView)fragmentView.findViewById(R.id.tv_点);
ListView listViewRec=(ListView)fragmentView.findViewById(R.id.lv_历史);
recordArrayAdapter=新的recordArrayAdapter(this.getActivity(),R.layout.row_记录,checkInRecList);
listViewRec.setAdapter(recordArrayAdapter);
返回碎片视图;
}
@凌驾
公共无效apiCompleted(ApiResult ApiResult,HttpRequest HttpRequest){
如果(progressDialog!=null){
progressDialog.disclose();
}
如果(!apireult.success){
ApiManager.handleMessageForReason(apiResult.failReason,getActivity());
返回;
}
CheckInRecord CheckInRecord=(CheckInRecord)apisult.valueObject;
如果(checkInRecord!=null){
textViewName.setText(checkInRecord.userName);
textViewPoints.setText(String.format(“积分%D分", checkInRecord.userPoints);
this.checkInRecList.clear();
this.checkInRecList.addAll(checkInRecord.checkInRecList);
recordArrayAdapter.notifyDataSetChanged();
}
}
}
确保设置为文本视图文本的checkInRec.providerName
、checkInRec.checkInDate
和checkInRec.providerPoints
值的类型为String
,如果是整数,则将其强制转换为String
// cast to String if checkInRec.checkInDate is integer
textViewPoints.setText(String.valueOf(checkInRec.providerPoints));
UPD:
ListView似乎位于标题视图后面。请尝试使用android:orientation=“vertical”
属性将fragment.xml
的根视图从FrameLayout
更改为LinearLayout
。如下所示:
<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>
请向我们显示完整的错误log@Tejas谢谢,我已经修好了很高兴你修好了,谢谢。但是列表是空的,你有什么想法吗?嗨,我找不到你的代码和我的代码之间的区别。实际上,项目数与服务器中的记录数相同,但只是每个项目都有空文本views和GRY imagesOops…这是格式化问题,所以解析器使用了根标记。我刚刚修复了它。请参阅代码。您确定从服务器和解析获得的数据有效吗?调试您的代码,并查看您在getView
方法中为TextView设置的值。您好,我想适配器中可能有问题,因此如何确定whet请告诉她getView中的对象是否有效。如果您没有时间解决我的问题,那就好了