2012-04-13 4 views
0

こんにちは私はアンドロイドに慣れていますLocationManager.GPS_PROVIDERを使用してロケーションオブジェクトを作成中にnullが発生しています。ここでアンドロイドのロケーションオブジェクトを取得する方法

マイGPSDemoActivity:私のXML以下ここで

import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Toast; 

public class GPSDemoActivity extends Activity { 

    private static final long MINIMUM_DISTANCE = 1; 
    private static final long MINIMUM_TIME = 1000; 

    protected LocationManager locationManager; 

    // private Button showlocationButton; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     // showlocationButton = (Button) findViewById(R.id.btn_showlocation); 
     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
       MINIMUM_DISTANCE, MINIMUM_TIME, new MyLocationListener()); 
    } 

    public void showLocation(View v) { 
     Toast.makeText(this, "Clicked", Toast.LENGTH_LONG).show(); 
     Location location = locationManager 
       .getLastKnownLocation(LocationManager.GPS_PROVIDER);//here getting null. 

     if (location != null) { 
      String message = String.format(
        "Current Location: \n Longitude: %1$s \n Latitude: %2$s", 
        location.getLongitude(), location.getLatitude()); 
      Toast.makeText(this, message, Toast.LENGTH_LONG).show(); 
     } 

    } 

    private class MyLocationListener implements LocationListener { 

     @Override 
     public void onLocationChanged(Location location) { 
      String message = String.format(
        "Your Location \n Longitude: %1$s \n Latitude: %2$s", 
        location.getLongitude(), location.getLatitude()); 
      Toast.makeText(GPSDemoActivity.this, message, Toast.LENGTH_LONG); 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 

      Toast.makeText(GPSDemoActivity.this, "Provider disabled by the user. GPS turned off", Toast.LENGTH_LONG).show(); 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 

      Toast.makeText(GPSDemoActivity.this, 
        "Provider disabled by the user. GPS turned on", 
        Toast.LENGTH_LONG).show(); 

     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 

      Toast.makeText(GPSDemoActivity.this, 
        "Provider status changed", Toast.LENGTH_LONG).show(); 


     } 

    } 
} 

。 main122.jpg 私のmanifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.gps" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="8" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".GPSDemoActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

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


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

</manifest> 
+0

あなたのlogcatエラーを投稿してください。 – atluriajith

答えて

0

locationManager.getLastKnownLocationは、あなたが少なくとも一度位置を検出することができる場合にのみ有効な値を返します。 GPSなどの建物側テストアプリケーションアウトが建物内の場所を取得することはできません -

は、GPSを利用した位置検索があなたのデバイス上で有効になっている、だけにしてロケーションマネージャが場所

+0

こんにちはnareshありがとう私のデバイスでGPSを有効にして、その動作私は "LocationManager.GPS_PROVIDER"の代わりに "LocationManager.WPS_PROVIDER"を使用する場合 –

+0

これらは場所を取得する2つの方法であり、空との光景。あなたがインドアドールの場合は、場所を取得する前に最大20分かかることがあります。 – Naresh

+0

ohh k nareshとても感謝しています。 –

0

@Bhaskarをretreieveすることができますが、よろしいです。あなたのコードは問題ありません。

関連する問題