调用自定义适配器时应用程序崩溃

Application crashes when custom adapter is called

我已经创建了自定义 Adapter 并且我已经将 Arraylist 传递给它。但是当我调用这个适配器时,我的应用程序崩溃并出现以下错误:

01-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.system.NativeStart.main(Native Method)

我的自定义适配器代码是:

public class SuerveryAdapter extends BaseAdapter {
    private Activity activity;
    private ArrayList<serveyData> data;

    private static LayoutInflater layoutInflater=null;
    public Resources res;
    serveyData ser_obj;

    public SuerveryAdapter(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 tvId;
        public TextView tvName;

        // public TextView tvDesc;
        public Button select;
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          View v1=convertView;
          final  viewHolder holder;
          if (v1==null){
              holder=new viewHolder();
              v1 =layoutInflater.inflate(R.layout.single_item,null);
              holder.tvId=(TextView) v1.findViewById(R.id.tvName);
              holder.tvName=(TextView) v1.findViewById(R.id.textViewName);
              //holder.tvDesc=(TextView) v1.findViewById(R.id.textViewDesc);
              holder.select=(Button) v1.findViewById(R.id.btnDelete) ;

              v1.setTag(holder);
          }
          else {
              holder=(viewHolder)v1.getTag();
          }
          if(data.size()<1){
              holder.tvName.setText("NO Data found");
          }
          else {
              ser_obj=new serveyData();

              ser_obj=(serveyData) data.get(position);
              holder.tvId.setText(ser_obj.getId());
              holder.tvName.setText(ser_obj.getName());
              // holder.tvDesc.setText(ser_obj.getDesciption());
              //Toast.makeText(activity,ser_obj.getId()+ser_obj.getName()+ser_obj.getDesciption(),Toast.LENGTH_SHORT).show();
        }

        return v1;
    }
}

我的调用代码是

listView =(ListView)findViewById(R.id.slistview);
SuerveryAdapter suerveryAdapter= new SuerveryAdapter(SuerveyUserActivity.this,arrayList,getResources());
listView.setAdapter(suerveryAdapter);

请指导如何解决这个问题。

Xml 我的自定义列表的文件 listview

<?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:padding="2dp"
    android:gravity="center_vertical"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvID"
        android:visibility="gone"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tvName"
        android:layout_margin="13dp" />
    <Button
        android:layout_width="wrap_content"
        android:layout_gravity="center_vertical|right"
        android:layout_height="wrap_content" android:id="@+id/btnDelete"
        android:text="Delete" />
</LinearLayout>

另一个主要listview

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_suervey_user"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.abbkr.android2k17_proj.SuerveyUserActivity">
    <ListView
        android:layout_width="321dp"
        android:layout_height="247dp"
        android:id="@+id/slistview"
    />
</LinearLayout>

您正在将 int 值设置为 TextView。然后 TextView 期望来自您的资源的文本具有该 int 值 (ser_obj.getId())。更改此行:

holder.tvId.setText(ser_obj.getId()); 

有了这个:

holder.tvId.setText(String.valueOf(ser_obj.getId()));

祝你好运。

检查您的 Textview ID

  holder.tvId=(TextView) v1.findViewById(R.id.tvName);
  holder.tvName=(TextView) v1.findViewById(R.id.textViewName);

应该是

  holder.tvId=(TextView) v1.findViewById(R.id.tvID);
  holder.tvName=(TextView) v1.findViewById(R.id.tvName);