我是从我的应用程序连接到互联网的小新,所以我得到了这个错误,我的代码在这里。
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();
}
和kulaniciKontrol
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();
}
感谢您的帮助...
Can't create handler inside thread that has not called Looper.prepare()
您正在呼叫< code>UyariPencerem("Uyar!",icerik);在< code>doInBackGround()的< code>catch块中,它是一个工作线程。它正在进行UI操作(显示对话框),而你正在非UI线程中进行。因此我建议你用
runOnUiThread(new Runnable() {
@Override
public void run() {
UyariPencerem("Uyarı! ",icerik);
}
});
这个UyariPencerem("Uyari!", icerik)
显示一个alertdialog。UyariPencerem("Uyari!", icerik)
称为formdoInbackground
doInbackground
在后台线程上调用。您不能从<code>或doInbackground
返回<code>doinabackground</code>中背景计算的结果。doInbackground
计算的结果是onPostExecute
。基于中的结果更新ui,onPostExecute
有关更多信息,请查看文档
http://developer.android.com/reference/android/os/AsyncTask.html
我在StackOverflow的职业生涯中有一个第一个问题。我希望是你帮我。 发生了什么:
这是在一次Android采访中被问到的。有人问我是否可以从异步任务 1 的 doInBackground() 方法(让它成为 Task1)启动另一个异步任务(让它成为 Task2)。我浏览了文档,其中说了以下内容: 必须在UI线程上创建任务实例。 必须在 UI 线程上调用 execute(Params...)。 根据这些陈述,我认为从另一个任务的后台方法启动一个任务是不可能的。此外,async任务
这可能是一个更巧妙的问题,但我在ViewComponent类中有以下方法 所以我的问题是我应该采取什么方法?让异步在那里与警告无关,还是有一个解决方案/修复这个警告?它对我的项目有那么大的影响吗? 谢了!
本文向大家介绍Android的异步任务AsyncTask详解,包括了Android的异步任务AsyncTask详解的使用技巧和注意事项,需要的朋友参考一下 AsyncTask,顾名思义,异步任务。说到异步,最简单的理解就是不同步。再复杂一点理解,就得举例子了。 假设我要去火车站买票,刚到火车站我突然发现我忘了带身份证。怎么办?怎么办! 想办法,想办法!我想我应该找个在学校的同学帮我送过来,因为我不
我正在尝试使用以下代码: 是我在Android应用程序中使用的OAuth2Client的一部分。我得到这个错误: 我会提供一些帮助。 谢谢
本文向大家介绍Android AsyncTack 异步任务实例详解,包括了Android AsyncTack 异步任务实例详解的使用技巧和注意事项,需要的朋友参考一下 Android AsyncTack 异步任务 这里写一个小实例,来学习巩固Android AsyncTack 异步任务的知识,以便在项目中使用。 介绍一下如何使用 1, 继承AsyncTask publi