尝试在expandablelistview android studio中运行时创建按钮的NPE错误

时间:2014-09-29 17:46:06

标签: java android button expandablelistview

当您点击listview的其中一个元素时,我正在尝试在expandablelistview中的运行时创建按钮

     public class ExpandAnimationDemo extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    //To change body of overridden methods use File | Settings | File Templates.
        setContentView(R.layout.main);

        ListView list = (ListView)findViewById(R.id.udiniList);

        final LinearLayout lm = (LinearLayout) findViewById(R.id.toolbar);

        ArrayAdapter<String> listAdapter = new CustomListAdapter(this, R.layout.list_item);
        for (int i=0; i<5;i++)
            listAdapter.add("udini"+i);

        // create the layout params that will be used to define how your
        // button will be displayed

      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.HORIZONTAL);
        // Create Button
        final Button btn = new Button(this);
        // Give button an ID
        //btn.setId(j+1);
        btn.setText("Add To Cart");
        // set the layoutParams on the button
        btn.setLayoutParams(params);
        //Add button to LinearLayout
        ll.addView(btn);

        //Add button to LinearLayout defined in XML
        lm.addView(ll); here i have de null pointer!



        list.setAdapter(listAdapter);



        // Creating an item click listener, to open/close our toolbar for each item
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {


                View toolbar = view.findViewById(R.id.toolbar);



                // Creating the expand animation for the item
                ExpandAnimation expandAni = new ExpandAnimation(toolbar, 500);

                // Start the animation on the toolbar
                toolbar.startAnimation(expandAni);
            }
        });

    }

    /**
     * A simple implementation of list adapter.
     */
    class CustomListAdapter extends ArrayAdapter<String> {

        public CustomListAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.list_item, null);
            }

            ((TextView)convertView.findViewById(R.id.title)).setText(getItem(position));


            // Resets the toolbar to be closed
            View toolbar = convertView.findViewById(R.id.toolbar);
            ((LinearLayout.LayoutParams) toolbar.getLayoutParams()).bottomMargin = -50;
            toolbar.setVisibility(View.GONE);

            return convertView;
        }
    }
}

但是我得到一个空指针异常

  

引起:java.lang.NullPointerException               在com.udinic.expand_animation_example.ExpandAnimationDemo.onCreate(ExpandAnimationDemo.java:51)   ---&GT;这里lm.addView(ll);

0 个答案:

没有答案