[英]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.