自定义适配器onclicklistener按钮NPE * android

时间:2012-10-22 21:35:42

标签: android listview

我在我的listview上获得了NPE ..所以当我点击按钮时,它显示我在另一个表单上设置的数据..

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

public class AdapterListPemesananMenu extends BaseAdapter {
    public static ArrayList<MenuInputClass> searchArrayList;

    private LayoutInflater mInflater;

    public AdapterListPemesananMenu(Context context, ArrayList<MenuInputClass> results) {
        searchArrayList = results;
        mInflater = LayoutInflater.from(context);
    }   


    public AdapterListPemesananMenu(OnClickListener onClickListener,
            ArrayList<MenuInputClass> searchResults) {
        // TODO Auto-generated constructor stub
    } 


    public int getCount() {
        return searchArrayList.size(); *********************the NPE is here
    }

    public Object getItem(int position) {
        return searchArrayList.get(position);
    }

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_pemesananmenu_row, null);
            holder = new ViewHolder();
            holder.txtName = (TextView) convertView.findViewById(R.id.txtNamaMasuk);
            holder.txtCityState = (TextView) convertView
                    .findViewById(R.id.txtJumlahMasuk);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtName.setText(searchArrayList.get(position).getNama_menu());
        holder.txtCityState.setText(searchArrayList.get(position)
                .getJumlah_menu());


        return convertView;
    }

    static class ViewHolder {
        TextView txtName;
        TextView txtCityState;

    }
}

这是我的活动代码:

public class SingleListView_Activity extends Activity{

    Button order;       
    public static ListView list_pemesanan;    
    public static String nama_menu;
    TextView namamenu;
    public static String jumlah_menu;
    EditText jumlahmenu;  
    //public static ArrayList<MenuInputClass> searchResults = GetSearchResults();

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.single_list_view);  

        ListView list_single_list_view=(ListView)findViewById(R.id.list_single);    
        list_pemesanan = (ListView)findViewById(R.id.list);
        namamenu=(TextView)findViewById(R.id.nama_menu2);
        jumlahmenu = (EditText)findViewById(R.id.txtJumlahOrder);

        order=(Button)findViewById(R.id.btn_order);
        //when this button on click its set the data to my another form
        order.setOnClickListener(new OnClickListener() { 
                @Override
                public void onClick(View v) {                                             

                     ArrayList<MenuInputClass> searchResults = GetSearchResults();              
                      ListPemesanan_Activity.list_pemesanan.setAdapter(new AdapterListPemesananMenu(this,searchResults));   

                }

             });


    }

     private ArrayList<MenuInputClass> GetSearchResults(){
     //this is the data that i set
         ArrayList<MenuInputClass> results = new ArrayList<MenuInputClass>();

         MenuInputClass sr = new MenuInputClass();      
         sr.setJumlah_menu("asdas");
         sr.setNama_menu("2");
         results.add(sr);                
         return results;
        }
}

logcat:

10-23 04:33:19.759: E/AndroidRuntime(14459): FATAL EXCEPTION: main
10-23 04:33:19.759: E/AndroidRuntime(14459): java.lang.NullPointerException
10-23 04:33:19.759: E/AndroidRuntime(14459):    at list_pemesananmenu_view.AdapterListPemesananMenu.getCount(AdapterListPemesananMenu.java:35)
10-23 04:33:19.759: E/AndroidRuntime(14459):    at android.widget.ListView.setAdapter(ListView.java:479)
10-23 04:33:19.759: E/AndroidRuntime(14459):    at list_pemesananmenu_view.SingleListView_Activity$1.onClick(SingleListView_Activity.java:123)
10-23 04:33:19.759: E/AndroidRuntime(14459):    at android.view.View.performClick(View.java:2538)
10-23 04:33:19.759: E/AndroidRuntime(14459):    at android.view.View$PerformClick.run(View.java:9152)
10-23 04:33:19.759: E/AndroidRuntime(14459):    at android.os.Handler.handleCallback(Handler.java:587)
10-23 04:33:19.759: E/AndroidRuntime(14459):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-23 04:33:19.759: E/AndroidRuntime(14459):    at android.os.Looper.loop(Looper.java:130)
10-23 04:33:19.759: E/AndroidRuntime(14459):    at android.app.ActivityThread.main(ActivityThread.java:3693)
10-23 04:33:19.759: E/AndroidRuntime(14459):    at java.lang.reflect.Method.invokeNative(Native Method)
10-23 04:33:19.759: E/AndroidRuntime(14459):    at java.lang.reflect.Method.invoke(Method.java:507)
10-23 04:33:19.759: E/AndroidRuntime(14459):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
10-23 04:33:19.759: E/AndroidRuntime(14459):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
10-23 04:33:19.759: E/AndroidRuntime(14459):    at dalvik.system.NativeStart.main(Native Method)
10-23 04:33:19.769: E/(195): Dumpstate > /data/log/dumpstate_app_error
10-23 04:33:20.059: E/AudioPolicyManager(129): stopOutput stream = 1 , mHardwareOutput = 1, output = 1 

anyhelp将不胜感激......谢谢!!

3 个答案:

答案 0 :(得分:1)

order.setOnClickListener(new OnClickListener() { 
   @Override
   public void onClick(View v) {                                             
      ArrayList<MenuInputClass> searchResults = GetSearchResults();              
      ListPemesanan_Activity.list_pemesanan.setAdapter(new AdapterListPemesananMenu(this,searchResults));   
   }
});

在上面的代码中,您正在调用新的AdapterListPemesananMenu(OnClickListener onClickListener,ArrayList searchResults);

“this”指的是OnClickListener,而不是这种情况下的活动,因为你在新的OnClickListener()块中。

该构造函数未初始化您的searchArrayList。 searchArrayList只在构造函数中初始化(Context,ArrayList)。

如果你想保持其余的代码相同,你可以做

ListPemesanan_Activity.list_pemesanan.setAdapter(new AdapterListPemesananMenu(getApplicationContext(), searchResults));   

并且还摆脱了将OnClickListener作为参数的AdapterListPemesananMenu构造函数,以避免混淆。

这是对NPE的快速修复/分析,可能有更好的方法来重新安排代码以实现您想要实现的目标。

答案 1 :(得分:0)

您的直接问题是searchArrayList为空,但您可能已经知道了。你应该做的第一件事是检查null。您可以使用三元运算符在一行中执行此操作:

return (searchArrayList == null) ? 0 : searchArrayList.size();

答案 2 :(得分:0)

您只是忘了在使用过的构造函数中设置ArrayList<MenuInputClass>

 public AdapterListPemesananMenu(OnClickListener onClickListener,
        ArrayList<MenuInputClass> searchResults) {
     // TODO Auto-generated constructor stub
     searchArrayList = results;

 } 

这就是你searchArrayList为空的原因