使用警报对话框时出现一些错误。我使用 4.1 果冻豆 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();
}