传递上下文总是失败 - 无法添加 window -- token null 无效;你是 activity 运行 吗?
Passing the Context always fails - Unable to add window -- token null is not valid; is your activity running?
我正在开发一个具有 DialogBuilder Class 的应用程序,我在其中实现了该应用程序的所有对话框,以便能够在其他服务或活动中调用它们;它工作得很好,除了在 Activity 中,我尝试了一切来传递上下文——它不工作;因此,如果有任何提示或帮助,我将非常高兴,谢谢!
对话:
public static void bookingConfirmationDialog(Context mContext) {
if(mContext != null) {
final Dialog dialog = new Dialog(GoldbekStorageApp.getInstance(), 0);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.new_booking_layout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
TextView textView = dialog.findViewById(R.id.messageId);
textView.setText(GoldbekStorageApp.getInstance().messageId);
Button okButton = dialog.findViewById(R.id.ok);
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
Dialog的调用:
proceedButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
message.setType(type);
message.setFromId(fromID);
message.setToId(toID);
message.setTypeId(typeID);
message.setTime(time);
message.setTitle(title);
message.setReceiptNo(receiptNo);
message.setNote(note);
RestClient.putBookingOnPallet(basic,message,context);
DialogBuilder.bookingConfirmationDialog(context);
/* Intent activityChangeIntent = new Intent( NewProductActivity.this,
NewProductActivity.class);
NewProductActivity.this.startActivity(activityChangeIntent);*/
}
});
我可能遗漏了一些东西,但您可以 override onAttach
在 DialogFragment class 中而不是通过构造函数传递上下文。
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
}
我正在开发一个具有 DialogBuilder Class 的应用程序,我在其中实现了该应用程序的所有对话框,以便能够在其他服务或活动中调用它们;它工作得很好,除了在 Activity 中,我尝试了一切来传递上下文——它不工作;因此,如果有任何提示或帮助,我将非常高兴,谢谢!
对话:
public static void bookingConfirmationDialog(Context mContext) {
if(mContext != null) {
final Dialog dialog = new Dialog(GoldbekStorageApp.getInstance(), 0);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.new_booking_layout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
TextView textView = dialog.findViewById(R.id.messageId);
textView.setText(GoldbekStorageApp.getInstance().messageId);
Button okButton = dialog.findViewById(R.id.ok);
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
Dialog的调用:
proceedButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
message.setType(type);
message.setFromId(fromID);
message.setToId(toID);
message.setTypeId(typeID);
message.setTime(time);
message.setTitle(title);
message.setReceiptNo(receiptNo);
message.setNote(note);
RestClient.putBookingOnPallet(basic,message,context);
DialogBuilder.bookingConfirmationDialog(context);
/* Intent activityChangeIntent = new Intent( NewProductActivity.this,
NewProductActivity.class);
NewProductActivity.this.startActivity(activityChangeIntent);*/
}
});
我可能遗漏了一些东西,但您可以 override onAttach
在 DialogFragment class 中而不是通过构造函数传递上下文。
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
}