参见英文答案 > Android “Only the original thread that created a view hierarchy can touch its views.” 22个
我对这个类有一个例外:“只有创建视图层次结构的原始线程才能触及它的视图”.
我知道这可能是我使用异步的事实,但我是androID dev中的新手,我甚至没有安排它甚至在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 #112-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.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.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.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 more12-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 here12-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 here12-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.access0(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修改只在Ui thread中完成.你正在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); }
总结 以上是内存溢出为你收集整理的android – 只有创建视图层次结构的原始线程才能触及其视图异常全部内容,希望文章能够帮你解决android – 只有创建视图层次结构的原始线程才能触及其视图异常所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)