2016-12-14 63 views
-1

我使用JavascriptInterface與我的網站進行通信的WebView應用程序,我嘗試啓動MapsActivity並在地圖上標記點。從擴展MainActivity的類開始MapFragment活動

我加入到我的項目文件 - >新建 - >谷歌 - >谷歌地圖活動

@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); 
} 

在網絡服務器端我有這樣一些簡單的代碼

<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

活動必須來自於上下文對象

@JavascriptInterface 
public void markPointsOnMap(String points) { 
    Intent i = new Intent(context, MapsActivity.class); 
    i.putExtra("markPoints", points); 
    >> context.startActivity(i); << 
} 

呃開始!我失去了幾個小時與此,我得到和-1投票:)