gpt4 book ai didi

android - 我得到了 java.lang.IllegalStateException : No activity error when I update androidX libraries

转载 作者:行者123 更新时间:2023-11-29 18:28:54 25 4
gpt4 key购买 nike

我在显示对话框时遇到了这个错误

错误:-

No activity error

当我在 androidx 的库下面更新时出现这个错误

<强>1。实现 'androidx.appcompat:appcompat:1.1.0-rc01

<强>2。实现 'com.google.android.material:material:1.1.0-alpha09'

----- 这是我的确认对话框 ----

public class ConfirmDialog extends BaseDialogFragment {

public static ConfirmDialog newInstance() {
return new ConfirmDialog();
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // Error Comes

}

@Override
protected Dialog createDialog(Context activityContext) {

AlertDialog.Builder builder;
builder = new AlertDialog.Builder(activityContext, android.R.style.Theme_Material_Light_Dialog_Alert);

AlertDialog alertDialog = builder.create();
alertDialog.setCanceledOnTouchOutside(false);
return alertDialog;
}

@Override
public void onClick(DialogInterface dialog, int which) {
super.onDialogClick(dialog, which, which);
}
}

----- 这是我的BaseDialog ----

public abstract class BaseDialogFragment extends DialogFragment implements DialogInterface.OnClickListener {

protected OnDialogClickListener onClickListener;

protected BaseDialogFragment() {
}

protected BaseDialogFragment(OnDialogClickListener onClickListener) {
this.onClickListener = onClickListener;
}

public static void show(BaseDialogFragment dialogFragment, Context context) {
dialogFragment.onCreate(null);
Dialog dialog = dialogFragment.createDialog(context);
dialog.show();
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return createDialog(getActivity());
}

protected abstract Dialog createDialog(Context activityContext);

public void setOnClickListener(OnDialogClickListener onClickListener) {
this.onClickListener = onClickListener;
}

public void removeOnClickListener() {
this.onClickListener = null;
}

public void onDialogClick(DialogInterface dialog, int which, Object o) {
if (this.onClickListener != null) {
this.onClickListener.onDialogClick(dialog, which, o);
}
}
}

--- 最后,这是我调用我的 Confirmdialog 的时候 ---

ConfirmDialog confirmDialog = ConfirmDialog.newInstance();
confirmDialog.setOnClickListener(new OnDialogClickListener() {
@Override
public void onDialogClick(DialogInterface dialog, int which, Object o) {

}
});

ConfirmDialog.show(confirmDialog, activity); // When I call this

最佳答案

你可以试试:ConfirmDialog.show(confirmDialog, Activity );反而ConfirmDialog.show(confirmDialog, 这个);传递 Activity 的上下文或实例

关于android - 我得到了 java.lang.IllegalStateException : No activity error when I update androidX libraries,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57456310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com