1

我从我的应用程序连接到互联网有点新,所以我收到了这个错误,我的代码在这里。

08-28 14:43:29.705:E/AndroidRuntime(17341):致命异常:AsyncTask #1
08-28 14:43:29.705: E/AndroidRuntime(17341): java.lang.RuntimeException: 执行 doInBackground() 时出错
08-28 14:43:29.705: E/AndroidRuntime(17341): 在 android.os.AsyncTask$3.done(AsyncTask.java:200)
08-28 14:43:29.705: E/AndroidRuntime(17341): 在 java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
08-28 14:43:29.705: E/AndroidRuntime(17341): 在 java.util.concurrent.FutureTask.setException(FutureTask.java:125)
08-28 14:43:29.705: E/AndroidRuntime(17341): 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
08-28 14:43:29.705: E/AndroidRuntime(17341): 在 java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-28 14:43:29.705: E/AndroidRuntime(17341): 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-28 14:43:29.705: E/AndroidRuntime(17341): 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-28 14:43:29.705: E/AndroidRuntime(17341): 在 java.lang.Thread.run(Thread.java:1019)
08-28 14:43:29.705:E/AndroidRuntime(17341):原因:java.lang.RuntimeException:无法在未调用 Looper.prepare() 的线程内创建处理程序
08-28 14:43:29.705: E/AndroidRuntime(17341): 在 android.os.Handler.(Handler.java:121)
08-28 14:43:29.705: E/AndroidRuntime(17341): 在 android.app.Dialog.(Dialog.java:101)
08-28 14:43:29.705: E/AndroidRuntime(17341): 在 android.app.AlertDialog.(AlertDialog.java:63)
08-28 14:43:29.705: E/AndroidRuntime(17341): 在 android.app.AlertDialog.(AlertDialog.java:59)
08-28 14:43:29.705: E/AndroidRuntime(17341): 在 android.app.AlertDialog$Builder.create(AlertDialog.java:807)
08-28 14:43:29.705: E/AndroidRuntime(17341): 在 android.app.AlertDialog$Builder.show(AlertDialog.java:822)
08-28 14:43:29.705: E/AndroidRuntime(17341): at com.makkuzu.gps.tracker.Main.UyariPencerem(Main.java:129)
08-28 14:43:29.705: E/AndroidRuntime(17341): at com.makkuzu.gps.tracker.Main.access$2(Main.java:124)
08-28 14:43:29.705: E/AndroidRuntime(17341): at com.makkuzu.gps.tracker.Main$SaveDataTask.doInBackground(Main.java:108)
08-28 14:43:29.705: E/AndroidRuntime(17341): 在 com.makkuzu.gps.tracker.Main$SaveDataTask.doInBackground(Main.java:1)
08-28 14:43:29.705: E/AndroidRuntime(17341): 在 android.os.AsyncTask$2.call(AsyncTask.java:185)
08-28 14:43:29.705: E/AndroidRuntime(17341): 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-28 14:43:29.705: E/AndroidRuntime(17341): ... 4 更多
        private class SaveDataTask extends AsyncTask<URL, Integer, Long> {
     protected Long doInBackground(URL... urls) {
            if(KullaniciKontrol()){
                        try {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
                        String currentDateandTime = sdf.format(new Date());
                        ArrayList<NameValuePair> nameValPairs = new ArrayList<NameValuePair>();
                        nameValPairs.add(new BasicNameValuePair("name", userName.getText().toString()));
                        nameValPairs.add(new BasicNameValuePair("pass", passOne.getText().toString()));

                        nameValPairs.add(new BasicNameValuePair("time", currentDateandTime));
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost(
                                "http://www.mywebsite.com/yeni_kayit.php");
                        httppost.setEntity(new UrlEncodedFormEntity(nameValPairs));
                        httpclient.execute(httppost);

                        islemTamam();
                    } catch (Exception e) {
                        String icerik = "Bilgiler kaydedilemedi, internet bağlantınızın olduıundan emin olunuz.";
                        UyariPencerem("Uyarı! ",icerik);
                    }

                }
         return (long) 0;
     }

     protected void onProgressUpdate(Integer... progress) {

     }

     protected void onPostExecute(Long result) {

     }
 }

乌亚里彭塞雷姆

  private void UyariPencerem(String baslik, String icerik){
     Builder uyariPenceresi = new AlertDialog.Builder(this);
        uyariPenceresi.setTitle(baslik);
        uyariPenceresi.setMessage(icerik);
        uyariPenceresi.setCancelable(true);
            uyariPenceresi.show();
 }

和 kullaniciKontrol

 private boolean KullaniciKontrol() {
    // TODO Auto-generated method stub
// http post
try {
    String result = "";
    ArrayList<NameValuePair> nameValPairs = new ArrayList<NameValuePair>();
    nameValPairs.add(new BasicNameValuePair("name", userName.getText().toString()));
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(
            "http://mywebsite.com/kullanici_kontrol.php");
    httppost.setEntity(new UrlEncodedFormEntity(nameValPairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    InputStream is = entity.getContent();
    try {
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
    } catch (Exception err) {
    }
    try {
        JSONArray jArray = new JSONArray(result);
        for (int i = 0; i < jArray.length(); i++) {
            JSONObject json_data = jArray.getJSONObject(i);
            String icerik = "Böyle bir kullanici zaten mevcut lütfen farklı bir kullanıcı adı deneyin.";
            UyariPencerem("Uyarı! ", icerik);
            return false;
        }
    } catch (JSONException er_json) {
    }
} catch (Exception er) {
}
return true;
}

和 islemTamam 代码

          private void islemTamam() {

    Builder uyariPenceresi = new AlertDialog.Builder(this);
    uyariPenceresi.setTitle("İşlem Tamam! ");
    uyariPenceresi.setMessage("Bilgiler başarıyla kaydedilmiştir. \nKullanıcı adınız :" + userName.getText().toString() + "\nşifreniz:"+ passOne.getText().toString()+ "\nTelefon konumunuzu öğrenmek için \ntelefonumnerede.makkuzu.com web sitesini ziyaret ediniz. ");
    uyariPenceresi.setCancelable(true);
    uyariPenceresi.setPositiveButton("Tamam",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int id) {
            someData=getSharedPreferences(filename,0);
            Editor myEditor=someData.edit();
            myEditor.putString("name", userName.getText().toString()); 
            myEditor.putBoolean("isFirtsTime", false); 
            myEditor.commit();
            startService(new Intent(Main.this, GPSService.class));
            Main.this.finish();
        }
      });
        uyariPenceresi.show();


}

感谢您的帮助...

4

2 回答 2

0

这将UyariPencerem("Uyarı! ", icerik)显示一个警报对话框。UyariPencerem("Uyarı! ", icerik)被称为形式doInbackground

doInbackground在后台线程上调用。您无法从doInbackground.

后台计算的返回结果 doInabckground。计算的结果doInbackground是一个参数onPostExecute。根据结果​​更新 ui 中onPostExecute

有关更多信息,请查看文档

http://developer.android.com/reference/android/os/AsyncTask.html

于 2013-08-28T12:10:29.607 回答
0
Can't create handler inside thread that has not called Looper.prepare()

您正在调用UyariPencerem("Uyarı! ",icerik);of catch blockdoInBackGround()这是一个工作线程。它正在执行 UI 操作(显示对话框),并且您正在非 UI 线程中执行此操作。因此,我建议您使用

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            UyariPencerem("Uyarı! ",icerik);

                        }
                    });    
于 2013-08-28T12:12:16.960 回答