自定义列表适配器不起作用

时间:2017-07-28 11:21:58

标签: android listview

我正在自定义Android listView。以下是我的custtom适配器的代码

    public class MyAdapter extends BaseAdapter {

 Context context;
  ArrayList<menu_itemss>list;
  String[] textArray;
  int[] images;
  menu_itemss m;

 public class menu_itemss
  {
    public String text;
    public int image;
    public menu_itemss(String text,int images)
    {
        this.text=text;
        this.image=images;
    }
 }
 public class ViewHolders
 {
    ImageView imageView;
    TextView textView;

    public ViewHolders(View view)
    {
        textView=(TextView)view.findViewById(R.id.textView4);
        imageView=(ImageView)view.findViewById(R.id.imageView);
    }
  }





 public MyAdapter(Context contexts,String[] arr)
{

  list=new ArrayList<menu_itemss>();
  textArray=arr;
  this.context=contexts;
  images= new int[]{R.drawable.logo, R.drawable.logo, R.drawable.logo, 
   R.drawable.logo};

      for (int i=0;i<textArray.length;i++)
       {
       m=new menu_itemss(textArray[i],images[i]);
        list.add(m);
    }

   }

  public int getCount() {
    return list.size();
   }

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row=convertView;
    ViewHolders holder = null;
    if (row==null)
    {
        LayoutInflater inflater=(LayoutInflater) 
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.list,parent,false);
        holder=new ViewHolders(row);



        row.setTag(holder);
    }
 else
    {
   holder=(ViewHolders)row.getTag();
    }
    menu_itemss item=list.get(position);

    holder.imageView.setImageResource(item.image);
    holder.textView.setText(item.text);
    return row;
}
}

以下是使用Adapter

的ListView对象声明
    ListView listView=(ListView)findViewById(R.id.list);
    ListAdapter adapter=new MyAdapter(getApplicationContext(),s);
    listView.setAdapter(adapter);

当我构建代码时,Android工作室不会显示任何错误,但是当我在我的Android设备上运行时,会显示以下错误

   Unfortunately ! your application stopped working!

LOG Cat:

    FATAL EXCEPTION: main                                                                                       
    Process: com.example.mashood.muftiattaullahmultanicom, PID: 27743

  java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
                                                                                                  at com.example.mashood.muftiattaullahmultanicom.MyAdapter$ViewHolders.<init>(MyAdapter.java:48)
                                                                                                  at com.example.mashood.muftiattaullahmultanicom.MyAdapter.getView(MyAdapter.java:95)
                                                                                                  at android.widget.AbsListView.obtainView(AbsListView.java:2402)
                                                                                                  at android.widget.ListView.makeAndAddView(ListView.java:1891)
                                                                                                  at android.widget.ListView.fillDown(ListView.java:713)
                                                                                                  at android.widget.ListView.fillFromTop(ListView.java:779)
                                                                                                  at android.widget.ListView.layoutChildren(ListView.java:1699)
                                                                                                  at android.widget.AbsListView.onLayout(AbsListView.java:2206)
                                                                                                  at android.view.View.layout(View.java:16075)
                                                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5300)
                                                                                                  at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
                                                                                                  at android.view.View.layout(View.java:16075)
                                                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5300)
                                                                                                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                                                  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                                                  at android.view.View.layout(View.java:16075)
                                                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5300)
                                                                                                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                                                                                                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                                                                                                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                                                                                                  at android.view.View.layout(View.java:16075)
                                                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5300)
                                                                                                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                                                  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                                                  at android.view.View.layout(View.java:16075)
                                                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5300)
                                                                                                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                                                                                                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                                                                                                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                                                                                                  at android.view.View.layout(View.java:16075)
                                                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5300)
                                                                                                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                                                  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                                                  at android.view.View.layout(View.java:16075)
                                                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5300)
                                                                                                  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2119)
                                                                                                  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1873)
                                                                                                  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1073)
                                                                                                  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5988)
                                                                                                  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
                                                                                                  at android.view.Choreographer.doCallbacks(Choreographer.java:580)
                                                                                                  at android.view.Choreographer.doFrame(Choreographer.java:550)
                                                                                                  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
                                                                                                  at android.os.Handler.handleCallback(Handler.java:739)
                                                                                                  at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                                  at android.os.Looper.loop(Looper.java:135)
                                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5910)
                                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                                  at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
                                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 

在这方面的任何帮助将不胜感激。谢谢你

1 个答案:

答案 0 :(得分:0)

请将充气视图分配给该行。

row = inflater.inflate(R.layout.list,parent,false);

崩溃是因为您将null对象传递给ViewHolders。