2017-07-28 89 views
0

我正在自定義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; 
} 
} 

這裏的代碼的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貓:

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) 

在這方面的任何幫助將不勝感激。謝謝你

+0

你可以發佈你的logcat – sasikumar

+0

請發佈你的崩潰報告。 –

+0

在問題中加入 – MSD

回答

0

請分配充氣視圖的行。

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

因爲您將空對象傳遞給ViewHolders而導致其崩潰。

+0

它的工作現在..謝謝你:) – MSD

+0

不客氣! – Chithra

相關問題