Java 动态按钮的NullPointerException设置onClickEvent

Java 动态按钮的NullPointerException设置onClickEvent,java,android,nullpointerexception,dialog,Java,Android,Nullpointerexception,Dialog,我有一个函数,它创建了一个按钮,它有一个onClick事件来打开一个自定义对话框,其中只显示函数参数中的一些信息。对话框上还有两个按钮,一个用于关闭对话框,另一个用于向文件中添加信息 当我尝试为这些按钮设置onClick事件时,应用程序崩溃,我得到的错误是NullPointerException,它表示我正在尝试对null对象引用调用虚拟方法 如果我注释掉为两个按钮设置onClickEventListener代码的部分,则对话框显示为正常对话框,上面有按钮 注意:上下文是类中声明的变量。它只是C

我有一个函数,它创建了一个按钮,它有一个
onClick
事件来打开一个自定义对话框,其中只显示函数参数中的一些信息。对话框上还有两个按钮,一个用于关闭对话框,另一个用于向文件中添加信息

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

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

注意:
上下文
是类中声明的变量。它只是
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;
    }
}

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

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);

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

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);

请完成logcat可能需要
dialog.setContentView(R.layout.popup\u练习)
findViewById
之前。请完成logcat可能需要
dialog.setContentView(R.layout.popup\u练习)
findViewById
之前。您好,请查看我对实际问题的评论。将此标记为答案。谢谢您好,请看我对实际问题的评论。将此标记为答案。谢谢