我从我的应用程序连接到互联网的新手很少,所以我收到了这个错误,我的代码就在这里。
08-28 14:43:29.705: E/AndroidRuntime(17341): FATAL EXCEPTION: AsyncTask #1 08-28 14:43:29.705: E/AndroidRuntime(17341): java.lang.RuntimeException: An error occured while executing doInBackground() 08-28 14:43:29.705: E/AndroidRuntime(17341): at android.os.AsyncTask$3.done(AsyncTask.java:200) 08-28 14:43:29.705: E/AndroidRuntime(17341): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274) 08-28 14:43:29.705: E/AndroidRuntime(17341): at java.util.concurrent.FutureTask.setException(FutureTask.java:125) 08-28 14:43:29.705: E/AndroidRuntime(17341): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308) 08-28 14:43:29.705: E/AndroidRuntime(17341): at java.util.concurrent.FutureTask.run(FutureTask.java:138) 08-28 14:43:29.705: E/AndroidRuntime(17341): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 08-28 14:43:29.705: E/AndroidRuntime(17341): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581) 08-28 14:43:29.705: E/AndroidRuntime(17341): at java.lang.Thread.run(Thread.java:1019) 08-28 14:43:29.705: E/AndroidRuntime(17341): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 08-28 14:43:29.705: E/AndroidRuntime(17341): at android.os.Handler.(Handler.java:121) 08-28 14:43:29.705: E/AndroidRuntime(17341): at android.app.Dialog.(Dialog.java:101) 08-28 14:43:29.705: E/AndroidRuntime(17341): at android.app.AlertDialog.(AlertDialog.java:63) 08-28 14:43:29.705: E/AndroidRuntime(17341): at android.app.AlertDialog.(AlertDialog.java:59) 08-28 14:43:29.705: E/AndroidRuntime(17341): at android.app.AlertDialog$Builder.create(AlertDialog.java:807) 08-28 14:43:29.705: E/AndroidRuntime(17341): at 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): at com.makkuzu.gps.tracker.Main$SaveDataTask.doInBackground(Main.java:1) 08-28 14:43:29.705: E/AndroidRuntime(17341): at android.os.AsyncTask$2.call(AsyncTask.java:185) 08-28 14:43:29.705: E/AndroidRuntime(17341): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306) 08-28 14:43:29.705: E/AndroidRuntime(17341): ... 4 more
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) {
}
}
UyariPencerem
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();
}
感谢您的帮助......
答案 0 :(得分:0)
此UyariPencerem("Uyarı! ", icerik)
显示alertdialog。 UyariPencerem("Uyarı! ", icerik)
名为doInbackground
doInbackground
。您无法从doInbackground
更新ui。
在doInabckground
中返回背景计算的结果。 doInbackground
计算的结果是onPostExecute
的参数。基于onPostExecute
有关详细信息,请查看文档
http://developer.android.com/reference/android/os/AsyncTask.html
答案 1 :(得分:0)
Can't create handler inside thread that has not called Looper.prepare()
您正在调用UyariPencerem("Uyarı! ",icerik);
catch block
中的doInBackGround()
,这是一个工作线程。它正在执行UI操作(显示对话框),而您正在非UI线程中执行此操作。因此,我建议你使用
runOnUiThread(new Runnable() {
@Override
public void run() {
UyariPencerem("Uyarı! ",icerik);
}
});