将数据从onPostExecute()传递到适配器

时间:2015-04-14 15:40:02

标签: android android-asynctask

无法将onPostExecute()的数据传递给AutoComleteTextView的适配器。 Logcat告诉我:

  

performFiltering()期间发生异常! java.lang.NullPointerException:collection == null。

public class UzActivity extends Activity {

private static final String DEBUG_TAG = "HttpExample";
List<String> responseList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_uz);

    final String url = "http://booking.uz.gov.ua/purchase/station/%D0%9A%D0%B8%D0%B5/";
    new FetchStationTask().execute(url);





   AutoCompleteTextView textView = (AutoCompleteTextView)
            findViewById(R.id.autoCompleteTextView1);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, responseList);

    textView.setAdapter(adapter);


}

private class FetchStationTask extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... urls) {
        try {
            return new UzFetcher().getUrlString(urls[0]);
        } catch (IOException e) {
            return "Unable to retrieve web page. URL may be invalid.";
        }
    }

    @Override
    protected void onPostExecute(String result){

        try {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            StationResponse st = objectMapper.readValue(result, StationResponse.class);
            responseList = new ArrayList<>();
            for (int i = 0; i<st.mStations.size(); i++){

                    responseList.add(st.mStations.get(i).getTitle());

            }


            Log.i(DEBUG_TAG, responseList.get(0));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.i(DEBUG_TAG, result);

    }
}

1 个答案:

答案 0 :(得分:2)

  

java.lang.NullPointerException:collection == null。

ArrayList应首先初始化。

添加,

responseList = new ArrayList<String>();

setContentView();

之后