1

我正在尝试使用从网络服务器获得的一些坐标刷新地图。这是我的异步任务:

public class GetUsersAsyncTask extends AsyncTask<String, Void, String>
{

    HttpResponse response = null;
    InputStream is = null;
    AndroidHttpClient httpclient;
    String responseString = null;

    @SuppressLint("NewApi")
    @Override
    protected String doInBackground(String... url) {

        try{
            Log.i("entrabackground",""+(++ae));
        httpclient = AndroidHttpClient.newInstance("Android");
        Log.i("!!","upa upa");
        HttpUriRequest request = new HttpGet(mUrl+eventid);
        Log.i("!!","taaa beemmm");
        request.addHeader("Accept","application/json");
        Log.i("!!","oh pahhh");

    response = httpclient.execute(request);

    } catch (ClientProtocolException e1) {
            Log.i("erro1","Erro");
        e1.printStackTrace();
    } catch (IOException e1) {
        Log.i("erro2","Erro");
        e1.printStackTrace();
    }
        httpclient.close();

    try {
        if( String.valueOf(response.getEntity()) == "null")
        {
            responseString= "null";
            Log.i("null", responseString.toString());
        }
        else
        {
            is = response.getEntity().getContent();
            responseString = convertStreamToString(is);
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Log.i("saibackgroud",""+(++ao));

    return responseString;
}

@Override
protected void onPostExecute(String responseString) {
    //super.onPostExecute(responseString);
    //Do anything with response..
    //Toast.makeText(getApplicationContext(),                 responseString,Toast.LENGTH_SHORT).show();

    Log.i("entrapost",""+(++ai));
    try 
    {
        json = new JSONObject(responseString.trim());
        //Toast.makeText(getApplicationContext(), json.toString(), Toast.LENGTH_SHORT).show();
    }                   
    catch (JSONException e) {
            e.printStackTrace();
    }
    try{
        Log.i("faz isto",""+(++au));
        users_locations = json.getJSONArray("user");
        userdraw.removeOverlay(item);
        mapView.invalidate();


    user_name=users_locations.getJSONObject(0).getString("name");
    user_lat=users_locations.getJSONObject(0).getDouble("latitude");
    user_lng=users_locations.getJSONObject(0).getDouble("longitude");
    userpoint = new GeoPoint((int) (user_lat * 1E6), (int) (user_lng * 1E6));
    item = new OverlayItem(userpoint,null,null);
    //Get the new Drawable
    //Set its bounds
    usermarker.setBounds(-usermarker.getIntrinsicWidth()/2,-   usermarker.getIntrinsicHeight(),usermarker.getIntrinsicWidth()/2,0);
    //Set the new marker
    item.setMarker(usermarker);
    userdraw.addOverlay(item);      
    mc.animateTo(userpoint);
    mapView.invalidate();
    Log.i("userslocations",user_name+" "+user_lat+" "+user_lng);

    }catch (JSONException e) {
         Toast.makeText(getApplicationContext(), "Connection Lost", Toast.LENGTH_SHORT).show();
         e.printStackTrace();
     }

此 asyncTask 从处理程序中调用:

handler = new Handler();
      runnable = new Runnable() {
       @Override
       public void run() {
          Log.i("url",mUrl+eventid);
          /* do what you need to do */
          //call asynctask
          GetUsersAsyncTask getusers = new GetUsersAsyncTask();
          getusers.execute(mUrl+eventid).toString();

          /* and here comes the "trick" */
          //10 seconds delay
          handler.postDelayed(this, 30000);
       }
    };
    runnable.run();

PostExecution 任务的某个点发生错误,我不知道为什么。好像开启了json = new JSONObject(responseString.trim());

这是我的 LogCat:

01-16 00:44:37.005:E/AndroidRuntime(8625):未捕获的处理程序:线程主因未捕获的异常而退出 01-16 00:44:37.105:E/AndroidRuntime(8625):java.lang.NullPointerException 01-16 00:44:37.105: E/AndroidRuntime(8625): 在 com.example.su2.EventMap$GetUsersAsyncTask.onPostExecute(EventMap.java:273) 01-16 00:44:37.105: E/AndroidRuntime(8625): 在 com .example.su2.EventMap$GetUsersAsyncTask.onPostExecute(EventMap.java:1) 01-16 00:44:37.105: E/AndroidRuntime(8625): 在 android.os.AsyncTask.finish(AsyncTask.java:417) 01- 16 00:44:37.105: E/AndroidRuntime(8625): 在 android.os.AsyncTask.access$300(AsyncTask.java:127) 01-16 00:44:37.105: E/AndroidRuntime(8625): 在 android.os .AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429) 01-16 00:44:37.105: E/AndroidRuntime(8625): 在 android.os.Handler。dispatchMessage(Handler.java:99) 01-16 00:44:37.105: E/AndroidRuntime(8625): 在 android.os.Looper.loop(Looper.java:123) 01-16 00:44:37.105: E/ AndroidRuntime(8625): 在 android.app.ActivityThread.main(ActivityThread.java:4370) 01-16 00:44:37.105: E/AndroidRuntime(8625): 在 java.lang.reflect.Method.invokeNative(Native Method) 01-16 00:44:37.105: E/AndroidRuntime(8625): 在 java.lang.reflect.Method.invoke(Method.java:521) 01-16 00:44:37.105: E/AndroidRuntime(8625): 在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 01-16 00:44:37.105: E/AndroidRuntime(8625): at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:626) 01-16 00:44:37.105: E/AndroidRuntime(8625): 在 dalvik.system.NativeStart.main(Native Method) 01-16 00:44:37.285: E/SemcCheckin(8625): 获取崩溃转储级别:java.io.FileNotFoundException:/data/semc-checkin/crashdump

你能帮助我吗?

4

1 回答 1

0

你确定responseString不为空。

如果它为null,则调用responseString.trim()将导致java.lang.NullPointerException,如上面的堆栈跟踪所示。

于 2013-01-16T08:48:59.673 回答