gpt4 book ai didi

javascript - ANDROID/WEBVIEW-- 未捕获的类型错误 : Object [object Object] has no method 'changeActivity' at file:

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:52:31 30 4
gpt4 key购买 nike

我正在尝试使用带有输入框的 webview,获取字符串,并将其传递给第二个 Activity 。出于某种原因,该按钮没有执行任何操作,我收到错误消息:

Uncaught TypeError: Object [object Object] has no method 'changeActivity' at file:///android_asset/www/index.js:3

所以我的 HTML 是这样写的:

 <input id="name" value="" />&nbsp;&nbsp;&nbsp;
<button onclick="checkMovie()"> Check it! </button>

我的 JS 是这样说的:

    function checkMovie() {
var movieName = document.getElementById('name').value;
webapi.changeActivity(movieName);}

我的 Android 代码是这样说的:

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 上运行,因此您必须在您的 changeActivity 方法和您在 android 代码中调用的任何其他 javascript 方法上设置 @JavascriptInterface 注释。

@JavascriptInterface
public void changeActivity(String movieName) {
...

这对于 Android 4.2 或更高版本是必需的。

关注这个link阅读更多。

关于javascript - ANDROID/WEBVIEW-- 未捕获的类型错误 : Object [object Object] has no method 'changeActivity' at file:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20695973/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com