我正在使用我的csv文件中写入的数据加载自动完整文本视图,但该文件包含数千个数据,因此加载需要花费大量时间,是否有任何方法可以在后台完成。 我使用过异步任务但无法正常实现。
我的代码:
ArrayAdapter<String> TitleAdapter;
protected Void doInBackground(Void... params) {
readTitle();
return null;
}
public void readTitle()
{
InputStream is = _context.getResources().openRawResource(
R.raw.title);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr, 8192);
try {
String test;
while ((test = br.readLine()) != null) {
String[] RowData = test.split(",");
String Title = RowData[0];
TitleAdapter.add(Title);
}
isr.close();
is.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
我的堆栈跟踪:
03-01 15:06:35.396: ERROR/AndroidRuntime(572): FATAL EXCEPTION: AsyncTask #1
03-01 15:06:35.396: ERROR/AndroidRuntime(572): java.lang.RuntimeException: An error occured while executing doInBackground()
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.os.AsyncTask$3.done(AsyncTask.java:200)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at java.lang.Thread.run(Thread.java:1096)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.view.ViewRoot.requestLayout(ViewRoot.java:594)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.view.View.requestLayout(View.java:8125)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.view.View.requestLayout(View.java:8125)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.widget.AbsListView.requestLayout(AbsListView.java:993)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:790)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:31)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.widget.ArrayAdapter.notifyDataSetChanged(ArrayAdapter.java:247)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.widget.ArrayAdapter.add(ArrayAdapter.java:179)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at com.Salary.Others.ReadFileHelper.readJobTitle(ReadFileHelper.java:111)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at com.Salary.Others.ReadFileHelper.doInBackground(ReadFileHelper.java:57)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at com.Salary.Others.ReadFileHelper.doInBackground(ReadFileHelper.java:1)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-01 15:06:35.396: ERROR/AndroidRuntime(572): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
我认为这是因为在doinbackground我们无法做任何UI工作。 所以,如何处理这个,还有其他任何方式..
答案 0 :(得分:1)
您在问题中粘贴了什么问题,加载时间过长或崩溃日志?
如果是崩溃,则无法从后台线程调用ArrayAdapter.add
,如果doInBackground
方法属于AsyncTask
,则可以添加onPostExecute
到将在UI线程上运行的AsyncTask
,并在该方法中执行add
。