1

アラートダイアログの操作中にエラーが発生します。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();  

}
4

2 に答える 2

1

これを試して..

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();
于 2012-09-12T09:27:09.980 に答える
0

このような問題を解決しました。

public Activity activity = MainActivity.this;

これが関数のコードです

activity.runOnUiThread(new Runnable() {
                    public void run() {
                       AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setMessage("Uygulamayı kullanabilmek için internet bağlantınızın aktif olması gerekmektedir")
                       .setCancelable(false)
                       .setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                                System.exit(0);
                           }
                       });
                AlertDialog alert = builder.create();
                alert.show(); 
                    }
                });
于 2012-09-12T11:13:58.000 に答える