gpt4 book ai didi

android - 我无法在 Activity 中使用 Intent

转载 作者:行者123 更新时间:2023-11-29 15:41:06 25 4
gpt4 key购买 nike

当我点击 Google Map 上的标记时,我希望它进入另一个 Activity,但是当我从 javascript 得到结果时,它不能做 Intent

WebSettings webSetting = webView.getSettings();
webSetting.setJavaScriptEnabled(true);
webSetting.setDisplayZoomControls(true);
webSetting.setSupportZoom(true);
webSetting.setBuiltInZoomControls(true);
webView.addJavascriptInterface(new JavaScriptInterfaceTest(this), "Android");

和我的JavaScriptInterfaceTest():

public class JavaScriptInterfaceTest {
Context mContext;

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

/** Show a toast from the web page */
@JavascriptInterface
public void responseResult(String result){
Log.e("JsCallBack","DRINK");
Intent mainIntent = new Intent(MainActivity.this, Test.class);
startActivity(mainIntent);
}

@JavascriptInterface
private void startActivity(Intent mainIntent) {
// TODO Auto-generated method stub
}
}

HTML:

drinkMarker = new google.maps.Marker({
position: {lat: 25.079734,lng: 121.569519},
map: map,
});
drinkMarker.addListener('click',function(){
Android.responseResult("drink");
});

最佳答案

传递当前上下文而不是 MainActivity

public class JavaScriptInterfaceTest {
Context mContext;

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

/** Show a toast from the web page */
@JavascriptInterface
public void responseResult(String result){
Log.e("JsCallBack","DRINK");
Intent mainIntent = new Intent(mContext, Test.class);
mContext.startActivity(mainIntent);
}

@JavascriptInterface
private void startActivity(Intent mainIntent) {
// TODO Auto-generated method stub
}
}

关于android - 我无法在 Activity 中使用 Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40257962/

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