Java 这个代码有什么问题?应用程序应获取位置
当我尝试启动应用程序时,应用程序(它应该读取我的位置)崩溃: 我做了一切来修复它,但在我看来,有一些基本的问题,我已经监督 主要活动:Java 这个代码有什么问题?应用程序应获取位置,java,android,crash,location,Java,Android,Crash,Location,当我尝试启动应用程序时,应用程序(它应该读取我的位置)崩溃: 我做了一切来修复它,但在我看来,有一些基本的问题,我已经监督 主要活动: import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import andr
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.support.v4.content.LocalBroadcastManager;
import java.util.Locale;
public class MainActivity extends Activity {
public static final String TAG = "MainActivity";
private LocalBroadcastManager mLocalBroadcastManager;
private BroadcastReceiver mBroadcastReceiver;
private Intent mServiceIntent;
private TextView mLatitude;
private TextView mLongitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLatitude = (TextView) findViewById(R.id.latitude);
mLongitude = (TextView) findViewById(R.id.longitude);
mServiceIntent = new Intent(this, LocationService.class);
mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive()");
mLatitude.setText(String.format(Locale.getDefault(), "%.6f", intent.getDoubleExtra(LocationService.EXTRA_LATITUDE, 0.0)));
mLongitude.setText(String.format(Locale.getDefault(), "%.6f", intent.getDoubleExtra(LocationService.EXTRA_LONGITUDE, 0.0)));
}
};
mLocalBroadcastManager.registerReceiver(mBroadcastReceiver, new IntentFilter(LocationService.INTENT_NAME));
mLatitude.setText(savedInstanceState.getString(LocationService.EXTRA_LATITUDE, "0.0"));
mLongitude.setText(savedInstanceState.getString(LocationService.EXTRA_LONGITUDE, "0.0"));
}
@Override
protected void onSaveInstanceState(Bundle bundle){
super.onSaveInstanceState(bundle);
bundle.putString(LocationService.EXTRA_LONGITUDE, mLatitude.getText().toString());
bundle.putString(LocationService.EXTRA_LATITUDE, mLongitude.getText().toString());
Log.d(TAG, bundle.toString());
}
@Override
protected void onDestroy(){
mLocalBroadcastManager.unregisterReceiver(mBroadcastReceiver);
}
public void startGps(){
startService(mServiceIntent);
}
public void stopGps(){
stopService(mServiceIntent);
}
}
定位服务:
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
public class LocationService extends Service {
public static final String TAG = "LocationService";
public static final String INTENT_NAME = "location_changed";
public static final String EXTRA_LATITUDE = "latitude";
public static final String EXTRA_LONGITUDE = "longitude";
private LocationManager mLocationManager;
private LocationListener mLocationListener;
private LocalBroadcastManager mLocalBroadcastManager;
public void onCreate(Bundle bundle){
mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
Log.d(TAG, "onLocationChanged()");
Intent intent = new Intent(INTENT_NAME);
intent.putExtra("Latitude", location.getLatitude());
intent.putExtra("Longitude", location.getLongitude());
mLocalBroadcastManager.sendBroadcast(intent);
}
// we do not need the following function, but they have to be implemented (even if they are just empty)
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
};
}
@Override
public void onDestroy() throws SecurityException{
mLocationManager.removeUpdates(mLocationListener);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) throws SecurityException{
super.onStartCommand(intent, flags, startId);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent intent){
return null;
}
}
onCreate(savedInstanceState)
的javadoc声明:
savedInstanceState捆绑包
:如果活动在先前关闭后正在重新初始化,则此捆绑包
包含它最近在onSaveInstanceState(捆绑包)
中提供的数据注意:否则为空。
当您调用savedInstanceState.getString
时,您将得到一个NPE,其中有一条消息告诉您savedInstanceState
解决方案:修改您的
onCreate
方法,以处理尚未保存实例的情况;i、 e.当savedInstanceState
为null
时,是否可以发布由以下原因引起的崩溃日志(logcat):java.lang.NullPointerException:尝试调用空对象引用上的虚拟方法“java.lang.String android.os.Bundle.getString(java.lang.String,java.lang.String)”。RuntimeException:无法启动活动组件信息{at.klex.damagedapplication/at.klex.damagedapplication.MainActivity}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.lang.String android.os.Bundle.getString(java.lang.String,java.lang.String)”。您应该在帖子中添加附加信息。