Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.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
Android-无法使用JavascriptInterface访问actionbar_Javascript_Android_Interface_Webview_Android Actionbar - Fatal编程技术网

Android-无法使用JavascriptInterface访问actionbar

Android-无法使用JavascriptInterface访问actionbar,javascript,android,interface,webview,android-actionbar,Javascript,Android,Interface,Webview,Android Actionbar,我的WebView的JavascriptInterface有一个奇怪的问题。我想用Javascript更改actionbar中的标题。它应该在调用onPageFinished时发生。什么也没发生(有时新标题的第一个字母后面跟着“…”)。但是:当我按下Home按钮,然后返回应用程序时,标题就改变了。 如何立即更改标题 在我的例子中,我调用 w1.loadUrl("http://www.google.com"); 在onpagefinished中 w1.loadUrl("javascript:w

我的WebView的JavascriptInterface有一个奇怪的问题。我想用Javascript更改actionbar中的标题。它应该在调用onPageFinished时发生。什么也没发生(有时新标题的第一个字母后面跟着“…”)。但是:当我按下Home按钮,然后返回应用程序时,标题就改变了。 如何立即更改标题

在我的例子中,我调用

w1.loadUrl("http://www.google.com");
在onpagefinished中

 w1.loadUrl("javascript:window.jsin.abtitle(document.title)");
将标题更改为html标题(在本例中为“Google”)

页面加载完成后,actionbar中的标题为“G…” 按Home按钮,返回应用程序标题为“Google”

这是我的活动:

package com.example.test;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

WebView w1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    w1=(WebView)findViewById(R.id.webView1);
    w1.getSettings().setJavaScriptEnabled(true);
    w1.setWebViewClient(wc1);
    w1.addJavascriptInterface(new MyJavascriptInterface(this), "jsin");
    w1.loadUrl("http://www.google.com");
}


public WebViewClient wc1 = new WebViewClient(){

    @Override
    public void onPageFinished(WebView view, String url) {
         w1.loadUrl("javascript:window.jsin.abtitle(document.title)");
        super.onPageFinished(view, url);
    };

};



public class MyJavascriptInterface{

Context mContext;

     public MyJavascriptInterface(Context c) {
            mContext = c;
     }

    @JavascriptInterface
    public void abtitle(String s){
        ((MainActivity)mContext).getActionBar().setTitle(s);
    }
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}



}
这是我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<WebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

将对setTitle()的调用包装在可运行的and调用中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<WebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>