简体   繁体   English

尝试在空对象引用上调用虚拟方法'double android.location.Location.getLatitude()'

[英]Attempt invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference

why I dont know,I take this fault.The program was running before.But now I take this fault.Thank you for help... 为什么我不知道,我接了这个错误。程序以前运行过。但是现在我接了这个错误。谢谢您的帮助...

MenuActivity.java: 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);
    }
}

My Log Cat: 我的原木猫:

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)

The exception you're seeing in logcat points to a problem in the onCreate method of your activity. 您在logcat中看到的异常表明您的活动的onCreate方法存在问题。

The interesting part is this: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference . 有趣的部分是: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference It's telling you that whatever object you're calling getLatitude() on is null. 告诉您正在调用getLatitude()对象为空。

Looking at the body of the method, it's only possible on this line: LatLng koordinat = new LatLng(mLocation.getLatitude(), mLocation.getLongitude()); 查看方法的主体,仅在以下一行上可以: LatLng koordinat = new LatLng(mLocation.getLatitude(), mLocation.getLongitude()); . Therefore you can tell that the crash is caused by mLocation being null. 因此,您可以说崩溃是由mLocation为null引起的。

You're initializing mLocation like this: mLocationManager.getLastKnownLocation(PROVİDER); 您正在像这样初始化mLocationmLocationManager.getLastKnownLocation(PROVİDER); . The documentation tells you it's possible to get a null return value if the provider is disabled. 文档告诉您,如果禁用了提供程序,则有可能获得null返回值。

That should answer the "why". 那应该回答“为什么”。 Now you can think about the best way to work around this for your application. 现在,您可以考虑为应用程序解决此问题的最佳方法。 Can you try another provider? 您可以尝试其他提供者吗? If none of the providers are enabled, can you default to a generic location? 如果未启用任何提供程序,是否可以默认为通用位置? Do you want to show an error message instructing the user to enable location services and provide a way to try again? 您是否要显示一条错误消息,指示用户启用位置服务并提供重试方法?

the getLastKnownLocation() method can and will return null. getLastKnownLocation()方法可以并且将返回null。 The main problem is that it doesn't prompt a request to the OS for a new location lock, instead it just checks if there was a last known location from some other app's location request. 主要问题是它不会提示向操作系统请求新的位置锁定,而是仅检查其他应用程序的位置请求中是否存在最后一个已知的位置。 If no other app had recently made a location request, then you get a null location returned to you. 如果最近没有其他应用发出位置请求,那么您将得到一个空位置。

The only way to guarantee that you actually get a location is to request one, and this is done with a call to requestLocationUpdates(). 确保您实际获得位置的唯一方法是请求一个位置,这是通过调用requestLocationUpdates()完成的。

The location passed into the onLocationChanged() callback method will not be null, since the callback only occurs on a successful location lock. 传递给onLocationChanged()回调方法的位置将不为null,因为该回调仅在成功的位置锁上发生。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Mapbox时尝试在空对象引用上调用虚拟方法&#39;double android.location.Location.getLatitude()&#39; - Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference when using mapbox java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“double android.location.Location.getLatitude()” - java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference 在空对象引用上调用虚拟方法“double android.location.Location.getLatitude()”时出错 - Error invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference 问题:尝试调用虚方法&#39;double android.location.Location.getLatitude() - Problem: Attempt to invoke virtual method 'double android.location.Location.getLatitude() java.lang.IllegalArguementException:尝试在空对象引用上调用虚拟方法&#39;double android.Location.getlatitude()&#39; - java.lang.IllegalArguementException:Attempt to invoke virtual method 'double android.Location.getlatitude()' on a null object reference 调用虚方法double android.location.Location.getLatitude - Invoking virtual method double android.location.Location.getLatitude 例外:尝试在空对象引用上调用虚拟方法&#39;int com.google.android.gms.location.LocationRequest.b()&#39; - Exception: Attempt to invoke virtual method 'int com.google.android.gms.location.LocationRequest.b()' on a null object reference Android Studio蓝牙:-尝试在空对象引用上调用虚拟方法 - Android Studio Bluetooth :- attempt to invoke a virtual method on a null object reference 尝试在 Android Studio 中的 null object 参考上调用虚拟方法 - Attempt to invoke virtual method on a null object reference in Android Studio Android Studio:NullPointerException:尝试在 null object 参考上调用虚拟方法“...” - Android Studio: NullPointerException: Attempt to invoke virtual method '...' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM