我收到了java.lang.IllegalStateException:更新androidX库时没有活动错误

时间:2019-08-12 06:05:34

标签: android android-alertdialog androidx

在我要显示一个对话框时出现此错误

错误:-

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

4 个答案:

答案 0 :(得分:1)

在更新androidx appcompat依赖项的版本后出现此问题。 对于“临时”,我已通过以下解决方案解决了该问题。

使用

implementation 'androidx.appcompat:appcompat:1.0.2'

代替

implementation 'androidx.appcompat:appcompat:1.1.0'

答案 1 :(得分:0)

对话框片段应该在内部处理单击事件,而不是在初始化它的活动/片段中进行处理。

为了对话片段和活动之间的通信,请创建由活动实现的接口。

例如

interface DialogFragmentResultListener {
    fun onDialogResultReceived(requestCode: Int, isPositive: Boolean)
}

并通过DialogFragment传递结果,如下所示:

   if (activity is DialogFragmentResultListener) {
                        ......
   }

答案 2 :(得分:0)

您可以尝试: ConfirmDialog.show(confirmDialog,活动); 代替 ConfirmDialog.show(confirmDialog,此); 传递活动的上下文或实例

答案 3 :(得分:0)

您可以尝试使用这种方法-

BaseDialogFragment-

 abstract class BaseDialogFragment : DialogFragment(), View.OnClickListener, BaseView {

override fun onClick(v: View?) {
}

abstract fun initObjects()

abstract fun registerListeners()

abstract fun initWidgets()

protected fun setFullScreen() {
    val width = ViewGroup.LayoutParams.MATCH_PARENT
    val height = ViewGroup.LayoutParams.MATCH_PARENT
    dialog?.window?.setLayout(width, height)
}

override fun onNetworkFailure(errorCode: Int, errorMessage: String?) {
    (activity!! as BaseActivity).onNetworkFailure(errorCode, errorMessage)
}

override fun onResponseFailure(error: CloudError?) {
    (activity!! as BaseActivity).onResponseFailure(error)

}

override fun showLoader(msg: String?) {
    (activity!! as BaseActivity).showLoader(msg)

}

override fun showLoader(msg: String?, isCancellable: Boolean?) {
    (activity!! as BaseActivity).showLoader(msg, isCancellable)

}

override fun dismissLoader() {
    (activity!! as BaseActivity).dismissLoader()
}

}

像这样创建您的确认对话框-

public class AddEmailDialogFragment extends BaseDialogFragment implements View.OnClickListener {
public static final String TAG = AddEmailDialogFragment.class.getSimpleName();
private Button btnSubmit;
private EditText etEmail;

private GetEmailCallback getEmailCallBack;

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

public void setListener(GetEmailCallback listener) {
    getEmailCallBack = listener;
}

@Nullable
@Override
public View onCreateView(@NotNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_askemail, container, false);

}

@Override
public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    initObjects();
    initWidgets();
    registerListeners();
}

@Override
public void initObjects() {

}

@Override
public void registerListeners() {
    btnSubmit.setOnClickListener(this);

}

@Override
public void initWidgets() {
    View view = getView();
    btnSubmit = view.findViewById(R.id.btn_submit);
    etEmail = view.findViewById(R.id.et_email);

}

@Override
public void onClick(View view) {
    if (view.getId() == R.id.btn_submit) {
        saveEmail();
    }
}

private boolean isValidEmail(String target) {
    return (!TextUtils.isEmpty(target) && Patterns.EMAIL_ADDRESS.matcher(target).matches());
}

private void saveEmail() {
    if (isValidEmail(etEmail.getText().toString()))
        getEmailCallBack.gotEMail(etEmail.getText().toString());
    else
        etEmail.setError(getString(R.string.err_invalidEmail));
}

}

并使用此调用对话框片段-

    val activity = activity
        if (null != activity) {
            addEmailDialogFragment = AddEmailDialogFragment.newInstance()
            addEmailDialogFragment!!.show(getActivity()!!.supportFragmentManager, AddEmailDialogFragment.TAG)
            addEmailDialogFragment!!.isCancelable = false
            addEmailDialogFragment!!.setListener(this)
        }