我正在开发一个使用JavascriptInterface与我的网站javascript通信的WebView应用程序,我尝试启动MapsActivity并在地图上标记一些点。
我添加到我的项目文件 - >新 - > Google - > Google地图活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
...
mWebView.addJavascriptInterface(new WebAppInterface(this, activity, regIdFirebase, mWebView, mGoogleApiClient), "WEB2Android");
....
WebAppInterface类有许多正常工作的方法,但有一个例外: markPointsOnMap
public class WebAppInterface extends MainActivity {
Context context;
Activity activity;
String regIdFirebase;
WebView mWebView;
Object casaMarcat = null;
GoogleApiClient mGoogleApiClient;
public WebAppInterface(Context c, Activity a, String r, WebView w, GoogleApiClient p) {
context = c;
activity = a;
regIdFirebase = r;
mWebView = w;
mGoogleApiClient = p;
}
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
}
...
@JavascriptInterface
public void markPointsOnMap(String points) {
Log.d("markPointsOnMap", context.toString());
Intent i = new Intent(getApplicationContext(), MapsActivity.class);
i.putExtra("markPoints", points);
startActivity(i);
}
在Web服务器端,我有一些像这样的简单代码
<p>
<input type="button" value="Mark Points On Map" onClick="markPointsOnMap('47.179142,27.480926,KokoShannel;47.646331,26.254062,NaturelPiata;47.243562,26.716604,Jatakana;47.211149,27.014350,Placeholder')" />
</p>
<script type="text/javascript">
function markPointsOnMap(points) {
WEB2Android.markPointsOnMap(points);
}
</script>
我有这个错误:
12-14 13:20:01.771 27100-28063/ro.nexuserp.apps W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
12-14 13:20:01.771 27100-28063/ro.nexuserp.apps W/System.err: at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:112)
12-14 13:20:01.771 27100-28063/ro.nexuserp.apps W/System.err: at ro.nexuserp.apps.utils.WebAppInterface$override.markPointsOnMap(WebAppInterface.java:116)
12-14 13:20:01.771 27100-28063/ro.nexuserp.apps W/System.err: at ro.nexuserp.apps.utils.WebAppInterface$override.access$dispatch(WebAppInterface.java)
12-14 13:20:01.771 27100-28063/ro.nexuserp.apps W/System.err: at ro.nexuserp.apps.utils.WebAppInterface.markPointsOnMap(WebAppInterface.java:0)
12-14 13:20:01.771 27100-28063/ro.nexuserp.apps W/System.err: at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
12-14 13:20:01.771 27100-28063/ro.nexuserp.apps W/System.err: at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:41)
12-14 13:20:01.771 27100-28063/ro.nexuserp.apps W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
12-14 13:20:01.771 27100-28063/ro.nexuserp.apps W/System.err: at android.os.Looper.loop(Looper.java:158)
12-14 13:20:01.771 27100-28063/ro.nexuserp.apps W/System.err: at android.os.HandlerThread.run(HandlerThread.java:61)
我试过这个变种,但结果相同。
Intent i = new Intent(context, MapsActivity.class);
答案 0 :(得分:0)
活动必须从上下文对象
开始@JavascriptInterface
public void markPointsOnMap(String points) {
Intent i = new Intent(context, MapsActivity.class);
i.putExtra("markPoints", points);
>> context.startActivity(i); <<
}
嗯!我失去了几个小时,我得到了-1票:)