【问题标题】:What is wrong with this code? App should get the location这段代码有什么问题?应用程序应该获取位置
【发布时间】:2017-11-07 07:43:32
【问题描述】:

应用程序(它应该读取我的位置)在我尝试启动时崩溃:

我已尽一切努力修复它,但在我看来,我已经监督了一些基本问题。

主要活动:

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;
    }
}

【问题讨论】:

  • 你能发布崩溃日志(logcat)
  • 引起:java.lang.NullPointerException:尝试调用虚拟方法'java.lang.String android.os.Bundle.getString(java.lang.String, java.lang.String)' on空对象引用
  • java.lang.RuntimeException:无法启动活动 ComponentInfo{at.klex.damagedapplication/at.klex.damagedapplication.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法 'java.lang .String android.os.Bundle.getString(java.lang.String, java.lang.String)' 在空对象引用上
  • 你应该edit你的帖子添加额外的信息。

标签: java android crash location


【解决方案1】:

onCreate(savedInstanceState) 的 javadoc 声明:

savedInstanceState Bundle:如果活动在之前关闭后重新初始化,则此Bundle 包含它最近在onSaveInstanceState(Bundle) 中提供的数据。 注意:否则为空。

当您致电 savedInstanceState.getString 时,您会收到一个 NPE,并带有一条消息告诉您 savedInstanceState

解决方法:修改你的onCreate方法,以应对还没有保存实例的情况;即当savedInstanceStatenull

【讨论】:

    猜你喜欢
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 2018-09-09
    相关资源
    最近更新 更多