gpt4 book ai didi

android - Android 4.1 上的 AlertDialog 问题

转载 作者:行者123 更新时间:2023-11-30 04:01:44 26 4
gpt4 key购买 nike

我在使用警告对话框时遇到一些错误。我使用 4.1 jelly bean sdk。我想检查用户的互联网连接和 gps 是否可用。

错误行:

myAlertDialog = alertDialog.create();

我的控制函数:

public void kontrol(){

LocationManager locManager = (LocationManager) getSystemService(LOCATION_SERVICE);

if (locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){

if(isConn()){

{
startActivity(new Intent (MainActivity.this,GPSanasayfa.class));
}
}
else{
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);

alertDialog.setMessage("Uygulamayı kullanabilmek için internet bağlantınızın aktif olması gerekmektedir");
alertDialog.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
});

myAlertDialog = alertDialog.create(); //I get error this line
myAlertDialog.show();
}

}

else{
startActivity(new Intent (MainActivity.this,NormalAnasayfa.class));
}

}

错误日志:

09-12 09:06:28.685: E/AndroidRuntime(825): FATAL EXCEPTION: Thread-93
09-12 09:06:28.685: E/AndroidRuntime(825): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-12 09:06:28.685: E/AndroidRuntime(825): at android.os.Handler.<init>(Handler.java:121)
09-12 09:06:28.685: E/AndroidRuntime(825): at android.app.Dialog.<init>(Dialog.java:107)
09-12 09:06:28.685: E/AndroidRuntime(825): at android.app.AlertDialog.<init>(AlertDialog.java:114)
09-12 09:06:28.685: E/AndroidRuntime(825): at android.app.AlertDialog$Builder.create(AlertDialog.java:913)
09-12 09:06:28.685: E/AndroidRuntime(825): at com.minor.cepcadisiv3.MainActivity.kontrol(MainActivity.java:55)
09-12 09:06:28.685: E/AndroidRuntime(825): at com.minor.cepcadisiv3.MainActivity$2.run(MainActivity.java:78)
09-12 09:06:28.685: E/AndroidRuntime(825): at java.lang.Thread.run(Thread.java:856)

我在 onCreate 中调用这个函数:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pdialog = ProgressDialog.show(MainActivity.this, "Kontrol Ediliyor...", "İnternet ve GPS erişiminiz kontrol ediliyor.",true);

new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(2000);
kontrol();
pdialog.dismiss();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return;
}
}).start();

}

最佳答案

试试这个..

AlertDialog myAlertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setMessage("Uygulamayı kullanabilmek için internet bağlantınızın aktif olması gerekmektedir");
alertDialog.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
});

myAlertDialog.show();

关于android - Android 4.1 上的 AlertDialog 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12384896/

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