Я проверяю сетевое соединение, когда интернет недоступен, появляется настраиваемое диалоговое окно, и когда сеть снова становится доступной, он автоматически исчезает, но когда приложение запускается и сеть уже доступна, это вызывает сбой приложения. как это исправить?
public class LoginActivity extends AppCompatActivity implements DroidListener{
private DroidNet mDroidNet;
MyDialog myDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mDroidNet = DroidNet.getInstance();
mDroidNet.addInternetConnectivityListener(this);
//My custom dialog
myDialog = new MyDialog();
}
@Override
public void onInternetConnectivityChanged(boolean isConnected) {
if (isConnected) {
myDialog.dismiss();
} else {
myDialog.show(getSupportFragmentManager(), "my_dialog");
tv_check_connection.setText(R.string.no_connection);
tv_check_connection.setVisibility(View.VISIBLE);
connection.startAnimation(slideDownToUp);
connection.setBackgroundColor(Color.parseColor("#2D2D2D"));
tv_check_connection.setTextColor(Color.WHITE);
}
}
}
Вот мой класс фрагмента диалога
public class MyDialog extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity(),R.style.myDialog);
final View view = getActivity().getLayoutInflater().inflate(R.layout.custom_dialog_nointernet, null);
dialog.getWindow().setContentView(view);
//dialog.getWindow().setBackgroundDrawable(d);
dialog.getWindow().setBackgroundDrawableResource(R.drawable.linearl_ayout_rounded_corner);
final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.CENTER;
dialog.setCanceledOnTouchOutside(true);
Button okay = (Button) view.findViewById(R.id.buttonOkay);
okay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
return dialog;
}
}
Исключение, которое я получаю:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ritecare, PID: 2546
java.lang.IllegalStateException: Fragment MyDialog{51034a9 (967dd225-591f-4fb0-8a7d-a15846c04836)} not associated with a fragment manager.
at androidx.fragment.app.Fragment.requireFragmentManager(Fragment.java:910)
at androidx.fragment.app.DialogFragment.dismissInternal(DialogFragment.java:245)
at androidx.fragment.app.DialogFragment.dismiss(DialogFragment.java:202)
at com.example.ritecare.activities.LoginActivity.onInternetConnectivityChanged(LoginActivity.java:292)
at com.droidnet.DroidNet.publishInternetAvailabilityStatus(DroidNet.java:227)
at com.droidnet.DroidNet.access$100(DroidNet.java:34)
at com.droidnet.DroidNet$1.onTaskFinished(DroidNet.java:197)
at com.droidnet.DroidNet$1.onTaskFinished(DroidNet.java:193)
at com.droidnet.CheckInternetTask.onPostExecute(CheckInternetTask.java:76)
at com.droidnet.CheckInternetTask.onPostExecute(CheckInternetTask.java:33)
at android.os.AsyncTask.finish(AsyncTask.java:755)
at android.os.AsyncTask.access$900(AsyncTask.java:192)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:772)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)