I developed a class to the listener when wifi is off and I wrote an alert dialog to show a message. Now I specialized a class to show messages in every class, but I can't resolve the error.
public class Messages extends AppCompatDialogFragment {
private String message;
public Messages(String message){
this.message = message;
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Warning!!!")
.setMessage(this.message)
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}
}
public class NetworkChangeListener extends BroadcastReceiver {
private static final String CONNECTION_WIFI_OFF ="WIFI OFF.";
@Override
public void onReceive(Context context, Intent intent) {
if(ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())){
Messages messages = new Messages(CONNECTION_WIFI_OFF);
messages.show(getSupportFragmentManager(),"messages");
}
}
}
}
I changed messages.show(getSupportFragmentManager(),"messages");
in messages.show(messages.getChildFragmentManager(),"messages");
, but it doesn't work. I received this error:
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x4000010 (has extras) } in com.example.connectivity.NetworkChangeListener@44fa736
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:891)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: Fragment Messages{f08eacf} (69334c8a-e786-46b5-bf7a-61a6b901b9d5) has not been attached yet.
at androidx.fragment.app.Fragment.getChildFragmentManager(Fragment.java:1075)
at com.example.connectivity.NetworkChangeListener.onReceive(NetworkChangeListener.java:49)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:881)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
CodePudding user response:
In order to access FragmentManager
the fragment itself have to be attached to activity. You are just creating the fragment, but it's not being used and displayed anywhere. It's just useless object.
You need to first display the Messages fragment (please name if MessagesFragment
) in the activity, then you can run the broadcast receiver inside it and bind it to it's lifecycle
CodePudding user response:
Try this you should only replace getSupportFragmentManager()
with messages.getChildFragmentManager()
public class Messages extends AppCompatDialogFragment {
private String message;
private Context mContext;
public Messages(String message,Context context){
this.message = message;
mContext = context;
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Warning!!!")
.setMessage(this.message)
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}
}
public class NetworkChangeListener extends BroadcastReceiver {
private static final String CONNECTION_WIFI_OFF ="WIFI OFF.";
@Override
public void onReceive(Context context, Intent intent) {
if(ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())){
Messages messages = new Messages(CONNECTION_WIFI_OFF);
messages.show(messages.getChildFragmentManager(), "messages");
}
}
}