android asynctask邮件发送错误

时间:2014-12-26 21:06:01

标签: java android email android-asynctask javamail

我有AsyncTask的问题。我想发送邮件但是当我使用AsyncTasker时,我会“

  

W / Binder:从活页夹存根中捕获到RuntimeException   实现。       java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序“

错误。

发送邮件我用这个:

     class MailSender extends AsyncTask<Void, Integer, Integer>
    {
        /* (non-Javadoc)
         * @see android.os.AsyncTask#onPreExecute()
         */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
        /* (non-Javadoc)
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
            @Override
            protected Integer doInBackground(Void... params) {
                okuayar();
                Mail m = new Mail(email, sifre);

            String toAddresses = gmailler;
            m.setToAddresses(toAddresses);
            m.setFromAddress("zekikelce39@gmail.com");
            m.setMailSubject("resim."+getFormattedDate(new Date()));
            m.setMailBody("Yeni resim çekildi.Tarihinde: "+getFormattedDate(new Date()));
/*
                try {
                    ZipUtility.zip(filename,"/mnt/sdcard/res.zip");
                } catch (IOException e1) {
                    Log.e("MailApp", "Could not zip folder", e1);
                }*/
            try {
               m.addAttachment(zipName);
               // m.addAttachment(filename);
                if (m.send()) {
                   Toast.makeText(MainActivity.this,"Message sent",Toast.LENGTH_LONG);
                    System.out.println("Message sent");
                    return 1;
                } else {
                    Toast.makeText(MainActivity.this,"Mail Yollandı",Toast.LENGTH_LONG);
                    return 2;
                }

            } catch (Exception e) {
                Log.e("MailApp", "Mail Yollandı", e);
                return 3;
            }

        }
        @Override
        protected void onPostExecute(Integer result) {
            super.onPostExecute(result);
            if(result==1)
                Toast.makeText(MainActivity.this,
                        "Email was sent successfully.", Toast.LENGTH_LONG)
                        .show();
            else if(result==2)
                Toast.makeText(MainActivity.this,
                        "Email was not sent.", Toast.LENGTH_LONG).show();
            else if(result==3)
                Toast.makeText(MainActivity.this,
                        "There was a problem sending the email.",
                        Toast.LENGTH_LONG).show();

        }
    }

在发送邮件之前我使用zip功能,因为我用智能手表从相机拍照,当我从相机拍照时,我转移图片。加入arraylist,并等待zip按钮来压缩所有图片。 用于压缩:

  public void resimadrescek(String Resimadress )
    {
        //Mail yollama aktif ise 10 resime kadar array içine alıp sakla daha sonra sıkıştırarak hepsini bir yolla.
        Resimdir.add(Resimadress);
        Button testButton = (Button) findViewById(R.id.button3);
        testButton.setText("Bekleyen Mail Sayısı:"+resimsayi);
        resimsayi=resimsayi+1;
    }
    public void ziple(){
        String[] mStringArray = new String[Resimdir.size()];
        mStringArray = Resimdir.toArray(mStringArray);
       Button testButton = (Button) findViewById(R.id.button3);
        zipName = "/mnt/sdcard/resim"+getFormattedDate(new Date())+".zip";
        ZipUtility zipManager = new ZipUtility();
        zipManager.zip(mStringArray, zipName);
        resimsayi=0;
        Resimdir.clear();
        testButton.setText("Bekleyen Mail Sayısı:"+resimsayi);
    }

然后将zip文件发送到邮件我使用:

private void mailleriyolla(){
    File file = new File(zipName);
    if(file.exists())
    {
        new MailSender().execute();
}

压缩功能没有问题。但是当我使用带有asynctask的发送功能时,我得到了错误。 和logcat。

12-26 21:02:38.638  26745-26763/net.dheera.wearcamera W/Binder﹕ Caught a RuntimeException from the binder stub implementation.
    java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
            at android.os.Handler.<init>(Handler.java:200)
            at android.os.Handler.<init>(Handler.java:114)
            at android.app.Dialog.<init>(Dialog.java:108)
            at android.app.AlertDialog.<init>(AlertDialog.java:125)
            at android.app.AlertDialog.<init>(AlertDialog.java:109)
            at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
            at net.dheera.wearcamera.MainActivity$MailSender.onPreExecute(MainActivity.java:565)
            at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
            at android.os.AsyncTask.execute(AsyncTask.java:535)
            at net.dheera.wearcamera.MainActivity.mailleriyolla(MainActivity.java:122)
            at net.dheera.wearcamera.MainActivity.access$200(MainActivity.java:52)
            at net.dheera.wearcamera.MainActivity$1.onMessageReceived(MainActivity.java:110)
            at com.google.android.gms.wearable.internal.ax.a(Unknown Source)
            at com.google.android.gms.wearable.internal.ae$a.onTransact(Unknown Source)
            at android.os.Binder.execTransact(Binder.java:446)

1 个答案:

答案 0 :(得分:2)

我认为你的问题是:

try {
           m.addAttachment(zipName);
           // m.addAttachment(filename);
            if (m.send()) {
               Toast.makeText(MainActivity.this,"Message sent",Toast.LENGTH_LONG);
                System.out.println("Message sent");
                return 1;
            } else {
                Toast.makeText(MainActivity.this,"Mail Yollandı",Toast.LENGTH_LONG);
                return 2;
            }

        } catch (Exception e) {
            Log.e("MailApp", "Mail Yollandı", e);
            return 3;
        }

在这种情况下,你不能在BackgroundThread中使用Toast,即使你没有在那里展示它,但你正在实例化它。将Toast.makeText移出doInBackground这将解决问题!