启动应用程序时出错,我创建了数据模型,适配器,此适配器的行,创建了listview并将此适配器设置为它们。我不明白问题是什么,以及我必须做些什么来避免这种情况。 我有适配器:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class ListViewAdapter extends ArrayAdapter<Model> {
private final Context context;
private final ArrayList<Model> modelsArrayList;
public ListViewAdapter(Context context, ArrayList<Model> modelsArrayList) {
super(context, R.layout.row_listview, modelsArrayList);
this.context = context;
this.modelsArrayList = modelsArrayList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
View rowView = null;
if(!modelsArrayList.get(position).isGroupHeader()){
rowView = inflater.inflate(R.layout.row_listview, parent, false);
// 3. Get icon,title & counter views from the rowView
ImageView imgView = (ImageView) rowView.findViewById(R.id.iconlistview);
TextView titleView = (TextView) rowView.findViewById(R.id.listview_textview_name);
TextView counterView = (TextView) rowView.findViewById(R.id.listview_textview_time);
// 4. Set the text for textView
imgView.setImageResource(modelsArrayList.get(position).getIcon());
titleView.setText(modelsArrayList.get(position).getTitle());
counterView.setText(modelsArrayList.get(position).getCounter());
}
else{
rowView = inflater.inflate(R.layout.group_header_item, parent, false);
TextView titleView = (TextView) rowView.findViewById(R.id.header);
titleView.setText(modelsArrayList.get(position).getTitle());
}
// 5. retrn rowView
return rowView;
}
}
此适配器的行:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/relativelayoutfootprints"
>
<ImageView
android:layout_marginLeft="30dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/iconlistview"
android:layout_centerVertical="true"
>
</ImageView>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="@id/iconlistview"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:id="@+id/listview_textview_name"
android:textColor="@color/white"
android:textSize="18sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:id="@+id/listview_textview_time"
android:layout_centerVertical="true"
android:layout_marginRight="60dp"
android:textSize="20sp"
android:textColor="@color/white"
>
</TextView>
</RelativeLayout>
和型号:
public class Model{
private int icon;
private String title;
private String counter;
private boolean isGroupHeader = false;
private boolean groupHeader = false;
public void setIsGroupHeader(boolean isGroupHeader) {
this.isGroupHeader = isGroupHeader;
}
public Model(String title) {
this(-1,title,null);
isGroupHeader = true;
}
public Model(int icon, String title, String counter) {
super();
this.icon = icon;
this.title = title;
this.counter = counter;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCounter() {
return counter;
}
public void setCounter(String counter) {
this.counter = counter;
}
public boolean isGroupHeader() {
return groupHeader;
}
}
但是当我开始应用时,我有错误:
03-22 20:35:39.254 7044-7044/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 7044
android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
at android.content.res.Resources.getValue(Resources.java:1351)
at android.content.res.Resources.getDrawable(Resources.java:804)
at android.content.Context.getDrawable(Context.java:458)
at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:26)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:321)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:197)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)
at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:66)
at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:71)
at com.example.myapplication.ListViewAdapter.getView(ListViewAdapter.java:46)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.makeAndAddView(ListView.java:1875)
at android.widget.ListView.fillDown(ListView.java:702)
at android.widget.ListView.fillFromTop(ListView.java:763)
at android.widget.ListView.layoutChildren(ListView.java:1684)
at android.widget.AbsListView.onLayout(AbsListView.java:2148)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.mai
主要错误符合:
imgView.setImageResource(modelsArrayList.get(position).getIcon());
初始化数据:
ListViewRecent = (ListView) v.findViewById(R.id.ListView_Recent);
ListViewAdapter adapter = new ListViewAdapter(getActivity(),generateData());
ListViewRecent.setAdapter(adapter);
private ArrayList<Model> generateData(){
ArrayList<Model> models = new ArrayList<Model>();
models.add(new Model("Recent Updates"));
models.add(new Model(R.drawable.emilypic,"Menu Item 1","1"));
models.add(new Model(R.drawable.emilypic,"Menu Item 2","2"));
models.add(new Model(R.drawable.emilypic,"Menu Item 3","12"));
return models;
}
答案 0 :(得分:2)
Resources$NotFoundException: Resource ID #0xffffffff
这是-1
的十六进制,它是从modelsArrayList.get(position).getIcon()
返回的,并且是在构造函数中设置的值。
从你的片段中,你称之为
models.add(new Model("Recent Updates"));
进入
// Are these meant to be one value??
private boolean isGroupHeader = false;
private boolean groupHeader = false;
public Model(String title) {
this(-1,title,null); // this line
isGroupHeader = true;
}
public Model(int icon, String title, String counter) {
super();
this.icon = icon; // this gets a -1
this.title = title;
this.counter = counter;
}
public int getIcon() {
return icon; // this is causing the error
}
public boolean isGroupHeader() {
return groupHeader; // this is always false...
}
所以你的问题是总是输入这个if语句,因为该布尔值总是false
而-1
是资源。您的错误不在emilyPic
上。
if(!modelsArrayList.get(position).isGroupHeader()){
rowView = inflater.inflate(R.layout.row_listview, parent, false);
ImageView imgView = (ImageView) rowView.findViewById(R.id.iconlistview);
您可以通过仅使用groupHeader
布尔值中的一个来解决此问题。