java–android.view.ViewRoot $CalledFromWrongThreadException:

java–android.view.ViewRoot $CalledFromWrongThreadException:,第1张

概述我在logcat中收到错误说明:java.net.UnknownHostException:apps.example.com09-1314:57:28.970:W/System.err(3823):android.view.ViewRoot$CalledFromWrongThreadException:Onlytheoriginalthreadthatcreatedaviewhierarchycantouchitsviews.谁看过这个吗?我

我在logcat中收到错误说明:

java.net.UnkNownHostException: apps.example.com09-13 14:57:28.970: W/System.err(3823): androID.vIEw.VIEwRoot$CalledFromWrongThreadException: Only the original thread that created a vIEw hIErarchy can touch its vIEws.

谁看过这个吗?我不确定究竟是什么导致了这个问题.

完整的LOGCAT:

09-13 14:57:27.085: D/dalvikvm(3823): GC_EXTERNAL_ALLOC freed 46K, 43% free 3084K/5379K, external 0K/0K, paused 77ms09-13 14:57:27.165: D/dalvikvm(3823): GC_EXTERNAL_ALLOC freed 1K, 43% free 3084K/5379K, external 1K/513K, paused 71ms09-13 14:57:27.285: D/dalvikvm(3823): GC_EXTERNAL_ALLOC freed 5K, 43% free 3091K/5379K, external 2167K/2538K, paused 64ms09-13 14:57:27.290: I/System.out(3823): Sim State509-13 14:57:27.300: D/Network Availability(3823): CONNECTED09-13 14:57:27.300: D/Network Availability(3823): NetworkInfo: type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecifIEd), extra: (none), roaming: false, failover: false, isAvailable: true09-13 14:57:27.375: D/CliPBOARD(3823): HIDe Clipboard dialog at Starting input: finished by someone else... !09-13 14:57:28.765: D/MDN(3823): 305586109209-13 14:57:28.770: I/System.out(3823): MDN%1234 = 76809-13 14:57:28.770: I/System.out(3823): MDN/1234 = 247638609-13 14:57:28.770: D/iccid(3823): 890126083212086796709-13 14:57:28.770: I/System.out(3823): iccid%1234 = 58709-13 14:57:28.770: I/System.out(3823): iccid/1234 = 721333941014657009-13 14:57:28.815: D/dalvikvm(3823): GC_EXTERNAL_ALLOC freed 20K, 42% free 3129K/5379K, external 2664K/3205K, paused 38ms09-13 14:57:28.855: I/url...(3823): https://apps.example.com/REST/phonesettings09-13 14:57:28.885: V/httpPost(3823): org.apache.http.clIEnt.methods.httpPost@4053805809-13 14:57:28.970: E/DataSettings(3823): java.net.UnkNownHostException: apps.example.com09-13 14:57:28.970: W/System.err(3823): androID.vIEw.VIEwRoot$CalledFromWrongThreadException: Only the original thread that created a vIEw hIErarchy can touch its vIEws.09-13 14:57:28.970: W/System.err(3823):     at androID.vIEw.VIEwRoot.checkThread(VIEwRoot.java:3088)09-13 14:57:28.970: W/System.err(3823):     at androID.vIEw.VIEwRoot.requestLayout(VIEwRoot.java:669)09-13 14:57:28.970: W/System.err(3823):     at androID.vIEw.VIEw.requestLayout(VIEw.java:8406)09-13 14:57:28.980: W/System.err(3823):     at androID.vIEw.VIEw.requestLayout(VIEw.java:8406)09-13 14:57:28.980: W/System.err(3823):     at androID.vIEw.VIEwGroup.removeAllVIEws(VIEwGroup.java:2261)09-13 14:57:28.980: W/System.err(3823):     at com.androID.internal.policy.impl.PhoneWindow.setContentVIEw(PhoneWindow.java:213)09-13 14:57:28.980: W/System.err(3823):     at androID.app.Activity.setContentVIEw(Activity.java:1663)09-13 14:57:28.980: W/System.err(3823):     at com.project.example.app.UpdateActivity.tryagain(UpdateActivity.java:654)09-13 14:57:28.980: W/System.err(3823):     at com.project.example.app.UpdateActivity$NetworkTask.getqueryResults(UpdateActivity.java:569)09-13 14:57:28.980: W/System.err(3823):     at com.project.example.app.UpdateActivity$NetworkTask.doInBackground(UpdateActivity.java:382)09-13 14:57:28.980: W/System.err(3823):     at com.project.example.app.UpdateActivity$NetworkTask.doInBackground(UpdateActivity.java:1)09-13 14:57:28.980: W/System.err(3823):     at androID.os.AsyncTask.call(AsyncTask.java:185)09-13 14:57:28.980: W/System.err(3823):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)09-13 14:57:28.980: W/System.err(3823):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)09-13 14:57:28.980: W/System.err(3823):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)09-13 14:57:28.980: W/System.err(3823):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)09-13 14:57:28.980: W/System.err(3823):     at java.lang.Thread.run(Thread.java:1019)09-13 14:57:34.089: V/In the parser(3823): Now09-13 14:57:34.089: W/System.err(3823): org.xml.sax.SAXException: No input specifIEd.09-13 14:57:34.100: W/System.err(3823):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:285)09-13 14:57:34.100: W/System.err(3823):     at com.project.example.app.XmlParserHandlerFinal.getqueryResponse(XmlParserHandlerFinal.java:170)09-13 14:57:34.100: W/System.err(3823):     at com.project.example.app.UpdateActivity$NetworkTask.success(UpdateActivity.java:598)09-13 14:57:34.105: W/System.err(3823):     at com.project.example.app.UpdateActivity$NetworkTask.onPostExecute(UpdateActivity.java:590)09-13 14:57:34.105: W/System.err(3823):     at com.project.example.app.UpdateActivity$NetworkTask.onPostExecute(UpdateActivity.java:1)09-13 14:57:34.105: W/System.err(3823):     at androID.os.AsyncTask.finish(AsyncTask.java:417)09-13 14:57:34.105: W/System.err(3823):     at androID.os.AsyncTask.access0(AsyncTask.java:127)09-13 14:57:34.105: W/System.err(3823):     at androID.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)09-13 14:57:34.105: W/System.err(3823):     at androID.os.Handler.dispatchMessage(Handler.java:99)09-13 14:57:34.105: W/System.err(3823):     at androID.os.Looper.loop(Looper.java:130)09-13 14:57:34.105: W/System.err(3823):     at androID.app.ActivityThread.main(ActivityThread.java:3691)09-13 14:57:34.105: W/System.err(3823):     at java.lang.reflect.Method.invokeNative(Native Method)09-13 14:57:34.105: W/System.err(3823):     at java.lang.reflect.Method.invoke(Method.java:507)09-13 14:57:34.105: W/System.err(3823):     at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)09-13 14:57:34.105: W/System.err(3823):     at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:665)09-13 14:57:34.105: W/System.err(3823):     at dalvik.system.NativeStart.main(Native Method)09-13 14:57:34.174: I/dalvikvm(3823): Failed resolving Lcom/project/example/app/NotificationActivityForMultiProf; interface 26 'LandroID/content/ClipboardManager$OnPrimaryClipChangedListener;'09-13 14:57:34.174: W/dalvikvm(3823): @R_301_6862@ of class 'Lcom/project/example/app/NotificationActivityForMultiProf;' Failed09-13 14:57:34.174: E/dalvikvm(3823): Could not find class 'com.project.example.app.NotificationActivityForMultiProf', referenced from method com.project.example.app.ConfigFinalActivity.showNotification09-13 14:57:34.174: W/dalvikvm(3823): VFY: unable to resolve const-class 561 (Lcom/project/example/app/NotificationActivityForMultiProf;) in Lcom/project/example/app/ConfigFinalActivity;09-13 14:57:34.174: D/dalvikvm(3823): VFY: replacing opcode 0x1c at 0x004b09-13 14:57:34.174: D/dalvikvm(3823): VFY: dead code 0x004d-007f in Lcom/project/example/app/ConfigFinalActivity;.showNotification ()V09-13 14:57:36.494: W/KeyCharacterMap(3823): No keyboard for ID 009-13 14:57:36.494: W/KeyCharacterMap(3823): Using default keymap: /system/usr/keychars/qwerty.kcm.bin09-13 14:57:36.785: D/CliPBOARD(3823): HIDe Clipboard dialog at Starting input: finished by someone else... !

