Java 执行doInBackground时出错?
这是我的密码Java 执行doInBackground时出错?,java,android,listview,onclicklistener,Java,Android,Listview,Onclicklistener,这是我的密码 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 a
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);
}
}
每一个帮助都是宝贵的您试图从后台线程访问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;
}
}
异步任务正在访问UI。不要在其中执行
findViewById
,使用活动/片段中的onCreate
这里还有几个公约问题:
String RollCallID = "14";
list = (ListView)findViewById(R.id.listAtStudent);
ArrayList<Student> rollCalls = new AllBO().getRollCallInfo(RollCallID);
array = rollCalls;
String RollCallID=“14”;
列表=(ListView)findViewById(R.id.listAtStudent);
ArrayList rollCalls=new AllBO().getRollCallInfo(RollCallID);
数组=roll调用;
- 变量不应以大写的rollCallID开头
- xml id不应为驼峰式
R.id.list\u as\u student
- 数组不在适配器外部使用,为什么要在适配器外部使用变量
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 :)
}
});
活动之外
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 :)
}
});
你能发布整个日志吗,这样我们就可以看到确切的错误在你的doInBackground方法上放置一个断点,然后逐步查找错误。哦,好的,我现在看到了。您正在从
doInBackground
调用另一个asynctask
。必须从UI线程调用Asynctasks,否则它们将抛出您在此处获得的RuntimeException
。