2012-08-30 146 views
-1

我使用此代碼查找當前位置。當獲取用戶當前位置時,應用程序以「應用程序意外停止」關閉

我的覆蓋圖工作正常,但無法獲取當前的經度和緯度。獲取用戶當前位置時,應用程序關閉消息「應用程序意外停止」。

我已經學習了一些關於查找位置的教程,但我無法解決它。

package google.com; 

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapActivity; 
import com.google.android.maps.MapView; 
import com.google.android.maps.MyLocationOverlay; 
import com.google.android.maps.OverlayItem; 

import java.util.*; 
import com.google.android.maps.Overlay; 
import android.location.*; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.view.Menu; 
import android.view.MenuItem; 


public class nandyog extends MapActivity implements LocationListener{ 

     LocationManager locManager; 
     Location location; 
     double lat; 
     double lng; 

    Boolean network_enabled; 
    Boolean gps_enabled; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_map); 

     MapView mapview = (MapView) findViewById(R.id.mapview); 
     mapview.setBuiltInZoomControls(true); 
     List<Overlay> mapOverlays = mapview.getOverlays(); 
     Drawable drawable = this.getResources().getDrawable(R.drawable.iconr); 
     HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this); 
     locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     Location loc = locManager.getLastKnownLocation(locManager.NETWORK_PROVIDER); 

     lat = loc.getLatitude(); 
     lng = loc.getLongitude(); 
       GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6)); 
     OverlayItem overlayitem = new OverlayItem(point, "Current Location","LAL-DARWAJA"); 

     itemizedoverlay.addOverlay(overlayitem); 
     mapOverlays.add(itemizedoverlay); 
    } 






    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_map, menu); 
     return true; 
    } 

    @Override 
    protected boolean isRouteDisplayed() { 
     // TODO Auto-generated method stub 
     return false; 
    } 






    @Override 
    public void onLocationChanged(Location location) { 
     // TODO Auto-generated method stub 

     lat = location.getLatitude(); 
     lng = location.getLongitude(); 


    } 






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

    } 






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

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 


    } 
} 
/* 
final class GPoint extends GeoPoint { 
    public GPoint(double latitude, double longitude) { 
     super((int) (latitude * 1E6), (int) (longitude * 1E6)); 
    } 
} 
*/ 

我的清單文件低於

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="google.com" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="7" 
     android:targetSdkVersion="10" /> 

    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 




    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     > 
     <uses-library android:name="com.google.android.maps"/> 
     <activity 
      android:name=".nandyog" 
      android:label="@string/title_activity_map" 
       android:theme="@android:style/Theme.NoTitleBar" 
      > 

      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

logcat的錯誤是...

08-31 18:55:49.226:d/dalvikvm(368):GC_CONCURRENT釋放1164K,54 %免費3148K/6727K,外部1625K/2137K,暫停6ms + 5ms 08-31 18:55:49.536:D/dalvikvm(368):GC_CONCURRENT釋放529K,51%釋放3307K/6727K,外部1625K/2137K,暫停6ms + 6ms 08-31 18:55:50.065:D/AndroidRuntime(368):關閉VM 08-31 18:55:50.076:W/dalvikvm(368):threadid = 1:線程退出時未捕獲的異常(group = 0x40015560) 08-31 18:55:50.105:E/AndroidRuntime(368):FATAL EXCEPTION :main 08-31 18:55:50.105:E/AndroidRuntime(368):java.lang.RuntimeException:無法啓動活動ComponentInfo {google.com/google.com.nandyog}:java.lang.NullPointerException 08- 31 18:55:50.105:E/AndroidRuntime(368):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 08-31 18:55:50.105:E/AndroidRuntime(368):at android.app .ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 08-31 18:55:50.105:E/AndroidRuntime(368):at android.app.ActivityThread.access $ 1500(ActivityThread.java:117) 08-31 18: 55:50.105:E/AndroidRuntime(368):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java: 931) 08-31 18:55:50.105:E/AndroidRuntime(368):at android.os.Handler.dispatchMessage(Handler.java:99) 08-31 18:55:50.105:E/AndroidRuntime(368) :at android.os.Looper.loop(Looper.java:130) 08-31 18:55:50.105:E/AndroidRuntime(368):at android.app.ActivityThread.main(ActivityThread.java:3683) 08 -31 18:55:50.105:E/AndroidRuntime(368):at java.lang.reflect.Method.invokeNative(Native Method) 08-31 18:55:50.105:E/AndroidRuntime(368):at java.lang .reflect.Method.invoke(Method.java:507) 08-31 18:55:50.105:E/AndroidRuntime(368):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839 ) 08-31 18:55:50.105:E/AndroidRuntime(368):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 08-31 18:55:50.105:E/An droidRuntime(368):在dalvik.system.NativeStart.main(本機方法) 08-31 18:55:50.105:E/AndroidRuntime(368):引起:java.lang.NullPointerException 08-31 18:55: 50.105:E/AndroidRuntime(368):at google.com.nandyog.onCreate(nandyog.java:46) 08-31 18:55:50.105:E/AndroidRuntime(368):at android.app.Instrumentation.callActivityOnCreate Instrumentation.java:1047) 08-31 18:55:50.105:E/AndroidRuntime(368):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 08-31 18:55:50.105:E/AndroidRuntime(368):... 11更多 08-31 18:55:50.365:D/dalvikvm(368):GC_CONCURRENT已釋放838K,54%免費3119K/6727K,外部1986K/2137K,暫停4ms + 32ms 08- 31 18:56:09.355:I /過程(368):發送信號。PID:368 SIG:9

+0

你應該看看logcat。你可能在那裏得到一個例外。跟隨麻布,檢查有什麼不對。如果你無法弄清楚,請在這裏發佈日誌。 – Nanne

回答

0

你有沒有嘗試從此得到布爾:

network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

,你應該訪問NETWORK_PROVIDER以靜態的方式這樣

Location loc = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

Location loc = locManager.getLastKnownLocation(locManager.NETWORK_PROVIDER); 
+0

我應該以靜態方式訪問NETWORK_PROVIDER,但它不能正常工作並給出相同的錯誤 –

相關問題