资源:

    // AsyncTask to call web service    public class NetworkTask extends AsyncTask<String, Integer, inputStream> {        @OverrIDe        protected voID onPreExecute() {            super.onPreExecute();            //        }        @OverrIDe        protected inputStream doInBackground(String... params) {            int result = 0;            {                Log.i("url...", Base_URL);                try {                    stream = getqueryResults(Base_URL);                } catch (SocketTimeoutException e1) {                    // Todo auto-generated catch block                    e1.printstacktrace();                } catch (SSLException e1) {                    // Todo auto-generated catch block                    e1.printstacktrace();                } catch (IOException e1) {                    // Todo auto-generated catch block                    e1.printstacktrace();                } catch (SAXException e1) {                    // Todo auto-generated catch block                    e1.printstacktrace();                } catch (Exception e1) {                    // Todo auto-generated catch block                    e1.printstacktrace();                }                // The code below plays a ST Promo animation                // prior to displaying update success or failure message                for (int incr = 0; incr < 2; incr++) {                    // Sleep for 1/2 second                    // Invoke UI to change updating text to show 1 dot                    // And Increasing the level to reduce the amount of clipPing                    // and                    // slowly reveals the hand image                    publishProgress(R.drawable.loading_full,                            R.drawable.loading_empty, R.drawable.loading_empty,                            R.drawable.loading_empty, R.drawable.loading_empty);                    try {                        Thread.sleep(500);                    } catch (InterruptedException e) {                        // Todo auto-generated catch block                    }                    publishProgress(R.drawable.loading_full,                            R.drawable.loading_full, R.drawable.loading_empty,                            R.drawable.loading_empty, R.drawable.loading_empty);                    try {                        Thread.sleep(500);                    } catch (InterruptedException e) {                        // Todo auto-generated catch block                    }                    publishProgress(R.drawable.loading_full,                            R.drawable.loading_full, R.drawable.loading_full,                            R.drawable.loading_empty, R.drawable.loading_empty);                    try {                        Thread.sleep(500);                    } catch (InterruptedException e) {                        // Todo auto-generated catch block                    }                    publishProgress(R.drawable.loading_full,                            R.drawable.loading_full, R.drawable.loading_full,                            R.drawable.loading_full, R.drawable.loading_empty);                    try {                        Thread.sleep(500);                    } catch (InterruptedException e) {                        // Todo auto-generated catch block                    }                    publishProgress(R.drawable.loading_full,                            R.drawable.loading_full, R.drawable.loading_full,                            R.drawable.loading_full, R.drawable.loading_full);                    // Sleep for 1/2 second                    try {                        Thread.sleep(500);                    } catch (InterruptedException e) {                        // Todo auto-generated catch block                    }                }                    return stream;            }        }        /*         * Sends a query to server and gets back the parsed results in a bundle         * urlqueryString - URL for calling the webservice         */        protected synchronized inputStream getqueryResults(String urlqueryString)                throws IOException, SAXException, SSLException,                SocketTimeoutException, Exception {            try {                // httpsURLConnection https = null;                String uri = urlqueryString;                List<nameValuePair> nameValuePairs = new ArrayList<nameValuePair>();                BasicnameValuePair mdn1, mdn2,ID1,ID2;                if (MDN.equals("")) {                    mdn1 = new BasicnameValuePair("mdn1", null);                    mdn2 = new BasicnameValuePair("mdn2", null);                } else {                    mdn1 = new BasicnameValuePair("mdn1", mdN1.toString());                    mdn2 = new BasicnameValuePair("mdn2", mdN2.toString());                }                BasicnameValuePair car = new BasicnameValuePair("car", caR);                if (iccid.equals("")) {                     ID1 = new BasicnameValuePair("ID1", null);                     ID2 = new BasicnameValuePair("ID2", null);                } else {                     ID1 = new BasicnameValuePair("ID1",                            ID1.toString());                     ID2 = new BasicnameValuePair("ID2",                            ID2.toString());                }                nameValuePairs.add(mdn1);                nameValuePairs.add(mdn2);                nameValuePairs.add(car);                nameValuePairs.add(ID1);                nameValuePairs.add(ID2);                UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(                        nameValuePairs, "ISO-8859-1");                KeyStore trustStore = KeyStore.getInstance(KeyStore                        .getDefaultType());                trustStore.load(null, null);                SSLSocketFactory sf = new MySSLSocketFactory(trustStore);                sf.setHostnameVerifIEr(SSLSocketFactory.ALLOW_ALL_HOSTname_VERIFIER);                httpParams params = new BasichttpParams();                httpProtocolParams.setVersion(params, httpVersion.http_1_1);                httpProtocolParams.setContentCharset(params, http.UTF_8);                SchemeRegistry registry = new SchemeRegistry();                registry.register(new Scheme("http", PlainSocketFactory                        .getSocketFactory(), 80));                registry.register(new Scheme("https", sf, 443));                ClIEntConnectionManager ccm = new ThreadSafeClIEntConnManager(                        params, registry);                httpClIEnt httpClIEnt = new DefaulthttpClIEnt(ccm, params);                params = httpClIEnt.getParams();                httpClIEntParams.setRedirecting(params, true);                httpPost httpPost = new httpPost(uri);                httpPost.addheader("Authorization",                        getB64Auth("nmundru", "abc123"));                httpPost.setheader("Content-Type", "text/plain; charset=utf-8");                Log.v("httpPost", httpPost.toString());                httpPost.setEntity(urlEncodedFormEntity);                httpResponse httpResponse = httpClIEnt.execute(httpPost);                System.out.println("response...." + httpResponse.toString());                Log.v("response...", httpResponse.toString());                stream = httpResponse.getEntity().getContent();                // save the inputStream in a file                try {                    fileOutputStream fOut = openfileOutput("settings.xml",                            Context.MODE_WORLD_READABLE);                    DatainputStream in = new DatainputStream(stream);                    BufferedReader br = new BufferedReader(                            new inputStreamReader(in));                    String strline;                    while ((strline = br.readline()) != null) {                         System.out.println(strline); //to print the response                        // in logcat                        fOut.write(strline.getBytes());                    }                    fOut.close();                } catch (fileNotFoundException e) {                    e.printstacktrace();                } catch (IOException e) {                    e.printstacktrace();                }                fis = openfileinput("settings.xml");            } catch (Exception e) {                Log.e(LOG_TAG, e.toString());                tryagain();            } finally {                // https.disconnect();            }            return stream;        }        private String getB64Auth(String login, String pass) {            String source = login + ":" + pass;            String ret = "Basic "                    + Base64.encodetoString(source.getBytes(), Base64.URL_SAFE                            | Base64.NO_WRAP);            return ret;        }        @OverrIDe        protected voID onPostExecute(inputStream stream) {            super.onPostExecute(stream);            // This method is called to parse the response and save the            // ArrayLists            success();        }        public voID success() {            // to parse the response            try {                handler.getqueryResponse(fis);            } catch (SAXException e) {                // Todo auto-generated catch block                e.printstacktrace();            } catch (IOException e) {                // Todo auto-generated catch block                e.printstacktrace();            }            // set method to save the ArryaLists from the parser            setArrayList();            Intent i = new Intent(UpdateActivity.this, ConfigFinalActivity.class);            startActivity(i);            finish();        }        // Framework UI thread method corresponding to publishProgress call in        // worker thread        protected voID onProgressUpdate(Integer... progress) {            // Call function to update image vIEw            setProgressimgVIEw(progress[0], progress[1], progress[2],                    progress[3], progress[4]);        }    }    private voID setProgressimgVIEw(Integer imgVIEwID1, Integer imgVIEwID2,            Integer imgVIEwID3, Integer imgVIEwID4, Integer imgVIEwID5) {        // update image vIEw with the updating dots        // reset vIEw layout in case orIEntation while updating        setContentVIEw(R.layout.updating);        mProgressImagevIEw1 = (ImageVIEw) findVIEwByID(R.ID.loading_empty1);        mProgressImagevIEw2 = (ImageVIEw) findVIEwByID(R.ID.loading_empty2);        mProgressImagevIEw3 = (ImageVIEw) findVIEwByID(R.ID.loading_empty3);        mProgressImagevIEw4 = (ImageVIEw) findVIEwByID(R.ID.loading_empty4);        mProgressImagevIEw5 = (ImageVIEw) findVIEwByID(R.ID.loading_empty5);        mProgressImagevIEw1.setimageResource(imgVIEwID1);        mProgressImagevIEw2.setimageResource(imgVIEwID2);        mProgressImagevIEw3.setimageResource(imgVIEwID3);        mProgressImagevIEw4.setimageResource(imgVIEwID4);        mProgressImagevIEw5.setimageResource(imgVIEwID5);    }    @OverrIDe    protected voID onRestart() {        super.onRestart();        if (mErrorAlert != null)            mErrorAlert.dismiss();    }    public voID tryagain() {        // displaying final layout after failure of pre-ICS automatic settings        // update        setContentVIEw(R.layout.tryagain);        String tryAgainText = "";        CharSequence styledTryAgainText;        tryAgainText = String.format(getString(R.string.tryagain_text1),                TotalSteps);        styledTryAgainText = HTML.fromHTML(tryAgainText);        TextVIEw tryAgain1 = (TextVIEw) findVIEwByID(R.ID.tryagain_text1);        tryAgain1.setText(styledTryAgainText);        tryAgainText = String.format(getString(R.string.tryagain_text2),                TotalSteps);        styledTryAgainText = HTML.fromHTML(tryAgainText);        TextVIEw tryAgain2 = (TextVIEw) findVIEwByID(R.ID.tryagain_text2);        tryAgain2.setText(styledTryAgainText);        tryAgainText = String.format(getString(R.string.tryagain_text3),                TotalSteps);        styledTryAgainText = HTML.fromHTML(tryAgainText);        TextVIEw tryAgain3 = (TextVIEw) findVIEwByID(R.ID.tryagain_text3);        tryAgain3.setText(styledTryAgainText);    }    private voID assistUpdate() {        // displaying final layout after pre-ICS automatic settings update        setContentVIEw(R.layout.assist_update);        assist_update_btn = (button) findVIEwByID(R.ID.assist_update_btn);        assist_update_btn.setonClickListener((OnClickListener) this);    }    public voID success() {        // to parse the response        try {            handler.getqueryResponse(fis);        } catch (SAXException e) {            // Todo auto-generated catch block            e.printstacktrace();        } catch (IOException e) {            // Todo auto-generated catch block            e.printstacktrace();        }        // set method to save the ArryaLists from the parser        setArrayList();        Intent i = new Intent(this, ConfigFinalActivity.class);        startActivity(i);        finish();    }    public String getResult() {        return result;    }}

解决方法:

       protected inputStream doInBackground(String... params)

你有

      stream = getqueryResults(Base_URL);

然后在getqueryResults中你有

    catch (Exception e) {            Log.e(LOG_TAG, e.toString());            tryagain();        }

在你的尝试中

    public voID tryagain() {    setContentVIEw(R.layout.tryagain);

您无法从后台线程更新/访问ui.这就是你得到那个例外的原因.

你还应该考虑你的设计.如果您在同一个活动中多次使用setContentVIEw,请重新考虑您的设计.

而不是多个try块有一个try块和多个catch块.

不确定你想要什么,但你不止一次调用publishProgress.

总结

以上是内存溢出为你收集整理的java – android.view.ViewRoot $CalledFromWrongThreadException:全部内容,希望文章能够帮你解决java – android.view.ViewRoot $CalledFromWrongThreadException:所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1097114.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存