Radiogroup中的NullPointerException

时间:2014-05-10 06:54:56

标签: java android gridview radio-button android-custom-view

当我点击添加按钮时,我收到空指针异常。

public class ListAdapter extends BaseAdapter {
Bitmap bm = null;
// Declare Variables
Context mContext;
LayoutInflater inflater;
private RadioButton qtyselbut, timeselbut;
private List<Pojo> list = null;
private ArrayList<Pojo> arraylist;

public ListAdapter(Context context, List<Pojo> list) {
    mContext = context;
    this.list = list;
    inflater = LayoutInflater.from(mContext);
    this.arraylist = new ArrayList<Pojo>();
    this.arraylist.addAll(list);
}

private Resources getResources() {
    // TODO Auto-generated method stub
    return null;
}

public class ViewHolder {
    TextView productname, product_name, product_price;
    TextView description;
    ImageView img1;
    TextView price;
    Button custom, add;
    RelativeLayout img, relatve, abve_lyt;
    LinearLayout radio;
    RadioGroup qtygroup, timegrp;
    RadioButton groupchecked;

}

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

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

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

@SuppressLint("ResourceAsColor")
public View getView(final int position, View view, ViewGroup parent) {
    final ViewHolder holder;
    if (view == null) {
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.gridimg, null);
        // Locate the TextViews in listview_item.xml
        holder.productname = (TextView) view.findViewById(R.id.productname);
        holder.product_name = (TextView) view
                .findViewById(R.id.product_name);
        holder.description = (TextView) view.findViewById(R.id.description);
        holder.img1 = (ImageView) view.findViewById(R.id.imageView1);
        holder.price = (TextView) view.findViewById(R.id.price);
        holder.product_price = (TextView) view
                .findViewById(R.id.product_price);
        holder.custom = (Button) view.findViewById(R.id.customize);
        holder.relatve = (RelativeLayout) view.findViewById(R.id.relavelyt);
        holder.radio = (LinearLayout) view.findViewById(R.id.radioGroup1);
        holder.qtygroup = (RadioGroup) view.findViewById(R.id.qty_text);
        /* holder.button1 = (RadioButton)view.findViewById(R.id.radibuttt0); */
        holder.abve_lyt = (RelativeLayout) view
                .findViewById(R.id.above_layt);
        holder.add = (Button) view.findViewById(R.id.Add_item);
        holder.timegrp = (RadioGroup) view.findViewById(R.id.time_text);

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

    holder.productname.setText(list.get(position).getProductname());
    holder.description.setText(list.get(position).getDescription());
    holder.price.setText(list.get(position).getPrice());
    holder.product_price.setText(list.get(position).getPrice());
    holder.product_name.setText(list.get(position).getProductname());
    /*
     * holder.button1.setOnCheckedChangeListener((OnCheckedChangeListener)this
     * ); holder.button1.setTypeface(Typeface.DEFAULT_BOLD);
     */

    Log.i("", "" + holder.productname.getText().toString());
    Log.i("", "" + holder.description.getText().toString());
    Log.i("", "" + holder.price.getText().toString());
    // holder.img1.setImageBitmap(DownloadFullFromUrl(list.get(position).getImg1()));
    Bitmap rotatedBitmap = DownloadFullFromUrl(list.get(position).getImg1());
    Drawable drawable = new BitmapDrawable(rotatedBitmap);
    holder.relatve.setBackgroundDrawable(drawable);

    // RadioButton radioButton = new RadioButton(mContext);
    // radioButton.setText(" Roasted Tomato");
    // radioButton.setText(" Red Chilli");
    // radioButton.setButtonDrawable(R.drawable.customizeradiobutton);
    // holder.radio.addView(radioButton);

    holder.relatve.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent bv = new Intent(mContext, ViewpagerActivity.class);
            bv.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(bv);

            // v.getContext().startActivity(mContext,ViewpagerActivity.class);

        }
    });



    holder.custom.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            holder.abve_lyt.setVisibility(View.VISIBLE);

        }
    });

    holder.add.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(final View v) {
            // TODO Auto-generated method stub

            String hj = holder.product_name.getText().toString();
            Log.d("sucess or failed", "" + hj);

            MainActivity.adapter.add(hj);
            MainActivity.adapter.notifyDataSetChanged();

            int selectedId = holder.qtygroup.getCheckedRadioButtonId();

            // find the radiobutton by returned id
            qtyselbut = (RadioButton) v.findViewById(selectedId);

            String yh = qtyselbut.getText().toString();

            Log.d("qtyselectdtext", "" + yh);

            int selectedid2 = holder.timegrp.getCheckedRadioButtonId();

            timeselbut = (RadioButton) v.findViewById(selectedid2);

            String yhd = timeselbut.getText().toString();

            Log.d("timeselectdtext", "" + yhd);

        }
    });



    return view;
}

public Bitmap DownloadFullFromUrl(String imagess) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();

    StrictMode.setThreadPolicy(policy);
    try {
        URL url = new URL(imagess);
        URLConnection ucon = url.openConnection();
        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayBuffer baf = new ByteArrayBuffer(50);
        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
        }
        bm = BitmapFactory.decodeStream(is);
        BitmapDrawable background = new BitmapDrawable(bm);
        View view = null;
        bm = BitmapFactory.decodeByteArray(baf.toByteArray(), 0,
                baf.toByteArray().length);
    } catch (IOException e) {
        Log.d("ImageManager", "Error: " + e);
    }
    return bm;
}

}`

4 个答案:

答案 0 :(得分:0)

你必须得到NPE:

String yh = qtyselbut.getText().toString();

因为qtyselbut为空。

在下面的线

qtyselbut = (RadioButton) v.findViewById(selectedId);

v引用您的按钮(holder.add),您需要父视图而不是v,您必须使用holder对象获取布局值

我在你的代码中看到的另一件事是

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();

不要使用此功能,而应使用AsycTask类或Handler

//更新

发生了新问题,因为行

 int selectedId = holder.qtygroup.getCheckedRadioButtonId();

holder不是您想要的持有者,您可以使用set CheckedRadioButtonId来完成工作,以便在getView方法中标记您的按钮,如:

  holder.add.setTag(yourValue);

然后在按钮点击中您可以使用:

  int selectedId = (Integer)v.getTag();

答案 1 :(得分:0)

我觉得这部分负责Exception

          qtyselbut = (RadioButton) v.findViewById(selectedId);
            String yh = qtyselbut.getText().toString();
        Log.d("qtyselectdtext", "" + yh);

答案 2 :(得分:0)

将此holder.relatve.setOnClickListener更改为view.relatve.setOnclickListener并进行检查。

答案 3 :(得分:0)

//尝试这种方式,希望这会对你有帮助......

在添加按钮下单击监听器此代码会出现问题因为您正在尝试查找RadioButton v.findViewById()此处v表示按钮视图而非父布局所以您可以请注释此代码并检查会发生什么

   int selectedId = holder.qtygroup.getCheckedRadioButtonId();
   // find the radiobutton by returned id
   qtyselbut = (RadioButton) v.findViewById(selectedId);
   String yh = qtyselbut.getText().toString();
   Log.d("qtyselectdtext", "" + yh);
   int selectedid2 = holder.timegrp.getCheckedRadioButtonId();
   timeselbut = (RadioButton) v.findViewById(selectedid2);
   String yhd = timeselbut.getText().toString();
    Log.d("timeselectdtext", "" + yhd);