无法启动WebMap活动(NullPointerException)

时间:2012-02-29 22:15:02

标签: android google-maps-api-3

我找到了知道commonsware的{​​{3}}。我尝试启动它,但我有一个NullPointer异常: 这是代码:

public class WebMapActivity extends Activity {
      private static String PROVIDER="gps";
      private WebView browser;
      private LocationManager myLocationManager=null;

      @Override
      public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        browser=(WebView)findViewById(R.id.webview);

        myLocationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);

       browser.getSettings().setJavaScriptEnabled(true);
        browser.addJavascriptInterface(new Locater(), "locater");
        browser.loadUrl("http://gmaps-samples.googlecode.com/svn/trunk/articles-android-webmap/simple-android-map.html");
      }

      @Override
      public void onResume() {
        super.onResume();
        myLocationManager.requestLocationUpdates(PROVIDER, 0,
                                                  0,
                                                  onLocation);
      }

      @Override
      public void onPause() {
        super.onPause();
        myLocationManager.removeUpdates(onLocation);
      }

      LocationListener onLocation=new LocationListener() {
        public void onLocationChanged(Location location) {
          StringBuilder buf=new StringBuilder("javascript:whereami(");

          buf.append(String.valueOf(location.getLatitude()));
          buf.append(",");
          buf.append(String.valueOf(location.getLongitude()));
          buf.append(")");

          browser.loadUrl(buf.toString());
        }

        public void onProviderDisabled(String provider) {
          // required for interface, not used
        }

        public void onProviderEnabled(String provider) {
          // required for interface, not used
        }

        public void onStatusChanged(String provider, int status,
                                      Bundle extras) {
          // required for interface, not used
        }
      };

      public class Locater {
        public String getLocation() throws JSONException {
          Location loc=myLocationManager.getLastKnownLocation(PROVIDER);

          if (loc==null) {
            return(null);
          }

          JSONObject json=new JSONObject();

          json.put("lat", loc.getLatitude());
          json.put("lon", loc.getLongitude());

          return(json.toString());
        }
      }
    }

logcat的:

02-29 22:10:35.096: E/AndroidRuntime(885): FATAL EXCEPTION: main
02-29 22:10:35.096: E/AndroidRuntime(885): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gatec.tunisiana/com.android.airplane.WebMapActivity}: java.lang.NullPointerException
02-29 22:10:35.096: E/AndroidRuntime(885):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-29 22:10:35.096: E/AndroidRuntime(885):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-29 22:10:35.096: E/AndroidRuntime(885):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-29 22:10:35.096: E/AndroidRuntime(885):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-29 22:10:35.096: E/AndroidRuntime(885):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-29 22:10:35.096: E/AndroidRuntime(885):  at android.os.Looper.loop(Looper.java:130)
02-29 22:10:35.096: E/AndroidRuntime(885):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-29 22:10:35.096: E/AndroidRuntime(885):  at java.lang.reflect.Method.invokeNative(Native Method)
02-29 22:10:35.096: E/AndroidRuntime(885):  at java.lang.reflect.Method.invoke(Method.java:507)
02-29 22:10:35.096: E/AndroidRuntime(885):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-29 22:10:35.096: E/AndroidRuntime(885):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-29 22:10:35.096: E/AndroidRuntime(885):  at dalvik.system.NativeStart.main(Native Method)
02-29 22:10:35.096: E/AndroidRuntime(885): Caused by: java.lang.NullPointerException
02-29 22:10:35.096: E/AndroidRuntime(885):  at com.android.airplane.WebMapActivity.onCreate(WebMapActivity.java:26)
02-29 22:10:35.096: E/AndroidRuntime(885):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-29 22:10:35.096: E/AndroidRuntime(885):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-29 22:10:35.096: E/AndroidRuntime(885):  ... 11 more

我不知道为什么它无法启动该活动。非常感谢你的帮助。

1 个答案:

答案 0 :(得分:1)

很高兴你得到了答案。为了将来参考,请查看您的日志。我知道你发布了它们,但真正的答案就像中途一样。

02-29 22:10:35.096: E/AndroidRuntime(885): Caused by: java.lang.NullPointerException
02-29 22:10:35.096: E/AndroidRuntime(885):  at com.android.airplane.WebMapActivity.onCreate(WebMapActivity.java:26)

在第二行显示.WebMapActivity.onCreate(WebMapActivity.java:26),因此您知道它位于第26行的onCreate()中的WebMapActivity中。转到该行并找出对null值至关重要的值那条线。

像ByteMe说的那样......

browser=(WebView)findViewById(R.id.webview);

除非找不到webview,否则该行中的任何内容都不会导致NullPointerException。