我希望获得地理编码的完整地址,并且我以LatLng
的形式提供了一个特定的位置。但是当我运行代码时,我在日志中遇到异常。
Panic.java
公共类恐慌扩展活动{
String address;
TextView result;
LatLng position;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.panic);
result = (TextView) findViewById(R.id.result);
ActionBar action_bar = getActionBar();
action_bar.setDisplayHomeAsUpEnabled(true);
position = new LatLng(17.6913913,75.9234617);
Toast.makeText(this, position.latitude + "," + position.longitude, 1).show();
String FullAddress = getFullAddress();
result.setText(FullAddress);
}
private String getFullAddress() {
Geocoder gc = new Geocoder(this);
List<Address> list = null;
try {
list = gc.getFromLocation(position.latitude, position.longitude, 10);
} catch (IOException e) {
e.printStackTrace();
}
String add_1 = list.get(0).getAddressLine(0);
String add_2 = list.get(0).getAddressLine(1);
String add_3 = list.get(0).getAddressLine(2);
return add_1 + ", " + add_2 + ", " + add_3 + ".";
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
flag=1;
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
logcat的
04-04 09:18:42.120: E/AndroidRuntime(30593): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.beproject.ourway/com.beproject.ourway.Panic}: java.lang.NullPointerException
04-04 09:18:42.120: E/AndroidRuntime(30593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
04-04 09:18:42.120: E/AndroidRuntime(30593): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
04-04 09:18:42.120: E/AndroidRuntime(30593): at android.app.ActivityThread.access$600(ActivityThread.java:149)
04-04 09:18:42.120: E/AndroidRuntime(30593): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
04-04 09:18:42.120: E/AndroidRuntime(30593): at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 09:18:42.120: E/AndroidRuntime(30593): at android.os.Looper.loop(Looper.java:153)
04-04 09:18:42.120: E/AndroidRuntime(30593): at android.app.ActivityThread.main(ActivityThread.java:4987)
04-04 09:18:42.120: E/AndroidRuntime(30593): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 09:18:42.120: E/AndroidRuntime(30593): at java.lang.reflect.Method.invoke(Method.java:511)
04-04 09:18:42.120: E/AndroidRuntime(30593): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
04-04 09:18:42.120: E/AndroidRuntime(30593): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
04-04 09:18:42.120: E/AndroidRuntime(30593): at dalvik.system.NativeStart.main(Native Method)
04-04 09:18:42.120: E/AndroidRuntime(30593): Caused by: java.lang.NullPointerException
04-04 09:18:42.120: E/AndroidRuntime(30593): at com.beproject.ourway.Panic.onCreate(Panic.java:45)
04-04 09:18:42.120: E/AndroidRuntime(30593): at android.app.Activity.performCreate(Activity.java:5020)
04-04 09:18:42.120: E/AndroidRuntime(30593): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-04 09:18:42.120: E/AndroidRuntime(30593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
04-04 09:18:42.120: E/AndroidRuntime(30593): ... 11 more
04-04 09:18:42.131: I/System.out(30593): [socket][2:53181] exception
04-04 09:18:42.131: I/System.out(30593): [CDS]close[53181]
04-04 09:18:42.132: I/System.out(30593): [socket][2] connection clients4.google.com/216.58.220.14:443;LocalPort=45123(0)
04-04 09:18:42.133: I/System.out(30593): [CDS]connect[clients4.google.com/216.58.220.14:443] tm:90
04-04 09:18:42.134: D/Posix(30593): [Posix_connect Debug]Process com.beproject.ourway :443
04-04 09:18:42.218: I/System.out(30593): [socket][/192.168.1.4:45123] connected
04-04 09:18:42.218: I/System.out(30593): [CDS]rx timeout:0
答案 0 :(得分:0)
检查getFullAddress是否返回Null
String FullAddress = getFullAddress();
如果它返回Null,你可能会得到NullPointerException