无法获取小部件的标签

时间:2014-01-31 10:01:03

标签: android button android-widget

public class Stock_check_fragment extends Fragment {
    private CustomAutoCompleteTextView2 select_product;
    ArrayAdapter<String> adapter;

    LinearLayout ly;
    int i = 0;
    ArrayList<HashMap<String, String>> values = new ArrayList<HashMap<String, String>>();

    String[] from = { "brand", "weight" };

    // Ids of views in listview_layout
    int[] to = { R.id.brand, R.id.weight };
    Button btn;
    EditText qty;
    Button next;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater
                .inflate(R.layout.stock_check, container, false);

        ly = (LinearLayout) rootView.findViewById(R.id.linearMain);

        select_product = (CustomAutoCompleteTextView2) rootView
                .findViewById(R.id.select_product);
        btn = (Button) rootView.findViewById(R.id.button1);
        next = (Button) rootView.findViewById(R.id.next);

        populate_produts();

        SimpleAdapter adapter = new SimpleAdapter(getActivity(), values,
                R.layout.autocomplete2, from, to);

        select_product.setThreshold(1);
        select_product.setAdapter(adapter);

        select_product.setOnItemClickListener(autoItemSelectedListner);
        next.setOnClickListener(o1);

        return rootView;

    }

    private OnItemClickListener autoItemSelectedListner = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            HashMap<String, String> hm = (HashMap<String, String>) arg0
                    .getAdapter().getItem(arg2);

            Add_text(select_product.getText().toString(), hm.get("weight")
                    .toString(), hm.get("Product_ID").toString());
        }
    };

    public void Add_text(String value, String weight2, String Product_ID) {

        LinearLayout ll = new LinearLayout(getActivity());
        ll.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        TextView product = new TextView(getActivity());
        product.setText(value);
        product.setPadding(10, 10, 10, 10);
        product.setTag("product");
        ll.addView(product);


        TextView weight = new TextView(getActivity());
        weight.setText(weight2);
        weight.setPadding(10, 10, 10, 10);
        product.setTag("weight");

        ll.addView(weight);


        TextView product_ID = new TextView(getActivity());
        product_ID.setText(Product_ID);
        product_ID.setPadding(10, 10, 10, 10);
        product_ID.setTag("Product_ID");

        ll.addView(product_ID);

        qty = new EditText(getActivity());
        qty.setText(i + "");
        qty.setId(i);
        qty.setWidth(120);
        qty.setTag("qty");
        qty.setPadding(10, 10, 10, 10);

        ll.addView(qty);

        Button btn = new Button(getActivity());
        btn.setLayoutParams(params);
        btn.setOnClickListener(o);

        btn.setText("Add");
        btn.setTag("Add");
        btn.setWidth(100);
        btn.setPadding(20, 20, 20, 20);
        ll.addView(btn);


        ly.addView(ll);
        i++;

    }

    public void populate_produts() {
        HashMap<String, String> temp = new HashMap<String, String>();
        temp.put("brand", "MONT Blanc");
        temp.put("weight", "200.00$");
        temp.put("Product_ID", "200.00$");
        values.add(temp);


    }

    OnClickListener o = new OnClickListener() {

        @Override
        public void onClick(View v) {
            ViewGroup layout = (ViewGroup) v.getParent();
            if (null != layout) // for safety only as you are doing onClick
                ly.removeView(layout);

        }
    };

    OnClickListener o1 = new OnClickListener() {

        @Override
        public void onClick(View v) {
            ViewGroup parentView = ly;

            String qty = null;
            String Product_ID = null;

            for (int i = 0; i < parentView.getChildCount(); i++) {
                LinearLayout l = (LinearLayout) parentView.getChildAt(i);

                ViewGroup child_view = l;

                for (int j = 0; j < child_view.getChildCount(); j++) {
                    View childView = child_view.getChildAt(j);
                    **Log.i("sddddddddddddddddd", childView.getTag().toString());**



                }



            }
        }

    }

我尝试上面的代码创建3个文本视图和一个编辑文本和一个按钮动态它工作正常。但是当我需要获取动态小部件中包含的所有标签时,我点击一个按钮。但它给出一个null点异常。

 01-31 15:45:11.710: E/AndroidRuntime(15061): java.lang.NullPointerException
01-31 15:45:11.710: E/AndroidRuntime(15061):    at ceylon.linux.osrumsalespad.Stock_check_fragment$3.onClick(Stock_check_fragment.java:197)

1 个答案:

答案 0 :(得分:1)

TextView weight = new TextView(getActivity());
        weight.setText(weight2);
        weight.setPadding(10, 10, 10, 10);
        product.setTag("weight");

我认为这个产品应该改变重量

  TextView weight = new TextView(getActivity());
            weight.setText(weight2);
            weight.setPadding(10, 10, 10, 10);
            weight.setTag("weight");