2

私の問題は、LocationManagerクラスとLocationListenerを使用してGPS座標を取得できないことです。これは私がOnCreate()で呼び出すコードです:

    locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    currentLocListener = new MyLocationListener();
    locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, currentLocListener);
    locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, currentLocListener);

そして、私はクラスLocationListenerを実装するクラスMyLocationListenerを持っています

public class MyLocationListener implements LocationListener {

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    String coordinates[] = {""+location.getLatitude(),""+location.getLongitude()};
    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);
    GeoPoint p = new GeoPoint((int)(lat*1E6), (int)(lng*1E6));
    mc.animateTo(p);
    mc.setZoom(19);

    MyMapOverlays marker = new MyMapOverlays(p);
    List<Overlay> listofOverLays = mapview.getOverlays();
    listofOverLays.clear();
    listofOverLays.add(marker);
    mapview.invalidate();
}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(), "GPS is disabled", Toast.LENGTH_SHORT).show();
}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(), "GPS is Enabled", Toast.LENGTH_SHORT).show();
}

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

DDMSのEmulatorControlで場所をモックすることで場所を取得できます。だから私は実際の電話でテストしようとしています。携帯電話にインストールすると、GPSとwifiをオンにしても現在地を自動的に取得できません。

私は市場から私の実際の電話に別のグーグルマップアプリを実行しようとしました、それはうまくいきました。コードの何が問題なのかわかりません...誰か助けてもらえますか?

LOGCAT&DEBUG

DalvikVM[localhost:8631]    
    Thread [<1> main] (Suspended (exception NullPointerException))  
        DashboardActivity$MyLocationListener.onLocationChanged(Location) line: 75   
        LocationManager$ListenerTransport._handleMessage(Message) line: 191 
        LocationManager$ListenerTransport.access$000(LocationManager$ListenerTransport, Message) line: 124  
        LocationManager$ListenerTransport$1.handleMessage(Message) line: 140    
        LocationManager$ListenerTransport$1(Handler).dispatchMessage(Message) line: 99  
        Looper.loop() line: 123 
        ActivityThread.main(String[]) line: 4627    
        Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
        Method.invoke(Object, Object...) line: 521  
        ZygoteInit$MethodAndArgsCaller.run() line: 868  
        ZygoteInit.main(String[]) line: 626 
        NativeStart.main(String[]) line: not available [native method]  
    Thread [<6> Binder Thread #2] (Running) 
    Thread [<5> Binder Thread #1] (Running) 
    Thread [<7> MapService] (Running)   
    Thread [<8> TrafficService] (Running)   
    Daemon Thread [<10> RefQueueWorker@org.apache.http.impl.conn.tsccm.ConnPoolByRoute@4610b970] (Running)  

LINE75エラーが発生しました。

4

1 に答える 1

2

NETWORK_PROVIDERの更新も要求しているため、次の権限も含める必要があります。

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

私の推測では、次の行があります。

locMgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, currentLocListener);

...はSecurityExceptionをスローしているため、GPS_PROVIDERを登録する次の行は実行されません。

トラブルシューティングを行うには、NETWORK_PROVIDERからの更新を要求する行をコメントアウトしてから、GPSがデバイスで機能するかどうかを確認することをお勧めします。または、マニフェストに上記の権限を含めてから、onLocationChanged()メソッドで受信している場所がGPS_PROVIDERからのものであることを確認してみてください。

また、文字列配列に変換する最初の行を削除してみてください。代わりに、変数に値を直接割り当てます。

double lat = location.getLatitude();
double lng = location.getLongitude();
于 2012-12-17T19:18:09.393 に答える