1

我正在成功访问 asmx Web 服务,但是当没有互联网时,应用程序崩溃。我已经尝试了所有方法来检测和处理异常,但都是徒劳的。
请检查我的代码和 logcat。
日志猫:

10-13 14:22:34.748: W/System.err(275):  at java.lang.Thread.run(Thread.java:1096)
10-13 14:22:34.759: W/dalvikvm(275): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
10-13 14:22:34.858: D/dalvikvm(275): GC_FOR_MALLOC freed 4242 objects / 236920 bytes in 62ms
10-13 14:22:34.858: E/AndroidRuntime(275): FATAL EXCEPTION: AsyncTask #1
10-13 14:22:34.858: E/AndroidRuntime(275): java.lang.RuntimeException: An error occured while executing doInBackground()
10-13 14:22:34.858: E/AndroidRuntime(275):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
10-13 14:22:34.858: E/AndroidRuntime(275):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-13 14:22:34.858: E/AndroidRuntime(275):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-13 14:22:34.858: E/AndroidRuntime(275):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-13 14:22:34.858: E/AndroidRuntime(275):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-13 14:22:34.858: E/AndroidRuntime(275):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
10-13 14:22:34.858: E/AndroidRuntime(275):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
10-13 14:22:34.858: E/AndroidRuntime(275):  at java.lang.Thread.run(Thread.java:1096)
10-13 14:22:34.858: E/AndroidRuntime(275): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
10-13 14:22:34.858: E/AndroidRuntime(275):  at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
10-13 14:22:34.858: E/AndroidRuntime(275):  at android.view.ViewRoot.requestLayout(ViewRoot.java:594)
10-13 14:22:34.858: E/AndroidRuntime(275):  at android.view.View.requestLayout(View.java:8125)
10-13 14:22:34.858: E/AndroidRuntime(275):  at android.view.View.requestLayout(View.java:8125)
10-13 14:22:34.858: E/AndroidRuntime(275):  at android.view.View.requestLayout(View.java:8125)
10-13 14:22:34.858: E/AndroidRuntime(275):  at android.view.View.requestLayout(View.java:8125)
10-13 14:22:34.858: E/AndroidRuntime(275):  at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:254)
10-13 14:22:34.858: E/AndroidRuntime(275):  at android.view.View.requestLayout(View.java:8125)
10-13 14:22:34.858: E/AndroidRuntime(275):  at android.widget.TextView.checkForRelayout(TextView.java:5371)
10-13 14:22:34.858: E/AndroidRuntime(275):  at android.widget.TextView.setText(TextView.java:2688)
10-13 14:22:34.858: E/AndroidRuntime(275):  at android.widget.TextView.setText(TextView.java:2556)
10-13 14:22:34.858: E/AndroidRuntime(275):  at android.widget.TextView.setText(TextView.java:2531)
10-13 14:22:34.858: E/AndroidRuntime(275):  at com.bibleverses.BibleverseActivity$ws.doInBackground(BibleverseActivity.java:190)
10-13 14:22:34.858: E/AndroidRuntime(275):  at com.bibleverses.BibleverseActivity$ws.doInBackground(BibleverseActivity.java:1)
10-13 14:22:34.858: E/AndroidRuntime(275):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-13 14:22:34.858: E/AndroidRuntime(275):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-13 14:22:34.858: E/AndroidRuntime(275):  ... 4 more
10-13 14:22:35.158: W/IInputConnectionWrapper(275): showStatusIcon on inactive InputConnection
10-13 14:22:36.009: E/WindowManager(275): Activity com.bibleverses.BibleverseActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f14620 that was originally added here
10-13 14:22:36.009: E/WindowManager(275): android.view.WindowLeaked: Activity com.bibleverses.BibleverseActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44f14620 that was originally added here
10-13 14:22:36.009: E/WindowManager(275):   at android.view.ViewRoot.<init>(ViewRoot.java:247)
10-13 14:22:36.009: E/WindowManager(275):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
10-13 14:22:36.009: E/WindowManager(275):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
10-13 14:22:36.009: E/WindowManager(275):   at android.view.Window$LocalWindowManager.addView(Window.java:424)
10-13 14:22:36.009: E/WindowManager(275):   at android.app.Dialog.show(Dialog.java:241)
10-13 14:22:36.009: E/WindowManager(275):   at com.bibleverses.BibleverseActivity$ws.onPreExecute(BibleverseActivity.java:240)
10-13 14:22:36.009: E/WindowManager(275):   at android.os.AsyncTask.execute(AsyncTask.java:391)
10-13 14:22:36.009: E/WindowManager(275):   at com.bibleverses.BibleverseActivity$1.onClick(BibleverseActivity.java:107)
10-13 14:22:36.009: E/WindowManager(275):   at android.view.View.performClick(View.java:2408)
10-13 14:22:36.009: E/WindowManager(275):   at android.view.View$PerformClick.run(View.java:8816)
10-13 14:22:36.009: E/WindowManager(275):   at android.os.Handler.handleCallback(Handler.java:587)
10-13 14:22:36.009: E/WindowManager(275):   at android.os.Handler.dispatchMessage(Handler.java:92)
10-13 14:22:36.009: E/WindowManager(275):   at android.os.Looper.loop(Looper.java:123)
10-13 14:22:36.009: E/WindowManager(275):   at android.app.ActivityThread.main(ActivityThread.java:4627)
10-13 14:22:36.009: E/WindowManager(275):   at java.lang.reflect.Method.invokeNative(Native Method)
10-13 14:22:36.009: E/WindowManager(275):   at java.lang.reflect.Method.invoke(Method.java:521)
10-13 14:22:36.009: E/WindowManager(275):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-13 14:22:36.009: E/WindowManager(275):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-13 14:22:36.009: E/WindowManager(275):   at dalvik.system.NativeStart.main(Native Method)
10-13 14:22:40.798: I/Process(275): Sending signal. PID: 275 SIG: 9

