سؤال

I am getting a crash in onLocationChanged() method and this is what I get in logCat

04-10 00:51:43.354: E/AndroidRuntime(4132): FATAL EXCEPTION: main
04-10 00:51:43.354: E/AndroidRuntime(4132): Process: com.dd.assistant, PID: 4132
04-10 00:51:43.354: E/AndroidRuntime(4132): java.lang.NullPointerException
04-10 00:51:43.354: E/AndroidRuntime(4132):     at com.twixkat.assistant.MainActivity.onLocationChanged(MainActivity.java:1314)
04-10 00:51:43.354: E/AndroidRuntime(4132):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:279)
04-10 00:51:43.354: E/AndroidRuntime(4132):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:208)
04-10 00:51:43.354: E/AndroidRuntime(4132):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:224)
04-10 00:51:43.354: E/AndroidRuntime(4132):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-10 00:51:43.354: E/AndroidRuntime(4132):     at android.os.Looper.loop(Looper.java:136)
04-10 00:51:43.354: E/AndroidRuntime(4132):     at android.app.ActivityThread.main(ActivityThread.java:5081)
04-10 00:51:43.354: E/AndroidRuntime(4132):     at java.lang.reflect.Method.invokeNative(Native Method)
04-10 00:51:43.354: E/AndroidRuntime(4132):     at java.lang.reflect.Method.invoke(Method.java:515)
04-10 00:51:43.354: E/AndroidRuntime(4132):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-10 00:51:43.354: E/AndroidRuntime(4132):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
04-10 00:51:43.354: E/AndroidRuntime(4132):     at dalvik.system.NativeStart.main(Native Method)

and method:

@Override
public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub
    List<Address> addresses = null;
    if (arg0 != null) {
        double longitude = arg0.getLongitude();
        double latitude = arg0.getLatitude();
        Geocoder gcd = new Geocoder(getApplicationContext(), Locale.getDefault());

        try {
            addresses = gcd.getFromLocation(latitude, longitude, 1);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        citylb.setText(addresses.get(0).getLocality());
        getWeather((String) citylb.getText());

        // if (addresses.size() > 0) return addresses.get(0).getLocality();
    }

}

I followed some tutorials and I don't know why application crashes!! Thank You!!

هل كانت مفيدة؟

المحلول

gcd.getFromLocation(latitude, longitude, 1); sometimes returns null. See documentation here.

Try to handle this situation. For example:

try {
    addresses = gcd.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
    // TODO Auto-generated catch block
    Log.e("MyTag", "MyMessage", e); // use this instead e.printStackTrace();
}

if(addresses != null && addresses.size() > 0){
    citylb.setText(addresses.get(0).getLocality());

    getWeather((String) citylb.getText());
}else{
    citylb.setText("No adresses found");
}

نصائح أخرى

You will have two causes for this exception:

1) your citylb might not have been initialized.

private TextView citylb;

citylb = (TextView)findViewById(R.id.myTextViewIdinLayout);

2) at this line of code :

citylb.setText(addresses.get(0).getLocality());

addresses has a null value or is empty!

Looks like your addresses is null. Simply move the rest to the try block and catch all Exception:

try { addresses = gcd.getFromLocation(latitude, longitude, 1); citylb.setText(addresses.get(0).getLocality()); getWeather((String) citylb.getText()); } catch (Exception e) { // GeoCoder service not available e.printStackTrace(); }

addresses is null, geocoder returns null for unknown locations, you have to test it's result before using it!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top