Java 安卓:AlertDialog不';不显示
我刚开始学习Android编程,但遇到了一个问题:Java 安卓:AlertDialog不';不显示,java,android,Java,Android,我刚开始学习Android编程,但遇到了一个问题:我的警报对话框不显示 我的想法是:当应用程序启动后,它会自动检查设备是否连接到互联网,并给出建议(通过警报对话框) 我真的希望有人能找到解决方案,因为我看过很多教程,但每个地方都有以onClickListener(按钮)开头的代码 代码段: public class SplashActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedI
我的警报对话框不显示
我的想法是:当应用程序启动后,它会自动检查设备是否连接到互联网,并给出建议(通过警报对话框)
我真的希望有人能找到解决方案,因为我看过很多教程,但每个地方都有以onClickListener
(按钮)开头的代码
代码段:
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);
致:
我认为这是一个问题,因为你要求完成飞溅的活动! 为什么您必须完成它,尽管您需要在其上下文上显示警报对话框 检查你的日志,你会发现这个 YourActivity泄漏了window com.android.internal.policy.impl.PhoneWindow$DecorView@40704090原来是加在这里的
您可以删除“完成”活动。。并将“完成活动”操作移动到对话框按钮操作之一。问题在于,您正在使用
SplashActivity
上下文创建一个AlertDialog
,但是您正在紧接着完成SplashActivity
,并且AlertDialog
所属的上下文不再存在
考虑到您想转到第二个活动,main活动
,只有在您显示此消息时选择启用
之前,我们建议您使用wifi,启用它?。您可以这样做:
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
第一次实例化new AlertDialog.Builder(此)的上下文(this
);代码>您确定没有连接到WiFi吗?首先要做的是注释掉if-else if-else
部分,并检查警报是否在不影响其可见性的情况下正常工作。如果它工作正常,您可能会在第一个If(如果)中跌倒,此时不会显示警报。顺便问一下,为什么要调用finish()
?在更改活动之前,让我们删除它,并检查警报是否正确显示。是的,我没有连接wifi 2。它与“Toast”3一起工作。由于教程的原因,我正在使用finish()。(删除它,没有任何更改)。4.我刚刚发现,如果我复制此代码并将其粘贴到MainActivity(它可以工作)中,您是否尝试调用setContentView(R.layout.your_layout)
设置SplashActivity
的布局,然后再调用checkConnection()
?@MatPag没有任何更改:/我将发布我能想到的最后一个解决方案,确保您也在SplashActivity的onCreate中膨胀布局,在super.onCreate(savedInstanceState)下面进行操作;并在清单文件中注册新活动(如果该文件尚未存在)。
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();
}
}
}