代码:

private class ws extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {
        // try {

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        String bk = ed.getText().toString();


        tx1 = (EditText) findViewById(R.id.editText1);
        String Chapter = tx1.getText().toString();
        int chp = Integer.parseInt(Chapter);

        EditText tx2 = (EditText) findViewById(R.id.editText2);
        String verse = tx2.getText().toString();
        int vs = Integer.parseInt(verse);

        //
        //

        request.addProperty("BookTitle", bk);
        request.addProperty("chapter", chp);
        request.addProperty("Verse", vs);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER12);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        try {

            androidHttpTransport.call(SOAP_ACTION, envelope);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            tv.setText(e.toString());
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Object result = null;
        try {
            result = (Object) envelope.getResponse();
        } catch (SoapFault e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        String html = result.toString();

        Document doc = Jsoup.parse(html);

        Elements result2 = doc.select("biblewords");
        String result3 = result2.toString();
        String res = Html.fromHtml(result3).toString();



        return res;
    }

    @Override
    protected void onPostExecute(String res) {
        dialog.dismiss();
        tv.setText(res); // txt.setText(result);

    }
4

3 回答 3

0

我相信你的问题在于这一行:

} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    tv.setText(e.toString());

注意与tv.setText(e.toString());? 您正在 AsyncTask 线程中设置 UI 值。我相信这是被禁止的。

尝试将其更改为:

return e.toString();
于 2012-10-13T11:41:10.893 回答
0

doInBackgorund()是一个非 UI 线程,您无法访问其中的 UI 元素。doInBackground() 中的代码在一个单独的非 UI 线程上运行,该线程无法访问布局中定义的 UI 元素。

tv.setText(e.toString());因此,删除从 doInBackground() 内部访问 UI 元素的代码“ ”,而是将其放入内部onPostExecute(),这是一个 UI 线程,在 doInBackground() 完成后台处理后调用。

于 2012-10-13T13:37:28.380 回答
0

您从AsyncTask线程中对 UI 进行更改。从除 Android 主线程之外的任何地方更改 UI 通常是一个坏主意,但在 AsyncTasks 的情况下,明确不允许这样做。

用于onProgressUpdate向主线程获取消息并更新 UI。

于 2012-10-13T11:39:50.687 回答