崩溃onLocationChanged()Android

时间:2014-04-09 22:57:08

标签: java android android-location

我在onLocationChanged()方法中遇到崩溃,这是我在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)

和方法:

@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();
    }

}

我遵循了一些教程,但我不知道应用程序崩溃的原因!谢谢!!

4 个答案:

答案 0 :(得分:2)

gcd.getFromLocation(latitude, longitude, 1);有时会返回null。请参阅文档here

尝试处理这种情况。例如:

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

答案 1 :(得分:0)

此异常有两个原因:

1)您的citylb可能尚未初始化。

private TextView citylb;

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

2) 在这行代码中:

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

addresses的值为空或为空!

答案 2 :(得分:0)

看起来您的addresses为空。只需将其余部分移至try块并捕获所有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(); }

答案 3 :(得分:0)

地址为空,地理编码器会针对未知位置返回null,您必须先测试它的结果才能使用它!