Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用WebAppInterface(Javascript界面)操作Android视图?_Javascript_Android - Fatal编程技术网

如何使用WebAppInterface(Javascript界面)操作Android视图?

如何使用WebAppInterface(Javascript界面)操作Android视图?,javascript,android,Javascript,Android,当我的Android WebView运行时,我使用WebAppInterface从Javascript获取数据。在我从Javascript中获取一些数据之后,如何操作Android视图 public class WebAppInterface { Context mContext; /** Instantiate the interface and set the context */ WebAppInterface(Context c) { mCont

当我的Android WebView运行时,我使用WebAppInterface从Javascript获取数据。在我从Javascript中获取一些数据之后,如何操作Android视图

public class WebAppInterface {
    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }

    //This function can only be called by javascript
    @android.webkit.JavascriptInterface
    public void sendDataToJava(float currTime, String url) {

        videoUrl = url;
        videoTime = Math.round(currTime);

        //use references to views to change stuff
        videoView.seekTo(videoTime); //ERROR HERE
       //ERROR: Only the original thread that created a view hierarchy can touch its views


    }


}

您需要保留对视图包含活动的引用,并使用runnable调用activity.runOnUiThread方法调用与视图相关的代码

它看起来像这样:

public class WebAppInterface {
    Context mContext;
    Activity mActivity;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c, Activity a) {
        mContext = c;
        mActivity = a;

    }

    //This function can only be called by javascript
    @android.webkit.JavascriptInterface
    public void sendDataToJava(float currTime, String url) {

        videoUrl = url;
        videoTime = Math.round(currTime);

        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                videoView.seekTo(videoTime); 
            }
        });
    }

}