异步任务执行doInBackground()时发生错误

时间:2011-11-17 18:28:35

标签: android listview android-asynctask

来自我的previous question

我得到了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
}
}
}
}

出了什么问题? 在此先感谢!!!

3 个答案:

答案 0 :(得分:16)

Can't create handler inside thread that has not called Looper.prepare()

您正在从doInBackground内部访问UI线程上的内容。这是不允许的。

*编辑:我已经看到了一切。对我来说,看起来最不寻常的是那些线:

updateWithNewLocation(location); // What does this do? Could you tell me?
locationManager.requestLocationUpdates(provider, 2000, 10,
        locationListener); // I don't think this is it

尝试将所有这些位置更新内容移至onProgressUpdate。我在这里失明,因为我看不到你的代码,但我猜测它用UI做了些什么。试试这个:

@Override
protected void onProgressUpdate(Location... locations) {
    super.onProgressUpdate(values);
    updateWithNewLocation(location);
}

但正如我所说,我不确定,因为除非你在这里发布代码,否则我不知道updateWithNewLocation会做什么。

答案 1 :(得分:6)

这正是为什么开始理解一些基于线程的基础知识而不仅仅是为Android中看起来与远程线程相关的所有东西说“AsyncTask”的原因。多线程可能很棘手,你必须仔细思考。

我怀疑这是发生了什么:你有一个AsyncTask,其中包括异步获取位置...这意味着你从主线程开始,你在另一个线程中隐式执行doInBackground(AsyncTask的线程),然后你调用任何正在进行你的位置获取的线程。然后你的AsyncTask线程继续,完成doInBackground中所有与JSON相关的工作,可能还要完成。 AsyncTask线程很可能完成。 然后您正在获取位置锁定,并且回调了您提供的侦听器,但现在AsyncTask已经完成,并且其线程不再有效。

基于您的代码,我怀疑您想要在获得位置后进行Google Maps API调用,对吧?如果是这样,请同步调用“获取位置”代码。在回调中,您可以放置​​代码以启动AsyncTask,现在只执行Google Maps API调用和处理。

答案 2 :(得分:5)

你在doInBackground()做了很多你不需要做的事情。只执行doInBackground()方法中会导致UI线程断断续续的内容。换句话说,所有不直接处理网络通信的代码都需要移出doInBackground()方法。