我在如何在异步任务中将适配器设置为listview时遇到问题。我有两个选择:如果设备上有互联网,另一个如果没有。如果互联网直接搜索我需要拥有的Web服务,而如果没有互联网,则查看我的本地数据库。第二种情况很好地进出异步任务。不幸的是,第一种情况,即互联网,任务失败但未完成。我留下了我的代码,因为我知道我无法在异步任务中使用该视图,但我的问题是,如果没有互联网,它为什么会起作用?
private class cargarListaInscripciones extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ValidacionMultiple.this);
pDialog.setMessage("Cargando...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
}
@Override
protected Void doInBackground(Void... arg0) {
cargarVista();
return null;
}
}
功能:
public void cargarVista(){
SessionManager manager = new SessionManager();
rut_inscripcion = manager.getValue(this, "rut_inscripcion");
nombreCliente = manager.getValue(this, "nombreCliente");
user = manager.getValue(this, "nombreUser");
folioEvento = manager.getValue(this, "folioEvento");
codigoEvento = manager.getValue(this, "codigoEvento");
seleccionValidadora = manager.getValue(this, "opcionVerificadora");
nombreUser = manager.getValue(this, "nombreUser");
hashUser = manager.getValue(this, "hashUsuario");
listView = (ListView) findViewById(R.id.listView1);
Connection cn = new Connection();
JSONObject json = new JSONObject();
BaseDeDatos nueva = new BaseDeDatos();
if(cn.isNetworkAvailable(ValidacionMultiple.this) || seleccionValidadora.equals("Mixto")){
urls = Config.URL_BASE + nombreCliente + "/"
+ Config.URL_VALIDACION_RUT_ARRAY + nombreUser + "/"
+ hashUser + "/" + folioEvento + "/" + rut_inscripcion;
if (manager.getValue(ValidacionMultiple.this,"checkin") != null) {
id_checkin = nueva.idCheckin(ValidacionMultiple.this, manager.getValue(ValidacionMultiple.this,"checkin"));
urls = urls + id_checkin;
}
System.out.println(urls);
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(urls);
post.setHeader("content-type", "application/json");
try {
JSONObject dato = new JSONObject();
StringEntity entity = new StringEntity(dato.toString());
post.setEntity(entity);
HttpResponse resp = httpClient.execute(post);
String respStr = EntityUtils.toString(resp.getEntity());
JSONObject respJSON = new JSONObject(respStr);
json = respJSON;
} catch (Exception ex) {
Log.e("ServicioRest", "Error!", ex);
}
System.out.println("JSON: " + json);
try {
JSONArray array = json.getJSONArray("Inscripciones");
for (int i = 0; i < array.length(); i++) {
JSONObject inscripcion = (JSONObject) array.get(i);
RowItem item = new RowItem(inscripcion.getString("nombre"),inscripcion.getString("ticket"), inscripcion.getString("asiento") , inscripcion.getString("numero"),images[0] , Integer.parseInt(inscripcion.getString("inscripcion_id")), inscripcion.getString("hash"), Integer.parseInt(inscripcion.getString("validado")));
rowItems.add(item);
}
adapter = new CustomListViewAdapter(this,R.layout.lista_validacion_multiple, rowItems );
//Line 141
listView.setAdapter(adapter);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
//Modo local o mixto.
rowItems = nueva.getInscripcionsByRut(this, rut_inscripcion);
adapter = new CustomListViewAdapter(this,R.layout.lista_validacion_multiple, rowItems );
listView.setAdapter(adapter);
}
}
错误互联网:
08-10 09:54:31.837: E/AndroidRuntime(7417): FATAL EXCEPTION: AsyncTask #1
08-10 09:54:31.837: E/AndroidRuntime(7417): java.lang.RuntimeException: An error occured while executing doInBackground()
08-10 09:54:31.837: E/AndroidRuntime(7417): at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-10 09:54:31.837: E/AndroidRuntime(7417): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
08-10 09:54:31.837: E/AndroidRuntime(7417): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
08-10 09:54:31.837: E/AndroidRuntime(7417): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
08-10 09:54:31.837: E/AndroidRuntime(7417): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-10 09:54:31.837: E/AndroidRuntime(7417): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-10 09:54:31.837: E/AndroidRuntime(7417): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-10 09:54:31.837: E/AndroidRuntime(7417): at java.lang.Thread.run(Thread.java:856)
08-10 09:54:31.837: E/AndroidRuntime(7417): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-10 09:54:31.837: E/AndroidRuntime(7417): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5404)
08-10 09:54:31.837: E/AndroidRuntime(7417): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1076)
08-10 09:54:31.837: E/AndroidRuntime(7417): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4175)
08-10 09:54:31.837: E/AndroidRuntime(7417): at android.view.View.invalidate(View.java:10894)
08-10 09:54:31.837: E/AndroidRuntime(7417): at android.view.View.invalidate(View.java:10849)
08-10 09:54:31.837: E/AndroidRuntime(7417): at android.widget.AbsListView.resetList(AbsListView.java:2290)
08-10 09:54:31.837: E/AndroidRuntime(7417): at android.widget.ListView.resetList(ListView.java:511)
08-10 09:54:31.837: E/AndroidRuntime(7417): at android.widget.ListView.setAdapter(ListView.java:451)
08-10 09:54:31.837: E/AndroidRuntime(7417): at com.google.zxing.client.android.ValidacionMultiple.cargarVista(ValidacionMultiple.java:141)
08-10 09:54:31.837: E/AndroidRuntime(7417): at com.google.zxing.client.android.ValidacionMultiple$cargarListaInscripciones.doInBackground(ValidacionMultiple.java:87)
08-10 09:54:31.837: E/AndroidRuntime(7417): at com.google.zxing.client.android.ValidacionMultiple$cargarListaInscripciones.doInBackground(ValidacionMultiple.java:1)
08-10 09:54:31.837: E/AndroidRuntime(7417): at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-10 09:54:31.837: E/AndroidRuntime(7417): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-10 09:54:31.837: E/AndroidRuntime(7417): ... 4 more
答案 0 :(得分:1)
首先:
这个línea很糟糕。
adapter = new CustomListViewAdapter(this,R.layout.lista_validacion_multiple, rowItems );
你有这个,但应该是你的aplicación背景 我不太清楚你的代码,但改为:
adapter = new CustomListViewAdapter(ValidacionMultiple.this,R.layout.lista_validacion_multiple,rowItems);
第二名:
你必须在post执行方法中,在那个方法中你可以访问主线程。
问候。