super.loadUrl(“javascript:func;”)方法中的空指针异常

时间:2014-05-14 10:26:47

标签: javascript android cordova nullpointerexception runtime-error

我想在android端从JS端运行一个方法。当我使用

  

sendJavaScript( “JavaScript的:函数()”)

我没有得到任何错误,但我认为来自JS方面的方法没有被调用,当我使用

  

super.loadUrl(“javascript:func();”)

我收到运行时错误。我写了一部分代码,请看看并回答我 我认为我的活动和背景问题,但我不知道我的问题在哪里。

public class Test extends DroidGap {
   @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.init();
            super.loadUrl("file:///android_asset/www/index.html");
    Log.e("run javascript func from native android", "");


    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    LocationListener locationListener = new GPSLocationListener();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000,
            0, locationListener);
   }
 }

GPScls:

public class GPScls extends DroidGap {
public void GPS(double latitude,double longitude) {
   super.loadUrl("javascript:GPS("+latitude+","+longitude+");");
    //this.sendJavascript("javascript:GPS("+latitude+","+longitude+")");
}
}

GPSLocationListener:

public class GPSLocationListener implements LocationListener{

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    if (location != null) {
        GPScls MyGPS= new GPScls();
        Log.e(""+location.getLatitude(), ""+location.getLongitude());
        MyGPS.GPS(location.getLatitude(),location.getLongitude());

    }
}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}

 }

logcat的:

05-14 14:43:00.897: D/AndroidRuntime(1148): Shutting down VM
05-14 14:43:00.897: W/dalvikvm(1148): threadid=1: thread exiting with uncaught exception (group=0x40cc2930)
05-14 14:43:00.983: E/AndroidRuntime(1148): FATAL EXCEPTION: main
05-14 14:43:00.983: E/AndroidRuntime(1148): java.lang.NullPointerException
05-14 14:43:00.983: E/AndroidRuntime(1148):     at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at android.view.View.<init>(View.java:3224)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at android.view.View.<init>(View.java:3279)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at android.view.ViewGroup.<init>(ViewGroup.java:431)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at android.widget.AbsoluteLayout.<init>(AbsoluteLayout.java:52)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at android.webkit.WebView.<init>(WebView.java:504)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at android.webkit.WebView.<init>(WebView.java:481)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at android.webkit.WebView.<init>(WebView.java:461)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at android.webkit.WebView.<init>(WebView.java:450)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at android.webkit.WebView.<init>(WebView.java:440)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at org.apache.cordova.CordovaWebView.<init>(CordovaWebView.java:126)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at org.apache.cordova.CordovaActivity.init(CordovaActivity.java:313)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at org.apache.cordova.CordovaActivity.loadUrl(CordovaActivity.java:376)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at org.example.testphonegap.GPScls.GPS(GPScls.java:7)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at org.example.testphonegap.GPSLocationListener.onLocationChanged(GPSLocationListener.java:18)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:255)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:184)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:200)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at android.os.Looper.loop(Looper.java:137)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at android.app.ActivityThread.main(ActivityThread.java:5039)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at java.lang.reflect.Method.invokeNative(Native Method)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at java.lang.reflect.Method.invoke(Method.java:511)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-14 14:43:00.983: E/AndroidRuntime(1148):     at dalvik.system.NativeStart.main(Native Method)
05-14 14:43:05.186: I/Process(1148): Sending signal. PID: 1148 SIG: 9

感谢。

1 个答案:

答案 0 :(得分:0)

DroidGapActivity,您无法使用new实例化活动类。具体来说,这是错误的:

GPScls MyGPS= new GPScls();

仅使用Intent来实例化活动。在这种情况下,您似乎应该在已为您实例化的loadUrl()活动上调用DroidGap