gpt4 book ai didi

java - 安卓 : AlertDialog doesn't show

转载 作者:行者123 更新时间:2023-12-02 12:01:25 26 4
gpt4 key购买 nike

我刚刚开始学习 Android 编程并遇到问题:我的警报对话框没有显示

我的想法:当应用程序启动时,它会自动检查设备是否连接到互联网,并给出建议(通过警报对话框)。

我真的希望有人能找到解决方案,因为我查阅了很多教程,但每个地方都有以 onClickListener (按钮)开头的代码

代码 fragment :

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);

关于java - 安卓 : AlertDialog doesn't show,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47232600/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com