如何为列表视图创建自定义适配器?获取 ResourceNotFoundException
How to create a custom adapter for a listview? Getting RessourceNotFoundException
我正在尝试为我的列表视图制作自定义适配器,但我一直在同一行收到 RessourceNotFoundException。
首先,这是我的适配器 class 代码:
public class CreateProgramAdapter extends BaseAdapter {
private final LayoutInflater inflater;
private ArrayList<TrainingPass> trainingPasses;
private List<TrainingPass> items;
private int layoutResourceId;
private Context context;
public CreateProgramAdapter(Context context, List<TrainingPass> items) {
this.context = context;
this.items = items;
inflater = LayoutInflater.from(this.context);
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
TrainingPassHolder holder;
if(convertView == null) {
convertView = inflater.inflate(R.layout.adapter_createprogram_trainingpasses_item, null);
holder = new TrainingPassHolder();
convertView.setTag(holder);
} else {
holder = (TrainingPassHolder) convertView.getTag();
}
holder.trainingPass = this.items.get(position);
holder.trainingPassRelativeLayout = (RelativeLayout) row.findViewById(R.id.trainingpass_item_relativelayout);
holder.headlineTxt = (TextView) row.findViewById(R.id.trainingpass_headline);
holder.setNoTxt = (TextView) row.findViewById(R.id.txt_trainingpass_setno);
holder.showHideIcon = (ImageView) row.findViewById(R.id.trainingpass_showhide_imageview);
holder.exerciseSearchField = (EditText) row.findViewById(R.id.trainingpass_exercise_searchfield);
holder.repsTextField = (EditText) row.findViewById(R.id.trainingpass_reps_txtfield);
holder.addSetButton = (Button) row.findViewById(R.id.add_set_button);
holder.addExerciseButton = (Button) row.findViewById(R.id.trainingpass_addexercise_button);
holder.setsListView = (ListView) row.findViewById(R.id.trainingpass_sets_listview);
holder.exercisesListView = (ListView) row.findViewById(R.id.trainingpass_exercises_listview);
holder.trainingPassRelativeLayout.setTag(holder.trainingPass);
holder.headlineTxt.setTag(holder.trainingPass);
holder.setNoTxt.setTag(holder.trainingPass);
holder.showHideIcon.setTag(holder.trainingPass);
holder.exerciseSearchField.setTag(holder.trainingPass);
holder.repsTextField.setTag(holder.trainingPass);
holder.addSetButton.setTag(holder.trainingPass);
holder.addExerciseButton.setTag(holder.trainingPass);
holder.setsListView.setTag(holder.trainingPass);
holder.exercisesListView.setTag(holder.trainingPass);
setupItems(holder);
return convertView;
}
private void setupItems(TrainingPassHolder trainingPassHolder) {
// TODO: set default values for items
}
public static class TrainingPassHolder {
TrainingPass trainingPass;
RelativeLayout trainingPassRelativeLayout;
TextView headlineTxt;
TextView setNoTxt;
ImageView showHideIcon;
EditText exerciseSearchField;
EditText repsTextField;
Button addSetButton;
Button addExerciseButton;
ListView setsListView;
ListView exercisesListView;
}
}
这里是视图的 xml,包含列表视图:
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.mobileplatformexam.activities.CreateProgramActivity"
android:id="@+id/relativeLayoutCreateProgram">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add training pass"
android:id="@+id/btn_add_trainingpass"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/btn_add_trainingpass_help"
android:layout_toStartOf="@+id/btn_add_trainingpass_help"
android:onClick="addTrainingPass"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\?"
android:id="@+id/btn_add_trainingpass_help"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="addTrainingPassHelp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create program"
android:id="@+id/btn_create_program"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/btn_add_trainingpass_help"
android:layout_alignEnd="@+id/btn_add_trainingpass_help"
android:onClick="createProgram"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView_trainingpasses"
android:layout_above="@+id/btn_create_program"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/btn_add_trainingpass" />
</RelativeLayout>
以及具有上述列表视图项目布局的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/trainingpass_item_relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/trainingpass_headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_alignParentTop="true"
android:text="Trainingpass"
android:textSize="26dp"
android:textColor="@android:color/white"
android:background="@android:color/holo_blue_dark"
android:height="50dp"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:layout_toLeftOf="@+id/trainingpass_showhide_imageview"
android:layout_toStartOf="@+id/trainingpass_showhide_imageview"
android:textIsSelectable="true"
android:textStyle="bold" />
<ImageView
android:id="@+id/trainingpass_showhide_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="@drawable/abc_spinner_mtrl_am_alpha"
android:layout_alignParentTop="true"
android:background="@android:color/holo_blue_dark"
android:minWidth="50dp"
android:minHeight="50dp"
android:layout_alignBottom="@+id/trainingpass_headline" />
<EditText
android:id="@+id/trainingpass_exercise_searchfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/trainingpass_headline"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/trainingpass_showhide_imageview"
android:layout_alignEnd="@+id/trainingpass_showhide_imageview"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:hint="Name of exercise"
android:padding="15dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/txt_trainingpass_setno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set 1"
android:textSize="20dp"
android:textColor="@android:color/black"
android:paddingRight="30dp"
android:paddingLeft="10dp"
android:paddingTop="20dp"
android:paddingBottom="10dp"
android:layout_below="@+id/trainingpass_exercise_searchfield"
android:layout_toLeftOf="@+id/trainingpass_reps_txtfield"
android:layout_toStartOf="@+id/trainingpass_reps_txtfield" />
<EditText
android:id="@+id/trainingpass_reps_txtfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Reps"
android:padding="15dp"
android:width="160dp"
android:layout_alignBottom="@+id/txt_trainingpass_setno"
android:layout_toLeftOf="@+id/add_set_button"
android:layout_toStartOf="@+id/add_set_button"
android:layout_marginRight="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add_set_button"
android:text="Add set"
android:layout_alignBottom="@+id/trainingpass_reps_txtfield"
android:layout_alignRight="@+id/trainingpass_exercise_searchfield"
android:layout_alignEnd="@+id/trainingpass_exercise_searchfield"
android:layout_below="@+id/trainingpass_exercise_searchfield" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/trainingpass_sets_listview"
android:layout_alignLeft="@+id/trainingpass_exercise_searchfield"
android:layout_alignStart="@+id/trainingpass_exercise_searchfield"
android:layout_below="@+id/txt_trainingpass_setno" />
<Button
android:text="Add exercise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/trainingpass_addexercise_button"
android:layout_below="@+id/trainingpass_sets_listview"
android:layout_alignRight="@+id/add_set_button"
android:layout_alignEnd="@+id/add_set_button"
android:layout_alignLeft="@+id/trainingpass_sets_listview"
android:layout_alignStart="@+id/trainingpass_sets_listview" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/trainingpass_exercises_listview"
android:layout_below="@+id/trainingpass_addexercise_button"
android:layout_alignLeft="@+id/trainingpass_addexercise_button"
android:layout_alignStart="@+id/trainingpass_addexercise_button" />
</RelativeLayout>
</LinearLayout>
这是在我的 activity 中设置适配器的代码(扩展 Activity)
ArrayList<TrainingPass> trainingPasses = new ArrayList<TrainingPass>();
TrainingPass trainingPass = new TrainingPass();
trainingPass.setTrainingPassName("wefwef");
TrainingPass trainingPass2 = new TrainingPass();
trainingPass.setTrainingPassName("rwefhtr");
trainingPasses.add(trainingPass);
trainingPasses.add(trainingPass2);
ListView trainingPassesListView = (ListView)findViewById(R.id.listView_trainingpasses);
CreateProgramAdapter createProgramAdapter = new CreateProgramAdapter(CreateProgramActivity.this, trainingPasses);
trainingPassesListView.setAdapter(createProgramAdapter);
我得到的错误是:
12661-12661/com.mobileplatformexam E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: Resource ID #0x7f04001a
at android.content.res.Resources.getValue(Resources.java:1019)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2107)
at android.content.res.Resources.getLayout(Resources.java:858)
at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.mobileplatformexam.Adapters.CreateProgramAdapter.getView(CreateProgramAdapter.java:56)
at android.widget.AbsListView.obtainView(AbsListView.java:2466)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
at android.widget.ListView.onMeasure(ListView.java:1155)
at android.view.View.measure(View.java:12807)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:642)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:424)
at android.view.View.measure(View.java:12807)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12807)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
at android.view.View.measure(View.java:12807)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12807)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1399)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:690)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:583)
at android.view.View.measure(View.java:12807)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2218)
at android.view.View.measure(View.java:12807)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1068)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2452)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
at dalvik.system.NativeStart.main(Native Method)
错误指的是我的适配器class中的第56行,也就是下面一行:
convertView = inflater.inflate(R.layout.adapter_createprogram_trainingpasses_item, null);
我真的找不到代码可能有什么问题,所以我希望有人能帮助我。
谢谢
感谢 JafarKhQ,我解决了这个问题。
通过将布局 "adapter_createprogram_trainingpasses_item .xml" 更改为以下内容,它起作用了:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="@+id/trainingpass_headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_alignParentTop="true"
android:text="Trainingpass"
android:textSize="20dp"
android:textColor="@android:color/white"
android:background="@android:color/holo_blue_dark"
android:height="30dp"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:layout_toLeftOf="@+id/trainingpass_showhide_imageview"
android:layout_toStartOf="@+id/trainingpass_showhide_imageview"
android:textIsSelectable="true"
android:textStyle="bold" />
<ImageView
android:id="@+id/trainingpass_showhide_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="@drawable/abc_spinner_mtrl_am_alpha"
android:layout_alignParentTop="true"
android:background="@android:color/holo_blue_dark"
android:height="30dp"
android:minWidth="50dp"
android:layout_alignBottom="@+id/trainingpass_headline" />
<EditText
android:id="@+id/trainingpass_exercise_searchfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/trainingpass_headline"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/trainingpass_showhide_imageview"
android:layout_alignEnd="@+id/trainingpass_showhide_imageview"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:hint="Name of exercise"
android:padding="15dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/txt_trainingpass_setno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set 1"
android:textSize="20dp"
android:textColor="@android:color/black"
android:paddingRight="30dp"
android:paddingLeft="10dp"
android:paddingTop="20dp"
android:paddingBottom="10dp"
android:layout_below="@+id/trainingpass_exercise_searchfield"
android:layout_toLeftOf="@+id/trainingpass_reps_txtfield"
android:layout_toStartOf="@+id/trainingpass_reps_txtfield" />
<EditText
android:id="@+id/trainingpass_reps_txtfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Reps"
android:padding="15dp"
android:width="160dp"
android:layout_alignBottom="@+id/txt_trainingpass_setno"
android:layout_toLeftOf="@+id/add_set_button"
android:layout_toStartOf="@+id/add_set_button"
android:layout_marginRight="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add_set_button"
android:text="Add set"
android:layout_alignBottom="@+id/trainingpass_reps_txtfield"
android:layout_alignRight="@+id/trainingpass_exercise_searchfield"
android:layout_alignEnd="@+id/trainingpass_exercise_searchfield"
android:layout_below="@+id/trainingpass_exercise_searchfield" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/trainingpass_sets_listview"
android:layout_alignLeft="@+id/trainingpass_exercise_searchfield"
android:layout_alignStart="@+id/trainingpass_exercise_searchfield"
android:layout_below="@+id/txt_trainingpass_setno" />
<Button
android:text="Add exercise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/trainingpass_addexercise_button"
android:layout_below="@+id/trainingpass_sets_listview"
android:layout_alignRight="@+id/add_set_button"
android:layout_alignEnd="@+id/add_set_button"
android:layout_alignLeft="@+id/trainingpass_sets_listview"
android:layout_alignStart="@+id/trainingpass_sets_listview" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/trainingpass_exercises_listview"
android:layout_below="@+id/trainingpass_addexercise_button"
android:layout_alignLeft="@+id/trainingpass_addexercise_button"
android:layout_alignStart="@+id/trainingpass_addexercise_button" />
</RelativeLayout>
我正在尝试为我的列表视图制作自定义适配器,但我一直在同一行收到 RessourceNotFoundException。
首先,这是我的适配器 class 代码:
public class CreateProgramAdapter extends BaseAdapter {
private final LayoutInflater inflater;
private ArrayList<TrainingPass> trainingPasses;
private List<TrainingPass> items;
private int layoutResourceId;
private Context context;
public CreateProgramAdapter(Context context, List<TrainingPass> items) {
this.context = context;
this.items = items;
inflater = LayoutInflater.from(this.context);
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
TrainingPassHolder holder;
if(convertView == null) {
convertView = inflater.inflate(R.layout.adapter_createprogram_trainingpasses_item, null);
holder = new TrainingPassHolder();
convertView.setTag(holder);
} else {
holder = (TrainingPassHolder) convertView.getTag();
}
holder.trainingPass = this.items.get(position);
holder.trainingPassRelativeLayout = (RelativeLayout) row.findViewById(R.id.trainingpass_item_relativelayout);
holder.headlineTxt = (TextView) row.findViewById(R.id.trainingpass_headline);
holder.setNoTxt = (TextView) row.findViewById(R.id.txt_trainingpass_setno);
holder.showHideIcon = (ImageView) row.findViewById(R.id.trainingpass_showhide_imageview);
holder.exerciseSearchField = (EditText) row.findViewById(R.id.trainingpass_exercise_searchfield);
holder.repsTextField = (EditText) row.findViewById(R.id.trainingpass_reps_txtfield);
holder.addSetButton = (Button) row.findViewById(R.id.add_set_button);
holder.addExerciseButton = (Button) row.findViewById(R.id.trainingpass_addexercise_button);
holder.setsListView = (ListView) row.findViewById(R.id.trainingpass_sets_listview);
holder.exercisesListView = (ListView) row.findViewById(R.id.trainingpass_exercises_listview);
holder.trainingPassRelativeLayout.setTag(holder.trainingPass);
holder.headlineTxt.setTag(holder.trainingPass);
holder.setNoTxt.setTag(holder.trainingPass);
holder.showHideIcon.setTag(holder.trainingPass);
holder.exerciseSearchField.setTag(holder.trainingPass);
holder.repsTextField.setTag(holder.trainingPass);
holder.addSetButton.setTag(holder.trainingPass);
holder.addExerciseButton.setTag(holder.trainingPass);
holder.setsListView.setTag(holder.trainingPass);
holder.exercisesListView.setTag(holder.trainingPass);
setupItems(holder);
return convertView;
}
private void setupItems(TrainingPassHolder trainingPassHolder) {
// TODO: set default values for items
}
public static class TrainingPassHolder {
TrainingPass trainingPass;
RelativeLayout trainingPassRelativeLayout;
TextView headlineTxt;
TextView setNoTxt;
ImageView showHideIcon;
EditText exerciseSearchField;
EditText repsTextField;
Button addSetButton;
Button addExerciseButton;
ListView setsListView;
ListView exercisesListView;
}
}
这里是视图的 xml,包含列表视图:
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.mobileplatformexam.activities.CreateProgramActivity"
android:id="@+id/relativeLayoutCreateProgram">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add training pass"
android:id="@+id/btn_add_trainingpass"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/btn_add_trainingpass_help"
android:layout_toStartOf="@+id/btn_add_trainingpass_help"
android:onClick="addTrainingPass"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="\?"
android:id="@+id/btn_add_trainingpass_help"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="addTrainingPassHelp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create program"
android:id="@+id/btn_create_program"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/btn_add_trainingpass_help"
android:layout_alignEnd="@+id/btn_add_trainingpass_help"
android:onClick="createProgram"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView_trainingpasses"
android:layout_above="@+id/btn_create_program"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/btn_add_trainingpass" />
</RelativeLayout>
以及具有上述列表视图项目布局的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/trainingpass_item_relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/trainingpass_headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_alignParentTop="true"
android:text="Trainingpass"
android:textSize="26dp"
android:textColor="@android:color/white"
android:background="@android:color/holo_blue_dark"
android:height="50dp"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:layout_toLeftOf="@+id/trainingpass_showhide_imageview"
android:layout_toStartOf="@+id/trainingpass_showhide_imageview"
android:textIsSelectable="true"
android:textStyle="bold" />
<ImageView
android:id="@+id/trainingpass_showhide_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="@drawable/abc_spinner_mtrl_am_alpha"
android:layout_alignParentTop="true"
android:background="@android:color/holo_blue_dark"
android:minWidth="50dp"
android:minHeight="50dp"
android:layout_alignBottom="@+id/trainingpass_headline" />
<EditText
android:id="@+id/trainingpass_exercise_searchfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/trainingpass_headline"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/trainingpass_showhide_imageview"
android:layout_alignEnd="@+id/trainingpass_showhide_imageview"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:hint="Name of exercise"
android:padding="15dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/txt_trainingpass_setno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set 1"
android:textSize="20dp"
android:textColor="@android:color/black"
android:paddingRight="30dp"
android:paddingLeft="10dp"
android:paddingTop="20dp"
android:paddingBottom="10dp"
android:layout_below="@+id/trainingpass_exercise_searchfield"
android:layout_toLeftOf="@+id/trainingpass_reps_txtfield"
android:layout_toStartOf="@+id/trainingpass_reps_txtfield" />
<EditText
android:id="@+id/trainingpass_reps_txtfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Reps"
android:padding="15dp"
android:width="160dp"
android:layout_alignBottom="@+id/txt_trainingpass_setno"
android:layout_toLeftOf="@+id/add_set_button"
android:layout_toStartOf="@+id/add_set_button"
android:layout_marginRight="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add_set_button"
android:text="Add set"
android:layout_alignBottom="@+id/trainingpass_reps_txtfield"
android:layout_alignRight="@+id/trainingpass_exercise_searchfield"
android:layout_alignEnd="@+id/trainingpass_exercise_searchfield"
android:layout_below="@+id/trainingpass_exercise_searchfield" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/trainingpass_sets_listview"
android:layout_alignLeft="@+id/trainingpass_exercise_searchfield"
android:layout_alignStart="@+id/trainingpass_exercise_searchfield"
android:layout_below="@+id/txt_trainingpass_setno" />
<Button
android:text="Add exercise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/trainingpass_addexercise_button"
android:layout_below="@+id/trainingpass_sets_listview"
android:layout_alignRight="@+id/add_set_button"
android:layout_alignEnd="@+id/add_set_button"
android:layout_alignLeft="@+id/trainingpass_sets_listview"
android:layout_alignStart="@+id/trainingpass_sets_listview" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/trainingpass_exercises_listview"
android:layout_below="@+id/trainingpass_addexercise_button"
android:layout_alignLeft="@+id/trainingpass_addexercise_button"
android:layout_alignStart="@+id/trainingpass_addexercise_button" />
</RelativeLayout>
</LinearLayout>
这是在我的 activity 中设置适配器的代码(扩展 Activity)
ArrayList<TrainingPass> trainingPasses = new ArrayList<TrainingPass>();
TrainingPass trainingPass = new TrainingPass();
trainingPass.setTrainingPassName("wefwef");
TrainingPass trainingPass2 = new TrainingPass();
trainingPass.setTrainingPassName("rwefhtr");
trainingPasses.add(trainingPass);
trainingPasses.add(trainingPass2);
ListView trainingPassesListView = (ListView)findViewById(R.id.listView_trainingpasses);
CreateProgramAdapter createProgramAdapter = new CreateProgramAdapter(CreateProgramActivity.this, trainingPasses);
trainingPassesListView.setAdapter(createProgramAdapter);
我得到的错误是:
12661-12661/com.mobileplatformexam E/AndroidRuntime﹕ FATAL EXCEPTION: main android.content.res.Resources$NotFoundException: Resource ID #0x7f04001a at android.content.res.Resources.getValue(Resources.java:1019) at android.content.res.Resources.loadXmlResourceParser(Resources.java:2107) at android.content.res.Resources.getLayout(Resources.java:858) at android.view.LayoutInflater.inflate(LayoutInflater.java:394) at android.view.LayoutInflater.inflate(LayoutInflater.java:352) at com.mobileplatformexam.Adapters.CreateProgramAdapter.getView(CreateProgramAdapter.java:56) at android.widget.AbsListView.obtainView(AbsListView.java:2466) at android.widget.ListView.measureHeightOfChildren(ListView.java:1244) at android.widget.ListView.onMeasure(ListView.java:1155) at android.view.View.measure(View.java:12807) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:642) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:424) at android.view.View.measure(View.java:12807) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) at android.view.View.measure(View.java:12807) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453) at android.view.View.measure(View.java:12807) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) at android.view.View.measure(View.java:12807) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1399) at android.widget.LinearLayout.measureVertical(LinearLayout.java:690) at android.widget.LinearLayout.onMeasure(LinearLayout.java:583) at android.view.View.measure(View.java:12807) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2218) at android.view.View.measure(View.java:12807) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1068) at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2452) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4476) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583) at dalvik.system.NativeStart.main(Native Method)
错误指的是我的适配器class中的第56行,也就是下面一行:
convertView = inflater.inflate(R.layout.adapter_createprogram_trainingpasses_item, null);
我真的找不到代码可能有什么问题,所以我希望有人能帮助我。
谢谢
感谢 JafarKhQ,我解决了这个问题。 通过将布局 "adapter_createprogram_trainingpasses_item .xml" 更改为以下内容,它起作用了:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="@+id/trainingpass_headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="false"
android:layout_alignParentTop="true"
android:text="Trainingpass"
android:textSize="20dp"
android:textColor="@android:color/white"
android:background="@android:color/holo_blue_dark"
android:height="30dp"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:layout_toLeftOf="@+id/trainingpass_showhide_imageview"
android:layout_toStartOf="@+id/trainingpass_showhide_imageview"
android:textIsSelectable="true"
android:textStyle="bold" />
<ImageView
android:id="@+id/trainingpass_showhide_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:src="@drawable/abc_spinner_mtrl_am_alpha"
android:layout_alignParentTop="true"
android:background="@android:color/holo_blue_dark"
android:height="30dp"
android:minWidth="50dp"
android:layout_alignBottom="@+id/trainingpass_headline" />
<EditText
android:id="@+id/trainingpass_exercise_searchfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/trainingpass_headline"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/trainingpass_showhide_imageview"
android:layout_alignEnd="@+id/trainingpass_showhide_imageview"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:hint="Name of exercise"
android:padding="15dp"
android:layout_marginTop="10dp" />
<TextView
android:id="@+id/txt_trainingpass_setno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set 1"
android:textSize="20dp"
android:textColor="@android:color/black"
android:paddingRight="30dp"
android:paddingLeft="10dp"
android:paddingTop="20dp"
android:paddingBottom="10dp"
android:layout_below="@+id/trainingpass_exercise_searchfield"
android:layout_toLeftOf="@+id/trainingpass_reps_txtfield"
android:layout_toStartOf="@+id/trainingpass_reps_txtfield" />
<EditText
android:id="@+id/trainingpass_reps_txtfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Reps"
android:padding="15dp"
android:width="160dp"
android:layout_alignBottom="@+id/txt_trainingpass_setno"
android:layout_toLeftOf="@+id/add_set_button"
android:layout_toStartOf="@+id/add_set_button"
android:layout_marginRight="10dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add_set_button"
android:text="Add set"
android:layout_alignBottom="@+id/trainingpass_reps_txtfield"
android:layout_alignRight="@+id/trainingpass_exercise_searchfield"
android:layout_alignEnd="@+id/trainingpass_exercise_searchfield"
android:layout_below="@+id/trainingpass_exercise_searchfield" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/trainingpass_sets_listview"
android:layout_alignLeft="@+id/trainingpass_exercise_searchfield"
android:layout_alignStart="@+id/trainingpass_exercise_searchfield"
android:layout_below="@+id/txt_trainingpass_setno" />
<Button
android:text="Add exercise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/trainingpass_addexercise_button"
android:layout_below="@+id/trainingpass_sets_listview"
android:layout_alignRight="@+id/add_set_button"
android:layout_alignEnd="@+id/add_set_button"
android:layout_alignLeft="@+id/trainingpass_sets_listview"
android:layout_alignStart="@+id/trainingpass_sets_listview" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/trainingpass_exercises_listview"
android:layout_below="@+id/trainingpass_addexercise_button"
android:layout_alignLeft="@+id/trainingpass_addexercise_button"
android:layout_alignStart="@+id/trainingpass_addexercise_button" />
</RelativeLayout>