[英]Android : AlertDialog doesn't show
我刚刚开始学习Android编程,但遇到了问题: My alert dialog's don't show up
。
我的想法:启动应用程序后,它将自动检查设备是否已连接到互联网,并给出建议(通过警报对话框)。
我真的希望有人能找到解决方案,因为我在看很多教程,但是每个地方都以onClickListener
(Button)开头的代码
代码段:
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkConnection();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
void checkConnection() {
final ConnectivityManager connMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
AlertDialog.Builder connectionAlert = new AlertDialog.Builder(this);
if (wifi.isConnectedOrConnecting ()) {
// Do nothing
} else if (mobile.isConnectedOrConnecting ()) {
connectionAlert.setMessage("We recommend to use wifi, enable it?");
connectionAlert.setPositiveButton("Enable", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(SplashActivity.this,"Wifi has been enabled!",Toast.LENGTH_LONG).show();
}
});
connectionAlert.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
connectionAlert.show();
} else {
connectionAlert.setMessage("Please, enable internet connection!");
connectionAlert.setNeutralButton("Wifi", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(SplashActivity.this,"Wifi has been enabled!",Toast.LENGTH_LONG).show();
}
});
connectionAlert.setNeutralButton("Mobile Data", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(SplashActivity.this,"Mobile Data has been enabled!",Toast.LENGTH_LONG).show();
}
});
connectionAlert.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
connectionAlert.show();
}
}
}
添加上下文。
public class SplashActivity extends AppCompatActivity {
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
checkConnection();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
///////
更改此行:
AlertDialog.Builder connectionAlert = new AlertDialog.Builder(this);
至:
AlertDialog.Builder connectionAlert = new AlertDialog.Builder(context);
我认为这个问题是因为您要求在启动活动上完成! 为什么您必须完成它,尽管您需要在上下文中显示警报对话框!
检查你的logcat,你会发现这个
YourActivity泄漏了最初在此处添加的窗口com.android.internal.policy.impl.PhoneWindow$DecorView@40704090
您可以删除完成活动..,并将完成活动动作移动为对话框按钮动作之一。
问题在于您正在使用SplashActivity
上下文创建AlertDialog
,但是SplashActivity
您将完成SplashActivity
,并且AlertDialog
所属的上下文不再存在。
考虑到您只想在显示此消息时选择“ Enable
,然后再转到第二个活动MainActivity
, We recommend to use wifi, enable it?
。 您可以执行以下操作:
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkConnection();
}
void checkConnection() {
final ConnectivityManager connMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
AlertDialog.Builder connectionAlert = new AlertDialog.Builder(this);
if (wifi.isConnectedOrConnecting()) {
// Do nothing
} else if (mobile.isConnectedOrConnecting()) {
connectionAlert.setMessage("We recommend to use wifi, enable it?");
connectionAlert.setPositiveButton("Enable", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(SplashActivity.this, "Wifi has been enabled!", Toast.LENGTH_LONG).show();
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
});
connectionAlert.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
connectionAlert.show();
} else {
connectionAlert.setMessage("Please, enable internet connection!");
connectionAlert.setNeutralButton("Wifi", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(SplashActivity.this, "Wifi has been enabled!", Toast.LENGTH_LONG).show();
}
});
connectionAlert.setNeutralButton("Mobile Data", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(SplashActivity.this, "Mobile Data has been enabled!", Toast.LENGTH_LONG).show();
}
});
connectionAlert.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
connectionAlert.show();
}
}
}
这里有很多选择。 但是重要的是,实际上您正在显示AlertDialog
但是却看不到它,因为您正在启动另一个活动并完成SplashActivity
,破坏了AlertDialog
首先实例化的上下文( this
) new AlertDialog.Builder(this);
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.