这个类有一个例外:“只有创建视图层次结构的原始线程才能接触其视图”。
我知道这可能是因为我使用了async,但我是android开发的新手,即使阅读了Stackoverflow上的其他案例,我也没能安排好它。欢迎光临!
我的班级:
package com.test.example;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class ShopsActivity extends Activity {
private ProgressDialog pDialog;
// URL to get contacts JSON
private static String url = "mywebserviceurl";
// JSON Node names
private static final String TAG_NAME = "shop_name";
private static final String TAG_ADDRESS_STRING = "shop_address";
TextView shop_address;
TextView shop_name;
TextView api;
private ListView list;
private ImageButton button1;
private ImageButton button2;
private ImageButton button3;
private ImageButton button4;
// contacts JSONArray
JSONArray contacts = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shops);
// Calling async task to get json
new GetJson().execute();
}
/**
* Async task class to get json by making HTTP call
* */
private class GetJson extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(ShopsActivity.this);
pDialog.setMessage("Mise à jour...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
JSONArray array = null;
try {
array = new JSONArray(jsonStr);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
for (int i = 0; i <array.length(); i++){
JSONObject jsonObject = array.getJSONObject(i);
String id = jsonObject.getString("id");
System.out.println("id --->" + id);
String url = jsonObject.getString("url");
System.out.println("url --->" + url);
String created_at = jsonObject.getString("created_at");
System.out.println("created_at --->" + created_at);
String updated_at = jsonObject.getString("updated_at");
System.out.println("updated_at --->" + updated_at);
String name = jsonObject.getString("name");
System.out.println("name --->" + name);
JSONArray photos_urls = jsonObject.getJSONArray("photos_urls");
System.out.println("photos_urls --->" + photos_urls);
String address_string = jsonObject.getString("address_string");
System.out.println("address_string --->" + address_string);
JSONObject booklet = jsonObject.getJSONObject("booklet");
String id_booklet = booklet.getString("id");
System.out.println("id_booklet --->" + id_booklet);
String url_booklet = booklet.getString("url");
System.out.println("url_booklet --->" + url_booklet);
String created_at_booklet = booklet.getString("created_at");
System.out.println("created_at_booklet --->" + created_at_booklet);
String updated_at_booklet = booklet.getString("updated_at");
System.out.println("updated_at_booklet --->" + updated_at_booklet);
String document_url = booklet.getString("document_url");
System.out.println("document_url --->" + document_url);
String presented = booklet.getString("presented");
System.out.println("presented --->" + presented);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_NAME, name);
map.put(TAG_ADDRESS_STRING, address_string);
oslist.add(map);
list=(ListView)findViewById(R.id.list);
ListAdapter adapter = new SimpleAdapter(ShopsActivity.this, oslist,
R.layout.listview_item_row,
new String[] { TAG_NAME, TAG_ADDRESS_STRING }, new int[] {
R.id.name_shop, R.id.address_shop});
list.setAdapter(adapter);
/* list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(ShopsActivity.this, ShopActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.animationin, R.anim.animationout);
}
});*/
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
}
}
}
这是我尝试使用onPostExecute后的日志:
12-30 14:10:28.515: E/AndroidRuntime(12951): FATAL EXCEPTION: AsyncTask #1
12-30 14:10:28.515: E/AndroidRuntime(12951): java.lang.RuntimeException: An error occured while executing doInBackground()
12-30 14:10:28.515: E/AndroidRuntime(12951): at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-30 14:10:28.515: E/AndroidRuntime(12951): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-30 14:10:28.515: E/AndroidRuntime(12951): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-30 14:10:28.515: E/AndroidRuntime(12951): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-30 14:10:28.515: E/AndroidRuntime(12951): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-30 14:10:28.515: E/AndroidRuntime(12951): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-30 14:10:28.515: E/AndroidRuntime(12951): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-30 14:10:28.515: E/AndroidRuntime(12951): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-30 14:10:28.515: E/AndroidRuntime(12951): at java.lang.Thread.run(Thread.java:856)
12-30 14:10:28.515: E/AndroidRuntime(12951): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
12-30 14:10:28.515: E/AndroidRuntime(12951): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4609)
12-30 14:10:28.515: E/AndroidRuntime(12951): at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:867)
12-30 14:10:28.515: E/AndroidRuntime(12951): at android.view.ViewGroup.invalidateChild(ViewGroup.java:4066)
12-30 14:10:28.515: E/AndroidRuntime(12951): at android.view.View.invalidate(View.java:10250)
12-30 14:10:28.515: E/AndroidRuntime(12951): at android.view.View.invalidate(View.java:10205)
12-30 14:10:28.515: E/AndroidRuntime(12951): at android.widget.AbsListView.resetList(AbsListView.java:1952)
12-30 14:10:28.515: E/AndroidRuntime(12951): at android.widget.ListView.resetList(ListView.java:502)
12-30 14:10:28.515: E/AndroidRuntime(12951): at android.widget.ListView.setAdapter(ListView.java:442)
12-30 14:10:28.515: E/AndroidRuntime(12951): at com.begital.votrebijoutier.ShopsActivity$GetJson.doInBackground(ShopsActivity.java:141)
12-30 14:10:28.515: E/AndroidRuntime(12951): at com.begital.votrebijoutier.ShopsActivity$GetJson.doInBackground(ShopsActivity.java:1)
12-30 14:10:28.515: E/AndroidRuntime(12951): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-30 14:10:28.515: E/AndroidRuntime(12951): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-30 14:10:28.515: E/AndroidRuntime(12951): ... 5 more
12-30 14:10:28.975: E/WindowManager(12951): Activity com.begital.votrebijoutier.ShopsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@419962f8 that was originally added here
12-30 14:10:28.975: E/WindowManager(12951): android.view.WindowLeaked: Activity com.begital.votrebijoutier.ShopsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@419962f8 that was originally added here
12-30 14:10:28.975: E/WindowManager(12951): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
12-30 14:10:28.975: E/WindowManager(12951): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
12-30 14:10:28.975: E/WindowManager(12951): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
12-30 14:10:28.975: E/WindowManager(12951): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
12-30 14:10:28.975: E/WindowManager(12951): at android.view.Window$LocalWindowManager.addView(Window.java:547)
12-30 14:10:28.975: E/WindowManager(12951): at android.app.Dialog.show(Dialog.java:277)
12-30 14:10:28.975: E/WindowManager(12951): at com.begital.votrebijoutier.ShopsActivity$GetJson.onPreExecute(ShopsActivity.java:77)
12-30 14:10:28.975: E/WindowManager(12951): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
12-30 14:10:28.975: E/WindowManager(12951): at android.os.AsyncTask.execute(AsyncTask.java:534)
12-30 14:10:28.975: E/WindowManager(12951): at com.begital.votrebijoutier.ShopsActivity.onCreate(ShopsActivity.java:61)
12-30 14:10:28.975: E/WindowManager(12951): at android.app.Activity.performCreate(Activity.java:5008)
12-30 14:10:28.975: E/WindowManager(12951): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-30 14:10:28.975: E/WindowManager(12951): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-30 14:10:28.975: E/WindowManager(12951): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-30 14:10:28.975: E/WindowManager(12951): at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-30 14:10:28.975: E/WindowManager(12951): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-30 14:10:28.975: E/WindowManager(12951): at android.os.Handler.dispatchMessage(Handler.java:99)
12-30 14:10:28.975: E/WindowManager(12951): at android.os.Looper.loop(Looper.java:137)
12-30 14:10:28.975: E/WindowManager(12951): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-30 14:10:28.975: E/WindowManager(12951): at java.lang.reflect.Method.invokeNative(Native Method)
12-30 14:10:28.975: E/WindowManager(12951): at java.lang.reflect.Method.invoke(Method.java:511)
12-30 14:10:28.975: E/WindowManager(12951): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-30 14:10:28.975: E/WindowManager(12951): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-30 14:10:28.975: E/WindowManager(12951): at dalvik.system.NativeStart.main(Native Method)
12-30 14:10:31.905: I/Process(12951): Sending signal. PID: 12951 SIG: 9
您无法更改或访问后台线程中的ui元素。listview setadapter方法导致错误。
ListAdapter adapter = new SimpleAdapter(ShopsActivity.this, oslist,
R.layout.listview_item_row, new String[] {TAG_NAME,TAG_ADDRESS_STRING},
new int[] {
R.id.name_shop, R.id.address_shop
});
list.setAdapter(adapter);
将此代码放入onPostExecute
> HashMap<String, String> map = new HashMap<String, String>();
> map.put(TAG_NAME, name);
> map.put(TAG_ADDRESS_STRING, address_string);
> oslist.add(map);
> list=(ListView)findViewById(R.id.list);
> ListAdapter adapter = new SimpleAdapter(ShopsActivity.this, oslist,
> R.layout.listview_item_row,
> new String[] { TAG_NAME, TAG_ADDRESS_STRING }, new int[] {
> R.id.name_shop, R.id.address_shop});
> list.setAdapter(adapter);
UI修改仅在UI线程中完成。您正在使用在不同线程中运行的doInbackGround方法更新ListView。。按以下方式更改代码。。
runOnUiThread(new Runnable() {
@Override
public void run() {
list.setAdapter(adapter);
}
});
或者在AsynchTask
的onPostExecute
()方法中更新listview,如...
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
list = (ListView) findViewById(R.id.list);
ListAdapter adapter = new SimpleAdapter(ShopsActivity.this, oslist,
R.layout.listview_item_row, new String[] { TAG_NAME,
TAG_ADDRESS_STRING }, new int[] { R.id.name_shop,
R.id.address_shop });
list.setAdapter(adapter);
}
我有一个错误,只有创建视图层次结构的原始线程才能接触它的视图。但我不知道如何在多线程中处理ui,比如列表视图,我也不知道该放在哪里。
我正在使用MultipartEntityBuilder将图像发送到php服务器,我将图片和视频发送到设备摄像头的目录中,我需要将文件保存在新的ArrayList上并将其发送到php服务器。 总是报告错误: 我的代码 更新
我在Android中构建了一个简单的音乐播放器。每首歌的视图都包含一个SeekBar,实现如下: 这很好用。现在我想要一个计时器来计算歌曲进度的秒/分钟。因此,我在布局中放置了一个文本视图,在onCreate()中使用findViewById()获取它,并在。设置进度(pos): 但最后一行给了我一个例外: Android看法ViewRoot$CalledFromErrorThreadExcept
这是我所有的代码。
我正在为小应用程序设计UnitTest。这个应用程序说明您使用的是什么命理数字出生日期。我有一个问题,我该调用检查数字并将值放入TextView的方法。我有一个错误 ' 我的布局:` 我想测试的方法:'
得到一个错误:android.view.ViewRootImpl$CalledFromWrongThreadException:只有创建视图层次结构的原始线程可以触摸它的视图。 我真的不知道为什么。 谢谢帮忙。