NullPointerException设置onClickEvent for dynamic button

时间:2016-04-15 11:42:44

标签: java android nullpointerexception dialog

我有一个创建按钮的函数,该按钮具有onClick事件以显示自定义对话框,该对话框仅显示函数参数中的一些信息。该对话框上还有两个按钮,一个用于关闭对话框,另一个用于向文件添加信息。

当我尝试为这些按钮设置onClick事件时,应用程序崩溃,我得到的错误是NullPointerException,表示我尝试在空对象引用上调用虚方法

如果我注释掉我为两个按钮设置onClickEventListener代码的部分,则对话框显示为正常,并带有按钮。

注意: context是在类中声明的变量。它只是Context context = this

代码如下:

public void addButton(String text, int id, String areas, String details, String notes) {
    Button button = new Button(this);
    final String title = "Add "+text;
    final String dName = text;
    final String dAreas = areas;
    final String dDetails = details;
    final String dNotes = notes;
    button.setText(text);
    button.setTextColor(ContextCompat.getColor(context, R.color.buttonText));
    button.setTextSize(32);
    button.setId(id);
    if (isEven(id+1)) {
        button.setBackgroundResource(R.drawable.buttonshapeother);
    } else {
        button.setBackgroundResource(R.drawable.buttonshape);
    }
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Make custom dialog
            final Dialog dialog = new Dialog(context);
            Button add = (Button) dialog.findViewById(R.id.btnAddExer);
            Button cancel = (Button) dialog.findViewById(R.id.btnCancel);
            dialog.setContentView(R.layout.popup_exercise);
            dialog.setTitle(title);

            // Set the custom components now
            TextView tName = (TextView) dialog.findViewById(R.id.lblNameData);
            TextView tAreas = (TextView) dialog.findViewById(R.id.lblAreaData);
            TextView tDetails = (TextView) dialog.findViewById(R.id.lblDetailsData);
            TextView tNotes = (TextView) dialog.findViewById(R.id.lblNotesData);
            tName.setText(dName);
            tAreas.setText(dAreas);
            tDetails.setText(dDetails);
            tNotes.setText(dNotes);

            // Add functions to buttons
            add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (addExercise(dName, dAreas, dDetails, dNotes)) { // Add exercise to user's workout
                        Toast.makeText(context, "Exercise was added to your workout", Toast.LENGTH_LONG).show();
                        dialog.dismiss(); // Close dialog
                    } else {
                        Toast.makeText(context, "There was an error adding your exercise", Toast.LENGTH_LONG).show();
                    }
                }
            });
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.dismiss(); // Close dialog
                }
            });

            dialog.show(); // Actually show the dialog
        }
    });
    LinearLayout lay = (LinearLayout) findViewById(R.id.innerLay);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    lay.addView(button, params);
}

public boolean isEven(int num) {
    if ((num&1) == 0) {
        return true;
    } else {
        return false;
    }
}

1 个答案:

答案 0 :(得分:2)

因为您在设置布局之前尝试查找按钮视图。所以试试这样:

final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.popup_exercise);
dialog.setTitle(title);
Button add = (Button) dialog.findViewById(R.id.btnAddExer);
Button cancel = (Button) dialog.findViewById(R.id.btnCancel);