无法动态创建按钮

时间:2018-09-24 09:25:26

标签: android button

我的屏幕上有一个叠加层,其中将显示一些按钮来控制该功能。此覆盖图上将有一个动态的按钮列表,该列表将根据步骤数创建。例如,如果步骤为3,则将创建3按钮。我已经创建了LinearLayout,此按钮将被创建。我创建了一个将设置所有按钮属性的方法,但是不知何故,它给了我空指针异常。

在onCreateView中,我已经初始化了buttonArray。

这是我的片段:

  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            mContext = getActivity();
            gc =  GlobalClass.getInstance(mContext);
            db =  DataBaseHelper.getInstance(mContext);
           sectionButtons = new Button[3];
            /*if(savedInstanceState!=null)
                mVisible = savedInstanceState.getBoolean("visible",false);*/

            manager = SharedPreferenceManager.getInstance(mContext);

            //check wether to play video in cover mode or another mode

              inflater.inflate(R.layout.fragment_video_side_by_side, container, false);

        }

和onViewCreated()我正在调用方法addSections():

// show overlay with buttons in onTouch
        parentLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    if (!mVisible) {
                        overLay.setVisibility(View.VISIBLE);
                        addSections(3);
                        mVisible = true;
                    } else {
                        overLay.setVisibility(View.GONE);
                        mVisible = false;

                    }


                }
                return true;
            }
        });

方法:

 public void addSections(int numOfSection){
        if(numOfSection==0)
            return;
        else {

            for (int i = 0; i < numOfSection; i++) {
                //set the properties for button

                sectionButtons[i].setLayoutParams(new LinearLayout.LayoutParams(50, 50));
                sectionButtons[i].setBackgroundResource(R.drawable.round_button);
                sectionButtons[i].setText("Section "+i+"");
                sectionButtons[i].setId(i);

                //add button to the layout
                mSectionLayout.addView(sectionButtons[i]);
            }
        }
    }

mSectionLayout是我的父级LinearLayout。在调试时,我发现我的sectionButtons没有显示null,但是在设置属性时却抛出nullPointer异常。

1 个答案:

答案 0 :(得分:0)

创建动态按钮并附加布局

您可以通过以下方式轻松创建动态按钮:

public class MainActivity extends Activity {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
                TextView textView = new TextView(this);
                textView.setText("Text View ");
                LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.FILL_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT
                );
                layout.addView(textView, p);

                Button buttonView = new Button(this);
                buttonView.setText("Button");
                buttonView.setOnClickListener(mThisButtonListener);
                layout.addView(buttonView, p);

        }
        private OnClickListener mThisButtonListener = new OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "Hello !",
                        Toast.LENGTH_LONG).show();
            }
        };
    }

使用此代码,您可以轻松地在布局上生成按钮列表。 编码愉快