【问题标题】:Attempt invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference尝试在空对象引用上调用虚拟方法“double android.location.Location.getLatitude()”
【发布时间】:2015-11-10 00:23:09
【问题描述】:

为什么我不知道,我犯了这个错误。程序之前运行。但是现在我犯了这个错误。谢谢你的帮助......

MenuActivity.java:

final Context context = this;
private LocationManager mLocationManager;
private String PROVİDER = LocationManager.GPS_PROVIDER;
private GoogleMap googleHarita;
Location mLocation;

public class MenuActivity extends FragmentActivity {
    final Context context = this;
    private static final int CAMERA_REQUEST = 1888;

    TextView e_kor, b_kor, acikAdres;
    Fragment harita;
    RadarView mRadarView = null;

    private LocationManager mLocationManager;
    private String PROVİDER = LocationManager.GPS_PROVIDER;
    private GoogleMap googleHarita=null;

    Location mLocation = null;
    TelephonyManager telemamanger;
    String getSimSerialNumber;



    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        // telefon numarası nesnesi
        telemamanger = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        e_kor = (TextView) findViewById(R.id.enlmkor);
        b_kor = (TextView) findViewById(R.id.bylmkor);
        acikAdres = (TextView) findViewById(R.id.acikAdres);

        // resim metodu
        mRadarView = (RadarView) findViewById(R.id.radarView);

        // radar çalıştırma metodları
        mRadarView.setShowCircles(true);
        mRadarView.startAnimation();

        // Konum-lokasyon kodları
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        final Location mLocation = mLocationManager
                .getLastKnownLocation(PROVİDER);

        if (googleHarita == null) {
            googleHarita = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.haritafragment)).getMap();

            if (googleHarita != null) {
                LatLng koordinat = new LatLng(mLocation.getLatitude(),
                        mLocation.getLongitude());
                googleHarita.addMarker(new MarkerOptions().position(koordinat));
                googleHarita.moveCamera(CameraUpdateFactory.newLatLngZoom(
                        koordinat, 10));
            }
        }

        showLocation(mLocation);
        new GetAddressTask().execute(mLocation);

    }

    // Lokasyon işlemleri
    private void showLocation(Location location) {
        if (location == null) {
            Toast.makeText(getApplicationContext(), "Konum Bulunamadı",
                    Toast.LENGTH_SHORT).show();
        } else {
            e_kor.setText(location.getLatitude() + "");
            b_kor.setText(location.getLongitude() + "");
        }
    }   

    @Override
    protected void onPause() {
        super.onPause();
        mLocationManager.removeUpdates(listener);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mLocationManager.requestLocationUpdates(PROVİDER, 0, 0, listener);
    }



    // Konum listener
    private LocationListener listener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            showLocation(location);
        }

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

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onProviderDisabled(String provider) {
        }
    };

public class GetAddressTask extends AsyncTask<Location, Void, String> {

    @Override
    protected String doInBackground(Location... locations) {
        Geocoder geocoder = new Geocoder(MenuActivity.this);
        Location location = locations[0];
        List<Address> addresses;
        String addrStr = null;
        try {
            addresses = (List<Address>) geocoder.getFromLocation(
                    location.getLatitude(), location.getLongitude(), 5);
            Address addr = addresses.get(0);
            addrStr = addr.getAddressLine(0) + ", " + addr.getAdminArea()
                    + ", " + addr.getCountryName();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return addrStr;
    }

    @Override
    protected void onPostExecute(String result) {
        acikAdres.setText(result);
    }
}

我的原木猫:

11-10 02:20:50.508: E/AndroidRuntime(29376): FATAL EXCEPTION: main
11-10 02:20:50.508: E/AndroidRuntime(29376): Process: com.gpsacilbildirim, PID: 29376
11-10 02:20:50.508: E/AndroidRuntime(29376): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gpsacilbildirim/com.gpsacilbildirim.MainActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gpsacilbildirim/com.gpsacilbildirim.MenuActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
11-10 02:20:50.508: E/AndroidRuntime(29376):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
11-10 02:20:50.508: E/AndroidRuntime(29376):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
11-10 02:20:50.508: E/AndroidRuntime(29376):    at android.app.ActivityThread.access$900(ActivityThread.java:172)
11-10 02:20:50.508: E/AndroidRuntime(29376):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)

【问题讨论】:

  • 您似乎没有处理onCreate()mLocationnull 的情况。

标签: java android eclipse


【解决方案1】:

您在 logcat 中看到的异常表明您的活动的 onCreate 方法存在问题。

有趣的是: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference。它告诉您,您调用getLatitude() 的任何对象都是空的。

查看方法的主体,只能在这一行:LatLng koordinat = new LatLng(mLocation.getLatitude(), mLocation.getLongitude());。因此,您可以判断崩溃是由mLocation 为空引起的。

您正在像这样初始化mLocationmLocationManager.getLastKnownLocation(PROVİDER);documentation 告诉您,如果提供程序被禁用,则可以获得 null 返回值。

这应该回答“为什么”。现在您可以考虑为您的应用程序解决此问题的最佳方法。您可以尝试其他提供商吗?如果没有启用任何提供程序,您可以默认使用通用位置吗?是否要显示一条错误消息,指示用户启用位置服务并提供重试方式?

【讨论】:

  • 谢谢你的回答。但我解决了这个问题。这是一个与eclipse工作室相关的问题。我再次下载了eclipse,它现在可以工作了。
【解决方案2】:

getLastKnownLocation() 方法可以并且将返回 null。主要问题是它不会提示操作系统请求新的位置锁定,而只是检查是否有来自其他应用程序的位置请求的最后一个已知位置。如果最近没有其他应用发出位置请求,那么您会收到一个空位置返回给您。

保证您实际获得位置的唯一方法是请求一个位置,这是通过调用 requestLocationUpdates() 来完成的。

传递给 onLocationChanged() 回调方法的位置不会为空,因为回调仅在位置锁定成功时发生。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    • 2015-11-24
    • 1970-01-01
    相关资源
    最近更新 更多