在调用另一个线程后应用程序崩溃,但它并不总是发生

时间:2013-03-30 17:51:29

标签: android android-alertdialog

我无法确定我缺少的东西......

我有一个在后台doWork()中运行的线程,它会侦听等待新电子邮件的Gmail帐户。收到新电子邮件后,我会显示一个对话框,其中包含要打印的选项或不打印电子邮件,任何选项都会再次调用方法doWork()

此外,虽然方法doWork()仍在侦听在后台运行的Gmail帐户,但用户可以打印先前已下载并插入数据库的电子邮件。打印后,再次调用方法doWork()

以下例外情况在打印完电子邮件后立即发生,同时仍在收听Gmail帐户....

以下是我得到的例外情况:

                04-01 16:39:52.365: I/System.out(22358): Subject : 
            04-01 16:39:55.705: D/dalvikvm(22358): GC_CONCURRENT freed 364K, 11% free 7106K/7943K, paused 3ms+3ms
            04-01 16:39:56.400: E/get message(22358): java.lang.NullPointerException
            04-01 16:39:56.400: W/dalvikvm(22358): threadid=11: thread exiting with uncaught exception (group=0x40c781f8)
            04-01 16:39:56.405: E/AndroidRuntime(22358): FATAL EXCEPTION: Thread-2153
            04-01 16:39:56.405: E/AndroidRuntime(22358): java.lang.NullPointerException
            04-01 16:39:56.405: E/AndroidRuntime(22358):    at com.example.gmailreceiver.MainActivity.retrieveEmails(MainActivity.java:578)
            04-01 16:39:56.405: E/AndroidRuntime(22358):    at com.example.gmailreceiver.MainActivity$6.run(MainActivity.java:312)
            04-01 16:39:56.405: E/AndroidRuntime(22358):    at java.lang.Thread.run(Thread.java:856)
            04-01 16:40:00.290: I/System.out(22358): Total MSG: 4 unreaed: 4

这是一种执行方法,直到在Gmail帐户中找到新电子邮件,然后它调用dialogMessage();行中的对话框:

    public void doWork(){
    try {
        Thread.sleep(11000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        disableHotspot();
        enableWifi();
        while(!isOnline()){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvStatus.setText("Waiting for a WiFi connection....");                 
                    tvStatus.setTextColor(Color.RED);   
                }
            });
        }

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tvStatus.setText("Connected!");
                tvStatus.setTextColor(Color.BLUE);                }
        });

        boolean temp = false;
        //while(!temp){
            //retrieveEmails();

        receiptPrint = new Thread(new Runnable() {

            @Override
            public void run() {
                retrieveEmails();

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(newemail){
                            getOrderDB();
                            etOrder.setText(lOrder.get(0).toString());
                            dialogMessage();
                        }
                    }
                });

            }
            });
            receiptPrint.start();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        //e.printStackTrace();
        Log.e("doWork()", e.toString());
    }
}

这是我的dialogMessage():

        public void dialogMessage() {


        final Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_safe_bag);
        dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        //dialog.setTitle(this.getString(R.string.alert_title_Confirm));
        dialog.setCancelable(false);

       // dialog.setOnDismissListener();

        final Button btnOK = (Button) dialog.findViewById(R.id.dialog_btnOK);
        final Button btnCancel = (Button) dialog.findViewById(R.id.dialog_btnCancel);

        getWindow().setSoftInputMode(
                  WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);


       // getOrderDB();

        EditText etOrderDialog = (EditText)dialog.findViewById(R.id.etorderDialog);


        if(qtyOrder > 0){
            etOrderDialog.setText(lOrder.get(0).toString());
        }

        btnOK.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(final View v) 
        {               
                Log.d("","Clicked on (Dialog) OK Button");

                // TODO: create an Alert
                //printing();

                new AsyncTask<Void, Void, Void>() {

                    @Override
                    protected void onPreExecute() {

                        //Show your progress dialog in here
                         printingDialog = ProgressDialog.show(v.getContext(), "Printing...", "Please wait", true, false);
                        super.onPreExecute();
                    }

                    @Override
                    protected Void doInBackground( Void... params ) {

                        printing();
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute( Void result ) {
                        //Dismiss your progress dialog here
                        printingDialog.dismiss();
                        doWork();
                    }
                }.execute();

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        btnPrint.setEnabled(false);
                    }
                });

                dialog.dismiss();
        }

        }); 


        btnCancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) 
        {               
                Log.d("","Clicked on (Dialog) Cancel Button");
                dialog.cancel();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        btnPrint.setEnabled(false);
                    }
                });
                 doWorkForDialog();
        }

        }); 

        dialog.show();
    }

0 个答案:

没有答案