0

我正在尝试在线程中创建警报框,但这里出现错误是我的线程和警报框代码,这个警报框成功地解决了线程。发布代码和错误日志,帮助我。

Thread paypalThread = new Thread() {
        @Override
        public void run() {
            try {
                PackageManager pm = getApplicationContext().getPackageManager();
                ApplicationInfo appInfo = pm.getApplicationInfo(
                        "com.mothistorycheck", 0);
                String appFile = appInfo.sourceDir;
                long installed = new File(appFile).lastModified();
                Date date = new Date(installed * 1000L);
                Date currentDate = Calendar.getInstance().getTime();
                Calendar cal=Calendar.getInstance();
                cal.setTime(date);
                cal.add(Calendar.YEAR, 1);
                Date installedPlusYear = cal.getTime();
                System.out.println(installedPlusYear);      


                if (currentDate.compareTo(date)==-1) {
                    AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
                    .setNegativeButton("Close",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    dialog.cancel();
                                }
                            }).create();
            // Setting Dialog Title
            alertDialog.setTitle("Alert");

            // Setting Dialog Message
            alertDialog
                    .setMessage("Either MOT test number or Document Reference Number should be entered!");

            alertDialog.setCancelable(true);

            // Setting Icon to Dialog
            // alertDialog.setIcon(R.drawable.tick);

            // Showing Alert Message
            alertDialog.show();
            return;

                }else
                    System.out.println("000000");
                 // int comparison2 = oneYearFromNow.compareTo(date);

                // sleep(31536000);
            }

            catch (NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
            //  finish();
                Intent i = new Intent(getApplicationContext(),
                        SplashActivity.class);
                //startActivity(i);
            }
        }
    };
    paypalThread.start();

这里记录详细信息

    10-24 12:32:23.462: E/AndroidRuntime(20705): FATAL EXCEPTION: Thread-5842
10-24 12:32:23.462: E/AndroidRuntime(20705): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
10-24 12:32:23.462: E/AndroidRuntime(20705):    at android.os.Handler.<init>(Handler.java:121)
10-24 12:32:23.462: E/AndroidRuntime(20705):    at android.app.Dialog.<init>(Dialog.java:107)
10-24 12:32:23.462: E/AndroidRuntime(20705):    at android.app.AlertDialog.<init>(AlertDialog.java:114)
10-24 12:32:23.462: E/AndroidRuntime(20705):    at android.app.AlertDialog$Builder.create(AlertDialog.java:913)
10-24 12:32:23.462: E/AndroidRuntime(20705):    at com.mothistorycheck.MainActivity$1.run(MainActivity.java:132)
4

3 回答 3

0

如前所述,您不能在 UI 线程之外对 UI 进行任何更改。我建议您使用 AsyncTask 并在 onPostExecute 回调中创建您的对话框。这是你可以使用它的方式:

public void verifyPaypal() {
    PayPalVerifyTask task = new PayPalVerifyTask();
    task.execute((Void) null);
}

public class PayPalVerifyTask extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected Boolean doInBackground(Void... params) {
        try {
            PackageManager pm = getApplicationContext().getPackageManager();
            ApplicationInfo appInfo = pm.getApplicationInfo(
                    "com.mothistorycheck", 0);
            String appFile = appInfo.sourceDir;
            long installed = new File(appFile).lastModified();
            Date date = new Date(installed * 1000L);
            Date currentDate = Calendar.getInstance().getTime();
            Calendar cal=Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.YEAR, 1);
            Date installedPlusYear = cal.getTime();
            System.out.println(installedPlusYear);      

            if (currentDate.compareTo(date)==-1) {  
                return true; // this will be passed to onPostExecute
            }else
                System.out.println("000000");
                // int comparison2 = oneYearFromNow.compareTo(date);
                // sleep(31536000);
                return false;
            }
        catch (NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
        //  finish();
            Intent i = new Intent(getApplicationContext(),
                    SplashActivity.class);
            //startActivity(i);
        }
    }

    @Override
    protected void onPostExecute(final Boolean show_alert) {
        if (show_alert) {
            AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
            .setNegativeButton("Close",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int id) {
                            dialog.cancel();
                        }
                    }).create();
            // Setting Dialog Title
            alertDialog.setTitle("Alert");

            // Setting Dialog Message
            alertDialog
                    .setMessage("Either MOT test number or Document Reference Number should be entered!");

            alertDialog.setCancelable(true);

            // Setting Icon to Dialog
            // alertDialog.setIcon(R.drawable.tick);

            // Showing Alert Message
            alertDialog.show();
        }
    }
}

运行 verifyPaypal() 以启动任务。

于 2013-10-24T21:22:40.460 回答
0

您必须需要AlertDialog在 UI 线程内创建,否则它将永远无法工作。如果你在不同的线程中使用MessageHandler或者可以使用runOnUiThread(使用runnable)在里面创建你的对话框。

在 onCreate() 或 onResume() 中创建处理程序

..onCreate(){
...mHandler=new Handler();
}

然后在你Thread()刚刚使用的内部:

mHandler.post(new Runnable{
    public void run(){
        //Be sure to pass your Activity class, not the Thread
        AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
        //... setup dialog and show
    }
});
于 2013-10-24T19:33:59.903 回答
0

发生这种情况是因为您可以在不是 UI 主线程的线程内操作视图,您可以使用它Handler来执行您的对话框出现,还可以查看Handler ExampleHandling View inside Handler

于 2013-10-24T19:07:23.617 回答