崩溃onLocationChanged()的Android(Crash onLocationChan

2019-10-19 20:18发布

我得到一个崩溃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();
    }

}

我跟着一些教程,我不知道为什么应用程序崩溃! 谢谢!!

Answer 1:

gcd.getFromLocation(latitude, longitude, 1); 有时返回null。 见文档这里 。

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

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


Answer 2:

您将有两个原因此异常:

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

private TextView citylb;

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

2)在这行代码:

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

addresses具有空值或者是空的!



Answer 3:

看起来像你的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(); }



Answer 4:

地址为空,地址解析器返回null未知的地址,你就来测试一下使用它之前的成绩!



文章来源: Crash onLocationChanged() Android