这是我的代码
package com.example.rollsystems;
import java.util.ArrayList;
import org.dto.Student;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import android.app.Activity;
import android.content.Context;
import com.example.listview.*;
import com.example.Utils.AllBO;
import com.example.rollsystems.R;
public class RollCallActivity extends Activity {
ArrayList<Student> array;
ListStudentAdapter arrayAdapter;
ListView list;
TextView txtClassAt;
TextView txtSubjectAt;
TextView txtInstructorAt;
TextView txtTimeAt;
TextView txtDateAt;
Context context;
public class RollCallTask extends AsyncTask<Void, Void, Void> {
public RollCallActivity activity;
public RollCallTask(RollCallActivity a)
{
activity = a;
}
@Override
protected Void doInBackground(Void... params) {
//SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("rs", Context.MODE_PRIVATE);
//String RollCallID = sharedPref.getString("RollCallID", "14");
String RollCallID = "14";
list = (ListView)findViewById(R.id.listAtStudent);
ArrayList<Student> rollCalls = new AllBO().getRollCallInfo(RollCallID);
array = rollCalls;
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
arrayAdapter = new ListStudentAdapter(activity, R.layout.list_atstudent, array);
list.setAdapter(arrayAdapter);
txtClassAt = (TextView) findViewById(R.id.txtClassAt);
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_rollcall);
new RollCallTask(this).execute();
txtClassAt = (TextView) findViewById(R.id.txtClassAt);
}
}
然后,当我跑:
E/AndroidRuntime(883): FATAL EXCEPTION: AsyncTask #1
E/AndroidRuntime(883): java.lang.RuntimeException: An error occured while executing
doInBackground()
E/AndroidRuntime(883): at android.os.AsyncTask$3.done(AsyncTask.java:278)
每一个帮助都是宝贵的
答案 0 :(得分:0)
您正尝试从后台线程访问UI。您需要在doInBackground
之外执行此操作。
您还必须从UI线程中调用AsyncTask
。不这样做是导致运行时错误的原因。
public class RollCallTask extends AsyncTask<Void, Void, Void> {
public RollCallActivity activity;
public RollCallTask(RollCallActivity a)
{
activity = a;
}
@Override
protected Void doInBackground(Void... params) {
//SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("rs", Context.MODE_PRIVATE);
//String RollCallID = sharedPref.getString("RollCallID", "14");
String RollCallID = "14";
ArrayList<Student> rollCalls = new AllBO().getRollCallInfo(RollCallID);
array = rollCalls;
return null;
}
}
然后你的onCreate
将
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_rollcall);
list = (ListView)findViewById(R.id.listAtStudent);
txtClassAt = (TextView) findViewById(R.id.txtClassAt);
new RollCallTask(this).execute();
}
答案 1 :(得分:0)
异步任务正在访问UI。不要在其中findViewById
,使用活动/片段中的onCreate
。
以下是一些常规问题:
String RollCallID = "14";
list = (ListView)findViewById(R.id.listAtStudent);
ArrayList<Student> rollCalls = new AllBO().getRollCallInfo(RollCallID);
array = rollCalls;
变量绝不应以大写rollCallID
xml id不应该是驼峰式R.id.list_as_student
答案 2 :(得分:0)
使用MainActivity中的Handler对象并发布可运行的对象。要从背景中使用它,您需要将对象设置为可以在MainActivity之外调用的静态,或者您可以创建活动的静态实例来访问它。
活动内部
private static Handler handler;
handler = new Handler();
handler().post(new Runnable() {
public void run() {
//ui stuff here :)
}
});
public static Handler getHandler() {
return handler;
}
活动之外
MainActivity.getHandler().post(new Runnable() {
public void run() {
//ui stuff here :)
}
});