
我正在测试运行Kitkat的HTC One S(通过Cyanogenmod).当我检查webvIEw时,插入的对象(AndroID)为空“对象{}”.我无法通过加载的页面使用界面中的功能.
我构建了一个更复杂的活动,但无法使界面正常工作,所以我创建了这个新的测试应用程序,它包含了webvIEw指南中唯一的代码.仍然没有将方法插入到公开的对象中.
MyActivity
import androID.app.Activity;import androID.os.Bundle;import androID.vIEw.Menu;import androID.vIEw.MenuItem;import androID.webkit.WebSettings;import androID.webkit.WebVIEw;public class MyActivity extends Activity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_my); WebVIEw myWebVIEw = (WebVIEw) findVIEwByID(R.ID.webvIEw); myWebVIEw.addJavaScriptInterface(new WebAppInterface(this),"AndroID"); WebSettings webSettings = myWebVIEw.getSettings(); webSettings.setJavaScriptEnabled(true); myWebVIEw.loadUrl("http://47e148ba.ngrok.com"); } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.my,menu); return true; } @OverrIDe public boolean onoptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button,so long // as you specify a parent activity in AndroIDManifest.xml. int ID = item.getItemID(); if (ID == R.ID.action_settings) { return true; } return super.onoptionsItemSelected(item); }} WebAppInterface
import androID.content.Context;import androID.webkit.JavaScriptInterface;import androID.Widget.Toast;/** * Created by graeme on 28/08/2014. */public class WebAppInterface { Context mContext; /** Instantiate the interface and set the context */ WebAppInterface(Context c) { mContext = c; } /** Show a toast from the web page */ @JavaScriptInterface public voID showToast(String toast) { Toast.makeText(mContext,toast,Toast.LENGTH_SHORT).show(); }} 布局
<?xml version="1.0" enCoding="utf-8"?><WebVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/webvIEw" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent" />
的build.gradle
apply plugin: 'com.androID.application'androID { compileSdkVersion 19 buildToolsversion "20.0.0" defaultConfig { applicationID "com.mydomain.JswebvIEw" minSdkVersion 17 targetSdkVersion 19 versionCode 1 versionname "1.0" } buildTypes { release { runProguard false proguardfiles getDefaultProguardfile('proguard-androID.txt'),'proguard-rules.pro' } }}dependencIEs { compile filetree(dir: 'libs',include: ['*.jar'])} 你可以看到我正在使用方法所需的装饰器,大多数问题似乎都是解决方案.我在AndroID Studio中开发,如果其中的任何预设可能会干扰?
编辑:
在Js文件中,或通过控制台,如果我通过Chrome检查webvIEw,如果我调用AndroID或window.AndroID我得到一个空的对象返回“对象{}”.或者,如果我尝试使用上面接口中定义的方法,我将收到一个方法未定义的错误.
解决方法 我遇到了同样的问题.原来是一个Proguard问题.尝试将以下内容添加到您的Proguard规则中:-keepclassmembers class ** { @androID.webkit.JavaScriptInterface <methods>;} 总结 以上是内存溢出为你收集整理的webview中的Android Javascript接口空对象全部内容,希望文章能够帮你解决webview中的Android Javascript接口空对象所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)