2012-02-29 55 views
0

我遵循Google提供的有关实现Web地图活动的代码,我理解了所有代码,但启动活动时我有一个Java空指针异常,我认为它没有' t发现了经度和纬度,我不知道如何解决这个问题。这里是代码:当启动一个“WebMap”活动时发生NullPointerException

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     getLocation(); 
     setupWebView(); 
     this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
     } 


     private void setupWebView(){ 
//  mostRecentLocation.setLatitude(36.848427); 
//  mostRecentLocation.setLongitude(10.26829); 
      final String centerURL = "javascript:centerAt(" + 
      mostRecentLocation.getLatitude() + "," + 
      mostRecentLocation.getLongitude()+ ")"; 
      webView = (WebView) findViewById(R.id.webview); 
      webView.getSettings().setJavaScriptEnabled(true); 
      //Wait for the page to load then send the location information 
      webView.setWebViewClient(new WebViewClient()); 
      webView.loadUrl(MAP_URL); 
      /** Allows JavaScript calls to access application resources **/ 
      webView.addJavascriptInterface(new JavaScriptInterface(), "android"); 
     } 

logcat指出问题出在setupWebView()函数中。 谢谢你的帮助。 logcat的:

02-29 13:06:42.284: E/AndroidRuntime(642): FATAL EXCEPTION: main 
02-29 13:06:42.284: E/AndroidRuntime(642): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.powerful/com.android.powerful.WebMapActivity}: java.lang.NullPointerException 
02-29 13:06:42.284: E/AndroidRuntime(642): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
02-29 13:06:42.284: E/AndroidRuntime(642): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
02-29 13:06:42.284: E/AndroidRuntime(642): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
02-29 13:06:42.284: E/AndroidRuntime(642): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
02-29 13:06:42.284: E/AndroidRuntime(642): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-29 13:06:42.284: E/AndroidRuntime(642): at android.os.Looper.loop(Looper.java:130) 
02-29 13:06:42.284: E/AndroidRuntime(642): at android.app.ActivityThread.main(ActivityThread.java:3683) 
02-29 13:06:42.284: E/AndroidRuntime(642): at java.lang.reflect.Method.invokeNative(Native Method) 
02-29 13:06:42.284: E/AndroidRuntime(642): at java.lang.reflect.Method.invoke(Method.java:507) 
02-29 13:06:42.284: E/AndroidRuntime(642): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
02-29 13:06:42.284: E/AndroidRuntime(642): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
02-29 13:06:42.284: E/AndroidRuntime(642): at dalvik.system.NativeStart.main(Native Method) 
02-29 13:06:42.284: E/AndroidRuntime(642): Caused by: java.lang.NullPointerException 
02-29 13:06:42.284: E/AndroidRuntime(642): at com.android.powerful.WebMapActivity.setupWebView(WebMapActivity.java:30) 
02-29 13:06:42.284: E/AndroidRuntime(642): at com.android.powerful.WebMapActivity.onCreate(WebMapActivity.java:24) 
02-29 13:06:42.284: E/AndroidRuntime(642): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
02-29 13:06:42.284: E/AndroidRuntime(642): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
+0

请发布logcat,以便我们可以看到错误 – 2012-02-29 13:12:16

+0

@AndreiG:Logcat发布。 – androniennn 2012-02-29 13:15:51

+0

你是否也可以发布nullpointerexception出现的代码?里面的setupWebView – 2012-02-29 13:22:04

回答

4

mostRecentLocation可能是零。 这就是为什么当你尝试访问getLatitude时,你会得到异常。 我没有看到你初始化mostRecentLocation在你发布的代码

+0

绝对;).. – androniennn 2012-02-29 13:22:33

+0

我做了这个'mostRecentLocation.setLatitude(36.848427);和'setLongitude'但是同样的问题:\。 – androniennn 2012-02-29 19:29:34

+0

你是否在代码中的任何位置设置'mostRecentLocation = something'? – 2012-02-29 20:03:07

相关问题