ANDROID / WEBVIEW--未捕获类型错误:对象的翻译:具有在文件中没有方法'changeActivity“: [英] ANDROID/WEBVIEW-- Uncaught TypeError: Object [object Object] has no method 'changeActivity' at file:
问题描述
我试图用一个WebView的输入框,走串,并将它传递给第二个活动。出于某种原因,该按钮没有做任何事情,我得到的错误:
I am trying to use a webview with an input box, take the string, and pass it to a second activity. For some reason, the button is not doing anything and I get the error:
未捕获类型错误:对象的翻译:没有方法'changeActivity'的文件:///android_asset/www/index.js:3
Uncaught TypeError: Object [object Object] has no method 'changeActivity' at file:///android_asset/www/index.js:3
所以,我的HTML这样说:
So my HTML says this:
<input id="name" value="" />
<button onclick="checkMovie()"> Check it! </button>
我的JS这样说:
my JS says this:
function checkMovie() {
var movieName = document.getElementById('name').value;
webapi.changeActivity(movieName);}
和我的Android code这样说:
and my Android code says this:
public class MainActivity extends Activity implements OnClickListener {
// Setting up known variables
JavaScriptInterface JSInterface;
Button find;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// changing main layout to show the splash first
setContentView(R.layout.activity_main);
// Tie my webviews and set up the webview to handle JS
WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
// Expecting UI
webView.setWebChromeClient(new WebChromeClient());
// allowing to inject Java objects into a page's JavaScript
webView.addJavascriptInterface(new JavaScriptInterface(this), "webapi");
// Load the local file
webView.loadUrl("file:///android_asset/www/index.html");
find = new Button(MainActivity.this);
find.setOnClickListener(this);
WebSettings ws = webView.getSettings();
ws.setJavaScriptEnabled(true);
// Add the interface to record javascript events
webView.addJavascriptInterface(find, "find");
}
public class JavaScriptInterface {
Context mContext;
// Instantiate the interface and set the context
JavaScriptInterface(Context c) {
mContext = c;
}
// Call the function changeActivity defined in my JS of my HTML
public void changeActivity(String movieName) {
// Call the Movie App and store the intent to pass it onto the app.
Log.e("XXXXXXXXXXXXXXX", "X==========>" + movieName);
Intent i = new Intent(MainActivity.this, second.class);
i.putExtra("movie_name", movieName);
startActivity(i);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.equals(find)) {
Log.e("XXXXXXXXXXXXXXX", "XXXXXXXXXXXXXXX");
}
}
}
推荐答案
现在的问题是这一点。既然你在4.2.2上运行,你必须将 @JavascriptInterface 在changeActivity方法的顶部和你在你的Android code调用任何其他JavaScript方法的注释。
The problem is exactly that. Since you run on 4.2.2 you have to set the @JavascriptInterface annotation on top of your changeActivity method and on any other javascript methods you call in your android code.
@JavascriptInterface
public void changeActivity(String movieName) {
...
这是必要的为Android 4.2或更高版本。
This is necessary for Android 4.2 or higher.
按照此<一个href=\"http://stackoverflow.com/questions/14031635/android-4-2-1-webview-and-javascript-interface-breaks\">link阅读更多。
这篇关于ANDROID / WEBVIEW--未捕获类型错误:对象的翻译:具有在文件中没有方法'changeActivity“:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!