应用程序崩溃时,我退出互联网连接和开。。如果有任何修正?我想问题出在dialog.show();&&重新启动(上下文);
这是我的密码! This Is My MainActivity.kt
```
private var MyReceiver: BroadcastReceiver? = null
override fun onCreate(savedInstanceState: Bundle?) {
MyReceiver = InternetReceiver()
// Internet Connection Checker
broadcastIntent();
}
// internet Connection Receiver
fun broadcastIntent() {
registerReceiver(MyReceiver, IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION))
}`This is my InternetReceiver`
public class InternetReceiver extends BroadcastReceiver {
Dialog dialog;
boolean check;
View view;
@Override
public void onReceive(final Context context, final Intent intent) {
NetworkInfo activeNetwork = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_no_internet_dialog);
dialog.setCancelable(false);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Button restartapp = (Button)dialog.findViewById(R.id.btnRestart);
restartapp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Restart(context);
}
});
Button BtnSettings = dialog.findViewById(R.id.btnSettings);
BtnSettings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Go to Settings > Mobile Data / Wifi
Intent dialogIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(dialogIntent);
}
});
if (activeNetwork.isConnected()){
if (check){
iUtils.ShowToast(context, context.getResources().getString(R.string.internet_connected));
Restart(context);
}
} else {
Toasty.error(context, context.getResources().getString(R.string.no_internet), Toast.LENGTH_LONG, true).show();
// dialog.show();
check = true;
}
}
public void Restart(Context context){
((Activity) context).finish();
Intent i = new Intent(context, MainActivity.class);
context.startActivity(i);
}
}`AndroidManifest.xml`
`Error Log*`
at com.mrkaz.instagramdownloader.receiver.InternetReceiver.Restart(InternetReceiver.java:76)
at com.mrkaz.instagramdownloader.receiver.InternetReceiver.onReceive(InternetReceiver.java:65)
暂无答案!
目前还没有任何答案,快来回答吧!