gpt4 book ai didi

android - Web View addJavascriptInterface 问题

转载 作者:行者123 更新时间:2023-11-29 21:56:40 25 4
gpt4 key购买 nike

当在 html 页面中单击表格行时,我试图从我的 Web View 中获得回调。我的 Activity 如下:

 public class LocalizationTestActivity extends Activity {



/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
WebView webView = new WebView(this);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
JavaScriptInterface javaScriptInterface=new JavaScriptInterface(this);
webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");
Vector<Employee> test=new Vector<Employee>();
for (int i=1;i<20;i++){
Employee employee=new Employee();
employee.setId(i);
employee.setName("Norton - "+i);
employee.setAddress("Indiranagar - " +i);
test.add(employee);
}
javaScriptInterface.setDashboardList(test);

webView.setWebViewClient(new WebViewClient() {

@Override
public void onPageFinished(WebView view, String url) {

view.loadUrl("javascript:populateData()");
}

});

webView.loadUrl("file:///android_asset/html/test2.html");
linearLayout.addView(webView);
setContentView(linearLayout);
}
}

我的 Java 脚本类如下:

public class JavaScriptInterface {
private Context mContext;
private Vector employeeList;

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

/** Show a toast from the web page */
public void callBack(String toast) {

int index=Integer.parseInt(toast);
Intent intent=new Intent(mContext, SampleActivity.class);

if(employeeList!=null && index<employeeList.size()){
Employee employee=(Employee) employeeList.elementAt(index);
intent.putExtra("value", "1");
intent.putExtra("name", employee.getName());
intent.putExtra("address", employee.getAddress());
}
mContext.startActivity(intent);
}


public void setEmployeeList(Vector employeeList) {
this.employeeList= employeeList;
}
}

回调正常。但是,当我尝试从 callBack 方法访问 employeeList 时,尽管我正在从我的 Activity 类中设置列表值,但该列表始终为 null。我错过了什么吗?有人可以帮我解决这个问题吗。

最佳答案

改变

webView.addJavascriptInterface(new JavaScriptInterface(this), "Android");

webView.addJavascriptInterface(javaScriptInterface, "Android");

关于android - Web View addJavascriptInterface 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13063222/

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