NullPointerException异常在我的AsyncTask [英] NullPointerException in my AsyncTask
问题描述
大家好,
我有一个异步任务用于指向基于用户输入的地址的地图上的点。我在onPostExecute一个NullPointerException异常,我可以明白为什么。这里是异步类
私有类地理coderTask延伸的AsyncTask<弦乐,太虚,列表与LT;地址>> { @覆盖
保护列表与LT;地址> doInBackground(字符串... LOCATIONNAME){
//创建地理codeR类的一个实例
地理codeR地理codeR =新的地缘codeR(getBaseContext());
清单<地址>地址= NULL; 尝试{
//获取唯一一个匹配查询地址
地址=地理coder.getFromLocationName(LOCATIONNAME [0],3);
}赶上(IOException异常五){
e.printStackTrace();
}
返回地址;
}
@覆盖
保护无效onPostExecute(列表<地址>地址){ 如果(地址== NULL || addresses.size()== 0){
Toast.makeText(getBaseContext(),找不到位置。检查地址,Toast.LENGTH_SHORT).show();
} //清除地图上所有的现有标记
mMap.clear(); //添加谷歌地图上标记为每个匹配的地址
的for(int i = 0; I< addresses.size();我++){//这是585线从logcat的 地址地址=(地址)addresses.get(I) //创建的GeoPoint的一个实例,在谷歌地图中显示
经纬度latLng_search =新的经纬度(address.getLatitude(),address.getLongitude()); 字符串addressText =的String.format(%S%S,
address.getMaxAddressLineIndex()> 0? address.getAddressLine(0):,
address.getLocality()); 的MarkerOptions的MarkerOptions =新的MarkerOptions();
markerOptions.position(latLng_search);
markerOptions.title(addressText); mMap.addMarker(的MarkerOptions);
//找到的第一个位置
如果(我== 0)
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng_search));
}
}
}
这里是logcat的:
2月9日至28日:51:45.501:E / AndroidRuntime(16587):致命异常:主要
2月9日至28日:51:45.501:E / AndroidRuntime(16587):显示java.lang.NullPointerException
2月9日至28日:51:45.501:E / AndroidRuntime(16587):在drkstr.yar.Create_reminder_loc $地理coderTask.onPostExecute(Create_reminder_loc.java:585)
2月9日至28日:51:45.501:E / AndroidRuntime(16587):在drkstr.yar.Create_reminder_loc $地理coderTask.onPostExecute(Create_reminder_loc.java:1)
2月9日至28日:51:45.501:E / AndroidRuntime(16587):在android.os.AsyncTask.finish(AsyncTask.java:417)
2月9日至28日:51:45.501:E / AndroidRuntime(16587):在android.os.AsyncTask.access $ 300(AsyncTask.java:127)
2月9日至28日:51:45.501:E / AndroidRuntime(16587):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:429)
2月9日至28日:51:45.501:E / AndroidRuntime(16587):在android.os.Handler.dispatchMessage(Handler.java:99)
2月9日至28日:51:45.501:E / AndroidRuntime(16587):在android.os.Looper.loop(Looper.java:143)
2月9日至28日:51:45.501:E / AndroidRuntime(16587):在android.app.ActivityThread.main(ActivityThread.java:4196)
2月9日至28日:51:45.501:E / AndroidRuntime(16587):在java.lang.reflect.Method.invokeNative(本机方法)
2月9日至28日:51:45.501:E / AndroidRuntime(16587):在java.lang.reflect.Method.invoke(Method.java:507)
2月9日至28日:51:45.501:E / AndroidRuntime(16587):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
2月9日至28日:51:45.501:E / AndroidRuntime(16587):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
2月9日至28日:51:45.501:E / AndroidRuntime(16587):在dalvik.system.NativeStart.main(本机方法)
该错误是在行585转动起来,我在code标明这一点。在该行的只有2个变量是i和地址,我空,如果地址办理入住手续。我不知道为什么我仍然得到一个空指针异常。
让我知道如果你需要看的了code的。
感谢您抽出时间来阅读这和任何帮助,你可以给。
如果(地址== NULL || addresses.size()== 0){
Toast.makeText(getBaseContext(),找不到位置。检查地址,Toast.LENGTH_SHORT).show();
返回; //添加此
}
Hey everyone , I have an Async Task for pointing to a point on the map based on user inputted address. I have a NullPointerException in the onPostExecute and I can work out why. Here is the async class
private class GeocoderTask extends AsyncTask<String, Void, List<Address>>{
@Override
protected List<Address> doInBackground(String... locationName) {
// Creating an instance of Geocoder class
Geocoder geocoder = new Geocoder(getBaseContext());
List<Address> addresses = null;
try {
// Getting only one address that matches the query
addresses = geocoder.getFromLocationName(locationName[0], 3);
} catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
@Override
protected void onPostExecute(List<Address> addresses) {
if(addresses==null || addresses.size()==0){
Toast.makeText(getBaseContext(), "No Location found.Please check address", Toast.LENGTH_SHORT).show();
}
// Clears all the existing markers on the map
mMap.clear();
// Adding Markers on Google Map for each matching address
for(int i=0;i<addresses.size();i++){// this is line 585 from the logcat
Address address = (Address) addresses.get(i);
// Creating an instance of GeoPoint, to display in Google Map
LatLng latLng_search = new LatLng(address.getLatitude(), address.getLongitude());
String addressText = String.format("%s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getLocality());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng_search);
markerOptions.title(addressText);
mMap.addMarker(markerOptions);
// Locate the first location
if(i==0)
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng_search));
}
}
}
here is the logcat:
09-28 02:51:45.501: E/AndroidRuntime(16587): FATAL EXCEPTION: main
09-28 02:51:45.501: E/AndroidRuntime(16587): java.lang.NullPointerException
09-28 02:51:45.501: E/AndroidRuntime(16587): at drkstr.yar.Create_reminder_loc$GeocoderTask.onPostExecute(Create_reminder_loc.java:585)
09-28 02:51:45.501: E/AndroidRuntime(16587): at drkstr.yar.Create_reminder_loc$GeocoderTask.onPostExecute(Create_reminder_loc.java:1)
09-28 02:51:45.501: E/AndroidRuntime(16587): at android.os.AsyncTask.finish(AsyncTask.java:417)
09-28 02:51:45.501: E/AndroidRuntime(16587): at android.os.AsyncTask.access$300(AsyncTask.java:127)
09-28 02:51:45.501: E/AndroidRuntime(16587): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
09-28 02:51:45.501: E/AndroidRuntime(16587): at android.os.Handler.dispatchMessage(Handler.java:99)
09-28 02:51:45.501: E/AndroidRuntime(16587): at android.os.Looper.loop(Looper.java:143)
09-28 02:51:45.501: E/AndroidRuntime(16587): at android.app.ActivityThread.main(ActivityThread.java:4196)
09-28 02:51:45.501: E/AndroidRuntime(16587): at java.lang.reflect.Method.invokeNative(Native Method)
09-28 02:51:45.501: E/AndroidRuntime(16587): at java.lang.reflect.Method.invoke(Method.java:507)
09-28 02:51:45.501: E/AndroidRuntime(16587): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-28 02:51:45.501: E/AndroidRuntime(16587): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-28 02:51:45.501: E/AndroidRuntime(16587): at dalvik.system.NativeStart.main(Native Method)
The error is turning up at line 585 , I have marked this in the code. The only 2 variables in that line are i and address and I check if address in null. I don't get why I am still getting a null pointer exception.
Let me know if you need to see anymore of the code.
Thanks for taking the time to read this and for any help that you can give.
if(addresses==null || addresses.size()==0){
Toast.makeText(getBaseContext(), "No Location found.Please check address", Toast.LENGTH_SHORT).show();
return; // add this
}
这篇关于NullPointerException异常在我的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!