Android的待定异常

Android的待定异常

本文介绍了Android的待定异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是一个混合的HTML5 + JS +安卓这在web视图通过JS界面运行和交流这么多的Andorid。

My app is a hybrid html5 + JS + android which run in webview and communicate so much with Andorid through JS interface.

我得到了一些报告后在某些设备上2秒失败。我加ACRA这样我就可以拿到报告,但我没有得到任何东西。

I got some report that it fails after 2 second on some devices. I added ACRA so I can get reports, but I didn't get any thing.

于是,我就测试它自己,要抛出一个异常,我添加了一个code操纵意见的JavaScript接口的功能主线程这是在HTML中的按钮触发的。这引发一个异常:只有创建视图层次可以触摸其观点原来的线程现在的应用程序开始,当我点击,出现异常的按钮,它的出口和ACRA发送报告

So I tried to test it myself, To raise an exception I add a code that manipulate views of main thread in Javascript interface's function which was triggered with a button in html. This raise an exception: Only the original thread that created a view hierarchy can touch its views. Now the app start and when I tap the button it exit with exception and ACRA send the report.

然后我把操纵code在JS接口的功能这是应用程序启动后立即调用。现在的应用程序是第二后关闭。但ACRA不发送任何错误。即使是的ExceptionHandler没赶上,但赶上第一案。

Then I put the manipulation code in a function of JS interface which was called immediately after the app start. now app is closed after a second. But ACRA doesn't send any error. Even an ExceptionHandler didn't catch it but catch the first case.

这是第二个场景的日志:

This is the log of second scenario:

59): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@43f8d990 (uid=10019 pid=329)
12-15 01:18:07.047: WARN/dalvikvm(409): JNI WARNING: JNI method called with exception raised
12-15 01:18:07.047: WARN/dalvikvm(409): in Landroid/webkit/BrowserFrame;.stringByEvaluatingJavaScriptFromString (Ljava/lang/String;)Ljava/lang/String; (NewString)
12-15 01:18:07.057: WARN/dalvikvm(409): Pending exception is:
12-15 01:18:07.057: INFO/dalvikvm(409): Landroid/view/ViewRoot$CalledFromWrongThreadException;: Only the original thread that created a view hierarchy can touch its views

我不知道这个未决的例外是什么?我无法找到网页上的任何东西。我不知道为什么不ACRA或异常处理程序抓住它?

I don't know what this pending exception is? I couldn't find anything on web. I wonder why dont ACRA or Exception Handler catch it?

class JavaScriptInterface
{
    MyActivity parent;
    JavaScriptInterface(MyActivity parent)
    {
        this.parent = parent;
    }

    public void immediatelyCalled()
    // Webview load index.html in oncreate of activity and js inside html calls this function immediately
    {
        parent.textview1.setText("test");
        // raise an exception which ACRA or Exception Handler dont catch
        // Problem is here
    }

    public void buttonCalled()
    // This is called when a button is tapped in html
    {
        parent.textview1.setText("test");
        // raise an exception which Exception Handler and ACRA catch
    }
}

这是我的活动:

public class MyActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
setContentView(R.layout.main);

        class MyExceptionHandler implements Thread.UncaughtExceptionHandler
        {
            @Override
            public void uncaughtException(Thread thread, Throwable throwable)
            {
                Log.d("ExceptionHandler", "Caught exception: " + throwable.getClass().getName() + ": " + throwable.getMessage());
            }
        }

        MyExceptionHandler handler = new MyExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(handler);


        this.wv = (WebView) findViewById(R.id.webview);
        this.wv.addJavascriptInterface(new JavaScriptInterface(this), "android");
        this.ws = this.wv.getSettings();
        this.ws.setJavaScriptEnabled(true);
        this.wv.loadUrl("file:///android_asset/index.html");

    }
}

这是index.html的一部分:

this is part of index.html:

<script type="text/javascript">
android.immediatelyCalled();
</scritp>
<button onclick="android.buttonCalled()"></button>

测试在AVD 2.2和Xperai弧4.0.3,两者相同

Tested on AVD 2.2 and Xperai Arc 4.0.3, Both the same

推荐答案

我觉得方法immediatelyCalled()被调用在另一个线程比你正在创建MyActivity父之一(UI线程,您的应用程序主线程)。所以它分配给UI线程并没有任何与正在运行的JavaScript code的过程中未捕获的异常处理程序,不能捞的setText此异常发生的历史(访问属于另一个线程的视图)。

I think the method immediatelyCalled() is called in another thread than the one you are creating "MyActivity parent" (a UI thread, your apps main thread). So an uncaught exception handler which you assign to the ui thread and which has nothing to do with the process which is running the javascript code, can not catch this exception occuring at setText (accessing a view which belongs to another thread).

我想你能赶上课程的异常,如果您附上parent.textview1.setText(测试);在try catch块。
您应该能够避免异常,如果您修改这种方式的setText调用:

I think you could catch the exception of course if you enclose parent.textview1.setText("test"); in a try catch block.You should be able to avoid the exception if you modify the setText call in this way:

parent.textView1.post(new Runnable() {
    public void run() {
        parent.textview1.setText("test");
    }
});

你必须标记你的父母参数作为决赛。
不幸的是我没有日食这里测试,它是为时已晚:)
祝你好运!

You'll have to mark your parent parameter as final.Unfortunately i have no eclipse here to test and it's late already :)good luck!

这篇关于Android的待定异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 20:24