我是android.i的新手需要获得当前位置的格度和逻辑。但是它在位置显示空指针异常。在下面我添加了我的代码。
LocationManager lm;
Location location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
location=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
SaveLocation(location);
}
private void SaveLocation(Location location) {
Long time=new GregorianCalendar().getTimeInMillis()+1*60*1000;
Intent intent=new Intent(MainActivity.this,AlarmReceiver.class);
intent.putExtra("lat",location.getLatitude());
intent.putExtra("lon",location.getLongitude());
AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP,time,PendingIntent.getBroadcast(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT));
}
我的androidmanifasted文件就像
<?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"
android:targetSdkVersion="17" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.gps.MainActivity"
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>
任何人都可以帮助我?
答案 0 :(得分:1)
您的位置为空,因为您在创建LocationManager后立即尝试获取最后的已知位置。在这段时间(几毫秒),您的设备无法找到任何位置。 在LocationManager的初始化和getLastKnownLocation方法之间需要一个时间间隔。我建议将getLastKnownLocation方法放在按钮上的OnClick事件中以创建时间间隔。
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE); /.... } button.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v){ location=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); SaveLocation(location); } });
答案 1 :(得分:0)
您的Location
对象为空,因为没有存储上一个已知位置。
location=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// Check if last known location exists.
if(location != null) {
SaveLocation(location);
}
答案 2 :(得分:0)
//to find the current latitude and longitude of user
public String getMyLocation(){
locationManager=(LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Location location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location!=null){
loc[0]=location.getLatitude();
loc[1]=location.getLongitude();
Log.d("STATUS n ", loc[0]+"entered 1"+loc[1]);
}
else {
location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location!=null)
{
loc[0]=location.getLatitude();
loc[1]=location.getLongitude();
Log.d("STATUS n","else"+loc[0]+"entered 1"+loc[1] );
}
}
return getPlace(loc);
}