0

I have an app that is loading data from text files and storing the data from each file into a separate ArrayList. I was told to load the data on an async thread even though my app does not crash loading it all from the onCreate method (it just takes approx 12 seconds to load all of the data from the text files into the ArrayLists).

I have attempted to set up an async thread, but I have run into an issue. I am brand new to async threads, so I do not know/yet fully understand all of the details (I am trying to piece info together from different sources). Here is the relevent code:

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

    new loadData().execute();

    arrayListElement = ArrayList1.get(0);

}

private class loadData extends AsyncTask<Void, Void, Void> {

    ProgressDialog dialog;
    protected void onPreExecute() {
        dialog = new ProgressDialog(ClassName.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
    // TODO Auto-generated method stub
        populateArrayLists();
        return null;
    }

    /*protected void onProgressUpdate() {

    }

    protected void onPostExecute() {

    }*/

}

I am getting an IndexOutOfBoundsException (invalid index 0, size is 0) at the last statement in the onCreate method.

All I want to do is load the data on a separate thread because even though my app does not crash, I was told I should do it. The UI thread should handle all of the rest. The app was working fine until I added the async thread code.

How my files are read can be seen here: Android: Asynch Thread Needed?

What am I doing wrong?

4

2 回答 2

2

AsyncTask 尚未完成运行,这就是为什么arrayListElement = ArrayList1.get(0);有一个IndexOutOfBoundsException.

尝试这个

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

    new loadData().execute();
}

private class loadData extends AsyncTask<Void, Void, Void> {

    ProgressDialog dialog;
    protected void onPreExecute() {
        dialog = new ProgressDialog(ClassName.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {
    // TODO Auto-generated method stub
        populateArrayLists();
        return null;
    }

    protected void onPostExecute(Void result) {
        arrayListElement = ArrayList1.get(0);
        dialog.dismiss();
        super.onPostExecute(result);
    }

}
于 2013-02-06T02:21:45.733 回答
1

您从 doInBackground 返回 null,所以这一切当然都失败了。使用 AsyncTask 来完成它的任务。要使其正常工作,您需要使用适当的类型参数正确定义您的 AsyncTask。

与其使 populateArrayLists 具有更新其他字段的副作用,不如使用如下方法:

void List<Foo> getListOfFoo() {
  ArrayList<Foo> listOfFoos = getTheListSomehow();
  return listOfFoos;
}

例如:

    public class MyActivity extends Activity {


        public void doSomething(List<String> stringList) {
           //do something here

        }

        class MyAsyncTask extends AsyncTask<Void, Void, List<String>> {

            @Override
            protected List<String> doInBackground(Void... params) {
                ArrayList<String> listOfStrings = getListOfStrings();
                return listOfStrings;
            }

            @Override
            protected void onPostExecute(List<String> strings) {
                super.onPostExecute(strings);
                doSomething(strings);
            }

            private List<String> getListOfStrings() {
ArrayList<String> stringList = new ArrayList<String>();
                //This is where you'd actually perform your expensive operation
  BufferedReader br = null;
    try {
        br = new BufferedReader(new InputStreamReader(getAssets().open(
                "text1.txt")));
        String text;
        while ((word = br.readLine()) != null) {
            stringList.add(text);
        }                           
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            br.close(); // stop reading
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
                return stringList;
            }
        }
    }
于 2013-02-06T02:29:26.350 回答