当我调用我的自定义列表视图适配器时,我的应用程序失败
My app is failing when i call to my custom listview adapter
当我调用我的自定义适配器构造函数时,它没有显示错误,但是当我调用 setAdapter 时,它终止了我的应用程序
ListView listView =(ListView)findViewById(R.id.slistview);
EmployeeListAdapter suerveryAdapter= new EmployeeListAdapter(SuerveyUserActivity.this,arrayList,getResources());
listView.setAdapter(suerveryAdapter);
我的自定义适配器 class 代码是
public class EmployeeListAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<serveyData> data;
private static LayoutInflater layoutInflater = null;
public Resources res;
serveyData employee;
public EmployeeListAdapter(Activity a,ArrayList d,Resources r)
{
activity = a;
data = d;
res = r;
layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
/*for (serveyData e : data) {
String NameAndEmail = data.size()+"000"+e.getId() + "," + e.getName()+ "," +e.getDesciption();
Toast.makeText(activity, NameAndEmail,Toast.LENGTH_SHORT).show();
}*/
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
public class ViewHolder
{
public TextView tID;
public TextView tName;
public Button bDelete;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v1 = convertView;
final ViewHolder holder;
if(convertView == null)
{
v1 = layoutInflater.inflate(R.layout.single_item,null);
holder = new ViewHolder();
holder.tID = (TextView) v1.findViewById(R.id.tvID);
holder.tName = (TextView) v1.findViewById(R.id.tvName);
holder.bDelete = (Button) v1.findViewById(R.id.btnDelete);
v1.setTag(holder);
}
else
{
holder = (ViewHolder)v1.getTag();
}
if(data.size() < 1)
{
holder.tName.setText("No Data Found");
holder.bDelete.setVisibility(View.GONE);
}
else
{
employee = new serveyData();
employee = (serveyData) data.get(position);
holder.tID.setText(employee.getId());
holder.tName.setText(employee.getName());
holder.bDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*Intent i = new Intent(activity, AddEmployee.class);
activity.startActivity(i);*/
}
});
}
return v1;
}}
错误日志是
enter code here
1-02 21:17:50.269 2957-2957/com.example.abbkr.android2k17_proj E/AndroidRuntime: FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.content.res.Resources.getText(Resources.java:229)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:3620)
at com.example.abbkr.android2k17_proj.CustomAdapter.SuerveryAdapter.getView(SuerveryAdapter.java:110)
at android.widget.AbsListView.obtainView(AbsListView.java:2267)
at android.widget.ListView.makeAndAddView(ListView.java:1769)
at android.widget.ListView.fillDown(ListView.java:672)
at android.widget.ListView.fillFromTop(ListView.java:733)
at android.widget.ListView.layoutChildren(ListView.java:1622)
at android.widget.AbsListView.onLayout(AbsListView.java:2102)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1638)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1422)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:433)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1866)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1687)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik
我将 arraylist 传递给了我的自定义适配器 class,所以请帮忙
您的问题是您将 setText()
与整数参数一起使用。在这种情况下,Android 需要一个字符串资源 ID,它也是一个整数,因此它会尝试查找 ID 为 1 的资源,当然它不存在。
要解决此问题,您必须将参数作为 String
传递。所以替换这一行:
holder.tID.setText(employee.getId());
有了这个:
holder.tID.setText(String.valueOf(employee.getId()));
您需要使用
进行设置
holder.tID.setText(String.valueOf(employee.getId()));
因为你在做什么,我们使用 setText()
设置一个整数会寻找字符串资源,但它不会在那里,因此会抛出错误。
当我调用我的自定义适配器构造函数时,它没有显示错误,但是当我调用 setAdapter 时,它终止了我的应用程序
ListView listView =(ListView)findViewById(R.id.slistview);
EmployeeListAdapter suerveryAdapter= new EmployeeListAdapter(SuerveyUserActivity.this,arrayList,getResources());
listView.setAdapter(suerveryAdapter);
我的自定义适配器 class 代码是
public class EmployeeListAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<serveyData> data;
private static LayoutInflater layoutInflater = null;
public Resources res;
serveyData employee;
public EmployeeListAdapter(Activity a,ArrayList d,Resources r)
{
activity = a;
data = d;
res = r;
layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
/*for (serveyData e : data) {
String NameAndEmail = data.size()+"000"+e.getId() + "," + e.getName()+ "," +e.getDesciption();
Toast.makeText(activity, NameAndEmail,Toast.LENGTH_SHORT).show();
}*/
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
public class ViewHolder
{
public TextView tID;
public TextView tName;
public Button bDelete;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v1 = convertView;
final ViewHolder holder;
if(convertView == null)
{
v1 = layoutInflater.inflate(R.layout.single_item,null);
holder = new ViewHolder();
holder.tID = (TextView) v1.findViewById(R.id.tvID);
holder.tName = (TextView) v1.findViewById(R.id.tvName);
holder.bDelete = (Button) v1.findViewById(R.id.btnDelete);
v1.setTag(holder);
}
else
{
holder = (ViewHolder)v1.getTag();
}
if(data.size() < 1)
{
holder.tName.setText("No Data Found");
holder.bDelete.setVisibility(View.GONE);
}
else
{
employee = new serveyData();
employee = (serveyData) data.get(position);
holder.tID.setText(employee.getId());
holder.tName.setText(employee.getName());
holder.bDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*Intent i = new Intent(activity, AddEmployee.class);
activity.startActivity(i);*/
}
});
}
return v1;
}}
错误日志是
enter code here
1-02 21:17:50.269 2957-2957/com.example.abbkr.android2k17_proj E/AndroidRuntime: FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: String resource ID #0x1
at android.content.res.Resources.getText(Resources.java:229)
at android.support.v7.widget.ResourcesWrapper.getText(ResourcesWrapper.java:52)
at android.widget.TextView.setText(TextView.java:3620)
at com.example.abbkr.android2k17_proj.CustomAdapter.SuerveryAdapter.getView(SuerveryAdapter.java:110)
at android.widget.AbsListView.obtainView(AbsListView.java:2267)
at android.widget.ListView.makeAndAddView(ListView.java:1769)
at android.widget.ListView.fillDown(ListView.java:672)
at android.widget.ListView.fillFromTop(ListView.java:733)
at android.widget.ListView.layoutChildren(ListView.java:1622)
at android.widget.AbsListView.onLayout(AbsListView.java:2102)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1638)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1422)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:433)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1866)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1687)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik
我将 arraylist 传递给了我的自定义适配器 class,所以请帮忙
您的问题是您将 setText()
与整数参数一起使用。在这种情况下,Android 需要一个字符串资源 ID,它也是一个整数,因此它会尝试查找 ID 为 1 的资源,当然它不存在。
要解决此问题,您必须将参数作为 String
传递。所以替换这一行:
holder.tID.setText(employee.getId());
有了这个:
holder.tID.setText(String.valueOf(employee.getId()));
您需要使用
进行设置holder.tID.setText(String.valueOf(employee.getId()));
因为你在做什么,我们使用 setText()
设置一个整数会寻找字符串资源,但它不会在那里,因此会抛出错误。