异步任务执行 doInBackground() 时发生错误
从我的上一个问题
我得到了 NullPointor 异常。因此,我对代码进行了一些更改,现在显示进度条,但出现以下错误。
11-17 23:39:51.373: ERROR/AndroidRuntime(495): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
11-17 23:39:51.373: ERROR/AndroidRuntime(495): java.lang.RuntimeException: An error occured while executing doInBackground()
11-17 23:39:51.373: ERROR/AndroidRuntime(495): at android.os.AsyncTask$3.done(AsyncTask.java:200)
11-17 23:39:51.373: ERROR/AndroidRuntime(495): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-17 23:39:51.373: ERROR/AndroidRuntime(495): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-17 23:39:51.373: ERROR/AndroidRuntime(495): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-17 23:39:51.373: ERROR/AndroidRuntime(495): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-17 23:39:51.373: ERROR/AndroidRuntime(495): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
11-17 23:39:51.373: ERROR/AndroidRuntime(495): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
11-17 23:39:51.373: ERROR/AndroidRuntime(495): at java.lang.Thread.run(Thread.java:1096)
11-17 23:39:51.373: ERROR/AndroidRuntime(495): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
代码是...
public class JsonExampleActivity extends ListActivity {
/** Called when the activity is first created. */
ProgressDialog mDialog;
final String TAG="a.c.b";
JSONFunction JSONfunction;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
new JSONPasingShowList().execute();
}
public void updateWithNewLocation(Location location2) {
if(location2!=null) {
double geoLat = location2.getLatitude();
double geoLng = location2.getLongitude();
}
}
class JSONPasingShowList extends AsyncTask<String, Integer,ArrayList<HashMap<String,String>>>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
mDialog = new ProgressDialog(JsonExampleActivity.this);
mDialog.setCancelable(true);
mDialog.setMessage("Loading...");
mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mDialog.setProgress(0);
mDialog.show();
}
@Override
protected ArrayList<HashMap<String,String>> doInBackground(String... params) {
double latitude[]=new double[20];
double longitude[]=new double[20];
String reference[]=new String[20];
double distance[]=new double[20];
LocationManager locationManager;
String context=Context.LOCATION_SERVICE;
locationManager=(LocationManager)getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider){
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider){ }
public void onStatusChanged(String provider, int status,
Bundle extras){ }
};
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener);
double geoLat = location.getLatitude();
Log.v(TAG, "hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii"+geoLat);
double geoLng = location.getLongitude();
Log.v(TAG, "hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii"+geoLng);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json=JSONFunction.getJSONfromURL("https://maps.googleapis.com/maps/api/place/search/json?location=37.422006,-122.084095&radius=1000&types=doctor&sensor=true&key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
try{
JSONArray JArray = json.getJSONArray("results");
Log.v(TAG, "getting results");
for(int i=0;i<JArray.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = JArray.getJSONObject(i);
JSONObject location1=e.getJSONObject("geometry").getJSONObject("location");
latitude[i]=location1.getDouble("lat");
longitude[i]=location1.getDouble("lng");
reference[i]=e.getString("reference");
Log.v(TAG, reference[i]);
distance[i]=GetLatAndLng.gps2m(geoLat, geoLng,latitude[i] ,longitude[i]);
map.put("id", String.valueOf(i));
map.put("name", "" + e.getString("name"));
map.put("vicinity", "Address " + e.getString("vicinity")+" "+"Disance:"+distance[i]);
mylist.add(map);
} }catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
final Intent intent=new Intent(JsonExampleActivity.this ,GetLatAndLng.class);
Bundle b=new Bundle();
b.putStringArray("key", reference);
intent.putExtras(b);
return mylist;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
mDialog.dismiss();
ListAdapter adapter = new SimpleAdapter(JsonExampleActivity.this, result , R.layout.listview,
new String[] { "name", "vicinity", },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
mDialog.dismiss();
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(JsonExampleActivity.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
final Intent intent=new Intent(JsonExampleActivity.this ,GetLatAndLng.class);
intent.putExtra("clickedid",position);
startActivity(intent);
}
});
}
public class MySimpleAdapter extends SimpleAdapter {
public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
}
}
}
}
出了什么问题? 提前致谢!!!
From my previous question
I was getting NullPointor exception. So i have done some changes in my code and now progress bar is showing up but getting the below errors.
11-17 23:39:51.373: ERROR/AndroidRuntime(495): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
11-17 23:39:51.373: ERROR/AndroidRuntime(495): java.lang.RuntimeException: An error occured while executing doInBackground()
11-17 23:39:51.373: ERROR/AndroidRuntime(495): at android.os.AsyncTask$3.done(AsyncTask.java:200)
11-17 23:39:51.373: ERROR/AndroidRuntime(495): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-17 23:39:51.373: ERROR/AndroidRuntime(495): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-17 23:39:51.373: ERROR/AndroidRuntime(495): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-17 23:39:51.373: ERROR/AndroidRuntime(495): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-17 23:39:51.373: ERROR/AndroidRuntime(495): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
11-17 23:39:51.373: ERROR/AndroidRuntime(495): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
11-17 23:39:51.373: ERROR/AndroidRuntime(495): at java.lang.Thread.run(Thread.java:1096)
11-17 23:39:51.373: ERROR/AndroidRuntime(495): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
Code is...
public class JsonExampleActivity extends ListActivity {
/** Called when the activity is first created. */
ProgressDialog mDialog;
final String TAG="a.c.b";
JSONFunction JSONfunction;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
new JSONPasingShowList().execute();
}
public void updateWithNewLocation(Location location2) {
if(location2!=null) {
double geoLat = location2.getLatitude();
double geoLng = location2.getLongitude();
}
}
class JSONPasingShowList extends AsyncTask<String, Integer,ArrayList<HashMap<String,String>>>
{
@Override
protected void onPreExecute() {
super.onPreExecute();
mDialog = new ProgressDialog(JsonExampleActivity.this);
mDialog.setCancelable(true);
mDialog.setMessage("Loading...");
mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mDialog.setProgress(0);
mDialog.show();
}
@Override
protected ArrayList<HashMap<String,String>> doInBackground(String... params) {
double latitude[]=new double[20];
double longitude[]=new double[20];
String reference[]=new String[20];
double distance[]=new double[20];
LocationManager locationManager;
String context=Context.LOCATION_SERVICE;
locationManager=(LocationManager)getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
public void onProviderDisabled(String provider){
updateWithNewLocation(null);
}
public void onProviderEnabled(String provider){ }
public void onStatusChanged(String provider, int status,
Bundle extras){ }
};
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener);
double geoLat = location.getLatitude();
Log.v(TAG, "hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii"+geoLat);
double geoLng = location.getLongitude();
Log.v(TAG, "hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii"+geoLng);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json=JSONFunction.getJSONfromURL("https://maps.googleapis.com/maps/api/place/search/json?location=37.422006,-122.084095&radius=1000&types=doctor&sensor=true&key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
try{
JSONArray JArray = json.getJSONArray("results");
Log.v(TAG, "getting results");
for(int i=0;i<JArray.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = JArray.getJSONObject(i);
JSONObject location1=e.getJSONObject("geometry").getJSONObject("location");
latitude[i]=location1.getDouble("lat");
longitude[i]=location1.getDouble("lng");
reference[i]=e.getString("reference");
Log.v(TAG, reference[i]);
distance[i]=GetLatAndLng.gps2m(geoLat, geoLng,latitude[i] ,longitude[i]);
map.put("id", String.valueOf(i));
map.put("name", "" + e.getString("name"));
map.put("vicinity", "Address " + e.getString("vicinity")+" "+"Disance:"+distance[i]);
mylist.add(map);
} }catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
final Intent intent=new Intent(JsonExampleActivity.this ,GetLatAndLng.class);
Bundle b=new Bundle();
b.putStringArray("key", reference);
intent.putExtras(b);
return mylist;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
}
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
mDialog.dismiss();
ListAdapter adapter = new SimpleAdapter(JsonExampleActivity.this, result , R.layout.listview,
new String[] { "name", "vicinity", },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
mDialog.dismiss();
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(JsonExampleActivity.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
final Intent intent=new Intent(JsonExampleActivity.this ,GetLatAndLng.class);
intent.putExtra("clickedid",position);
startActivity(intent);
}
});
}
public class MySimpleAdapter extends SimpleAdapter {
public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
}
}
}
}
What is going wrong?
Thanks in Advance!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在从 doInBackground 内部访问 UI 线程上的某些内容。这是不允许的。
*编辑:我已经看到了一切。对我来说,看起来最不寻常的是这些行:
尝试将所有这些位置更新内容移至
onProgressUpdate
中。我在这里很盲目,因为我看不到你的代码,但我猜测它对 UI 做了一些事情。试试这个:但正如我所说,我不确定,因为除非您在此处发布该代码,否则我无法知道
updateWithNewLocation
的作用。You are accessing something on the UI thread from inside doInBackground. And that's not allowed.
*edit: I've seen everything. To me, what looks mostly unusual are those lines:
Try to move all those location update stuff into the
onProgressUpdate
. I'm blind here since I can't see your code, but I'm guessing it does something with the UI. Try this:But as I said, I'm not sure since I can't know what
updateWithNewLocation
does unless you post that code here.这正是为什么开始了解有关线程的一些基础知识而不是仅仅对 Android 中看似与线程远程相关的所有内容说“AsyncTask”是有价值的。多线程可能很棘手,您必须仔细考虑。
我怀疑这就是正在发生的事情:您有一个 AsyncTask,其中除其他外,它还异步获取位置。这意味着您从主线程开始,在另一个线程(AsyncTask 的线程)中隐式执行 doInBackground,然后调用正在执行位置获取的任何线程。然后,您的 AsyncTask 线程继续运行,完成
doInBackground
中所有与 JSON 相关的工作,可能已经完成。 AsyncTask 线程可能会结束。 然后您将获得位置锁定,并且您提供的侦听器将被回调,但现在 AsyncTask 已经完成,并且其线程不再有效。仅根据您的代码,我怀疑您想在获取位置后进行 Google Maps API 调用,对吧?如果是这样,请同步调用“获取位置”代码。在回调中,您可以放置代码来启动 AsyncTask,该任务现在只执行 Google Maps API 调用和处理。
This is exactly why it's valuable to start understanding some basics around threading instead of just saying "AsyncTask" for everything that seems remotely threading-related in Android. Multi-threading can be tricky and you have to think things through.
I suspect this is what's happening: You have an AsyncTask which among other things gets the location... asynchronously. That means you start on the main thread, you execute
doInBackground
in another thread implicitly (the AsyncTask's thread), then you call off to whatever thread is doing your location acquisition. Then your AsyncTask thread continues on, finishing off all that JSON-related work indoInBackground
, probably finishing. The AsyncTask thread likely finishes. Then you are getting your location lock, and the listener that you provided is called back, except that now the AsyncTask has already finished, and its thread is no longer valid.Just based on a look at your code I suspect that you want to make that Google Maps API call after you get your location, right? If so, call your "get location" code synchronously. In the callback to that, you can put the code to kick off your AsyncTask which now only does the Google Maps API call and processing.
您在
doInBackground()
中做了很多您不需要在那里做的事情。仅在doInBackground()
方法中执行会导致 UI 线程卡顿的操作。换句话说,所有不直接处理网络通信的代码都需要从doInBackground()
方法中移出。You're doing a bunch of stuff in your
doInBackground()
that you don't need to do in there. Only do the stuff in yourdoInBackground()
method that would cause the stuttering on the UI thread. In other words, all the code that doesn't directly deal with your network communication needs to be moved out of yourdoInBackground()
method.