Java 调用适配器类的方法时出现ANR
我正在尝试从SQLite检索表的行数。我用来执行此任务的代码是冻结我的应用程序。这完全是在后端运行的,MainUI线程中没有任何内容。但是,我也得到了一个错误 这是我的代码,我在这里调用方法-Java 调用适配器类的方法时出现ANR,java,android,Java,Android,我正在尝试从SQLite检索表的行数。我用来执行此任务的代码是冻结我的应用程序。这完全是在后端运行的,MainUI线程中没有任何内容。但是,我也得到了一个错误 这是我的代码,我在这里调用方法- public class del_person extends Activity{ String[] images = {}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreat
public class del_person extends Activity{
String[] images = {};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.del_person);
final adapter info = new adapter(this);
// new del_person_Dup(null).execute();
new Task(info).execute((Void)null);
}
}
class Task extends AsyncTask<Void, Void, Void> {
String[] images = {};
adapter mAdapter;
Task(adapter adapter) {
mAdapter = adapter;
}
@Override
protected Void doInBackground(Void... params) {
// the code from the Runnable using the mAdapter instead of info.
Log.v("distinctrowcount() is:",""+mAdapter.distinctrowcount()+"");
Log.v("started del_person.java","started del_person.java");
for (int i=0;i<mAdapter.distinctrowcount();i++)
{
Log.v ("This is the "+i+"th iteration","This is the "+i+"th iteration");
images[i] =mAdapter.getPersonList(i+1);
Log.v("Persons",images[i]);
}
return null;
}
}
helper.java-getdistinctrc():
异步任务代码:
class Task extends AsyncTask<Void, Void, Void> {
String[] images = {};
adapter mAdapter;
Task(adapter adapter) {
mAdapter = adapter;
}
@Override
protected Void doInBackground(Void... params) {
// the code from the Runnable using the mAdapter instead of info.
Log.v("distinctrowcount() is:",""+mAdapter.distinctrowcount()+"");
Log.v("started del_person.java","started del_person.java");
for (int i=0;i<mAdapter.distinctrowcount();i++)
{
Log.v ("This is the "+i+"th iteration","This is the "+i+"th iteration");
images[i] =mAdapter.getPersonList(i+1);
Log.v("Persons",images[i]);
}
return null;
}
}
类任务扩展了异步任务{
字符串[]图像={};
适配器马达;
任务(适配器){
mAdapter=适配器;
}
@凌驾
受保护的Void doInBackground(Void…参数){
//来自Runnable的代码使用mAdapter而不是info。
Log.v(“distinctrowcount()是:”,“+mAdapter.distinctrowcount()+”);
Log.v(“starteddel_person.java”、“starteddel_person.java”);
对于(int i=0;i您当前的代码没有在后台线程上运行这些数据库查询。请使用AsyncTask
如下所示:
static class Task extends AsyncTask<Void, Void, Void> {
adapter mAdapter;
Task(adapter adapter) {
mAdapter = adapter;
}
@Override
protected Void doInBackground(Void... params) {
// the code from the Runnable using the mAdapter instead of info.
return null;
}
在后台线程上运行该方法的具体位置是什么?在代码中,您只需声明一个Runnable
对象,并立即直接调用其run()
方法。感谢您的回复@Luksprog,我只想运行一个for循环(我在run()方法中有这个循环),我将在其中显示日志消息。这就是我希望代码执行的操作。然后删除Runnable
,使用AsyncTask
并在doInBackground()中调用该方法
method.@Luksprog,我试过了,但是我遇到了语法错误,我是android的新手。如果你不介意,你能告诉我,如果可能的话,如何在AsyncTask中通过修改我的代码来实现相同的循环吗?请?谢谢你的回答@Luksprog,我如何在这里创建info
变量?@nkiinfo
是变量您当前可以在onCreate()
方法中创建。将其传递给AsyncTask
。应用程序现在没有冻结,但我无法发布日志消息log.v(“人员”,图像[I])
在for
循环中??不能AsyncTask发布日志消息吗?@nki异步任务可以发布日志消息。你在logcat中得到log.v(“distinctrowcount()是:”,“+info.distinctrowcount()+”);
行吗?如果是,你得到了多少值?我没有得到那行@Luksprog。
class Task extends AsyncTask<Void, Void, Void> {
String[] images = {};
adapter mAdapter;
Task(adapter adapter) {
mAdapter = adapter;
}
@Override
protected Void doInBackground(Void... params) {
// the code from the Runnable using the mAdapter instead of info.
Log.v("distinctrowcount() is:",""+mAdapter.distinctrowcount()+"");
Log.v("started del_person.java","started del_person.java");
for (int i=0;i<mAdapter.distinctrowcount();i++)
{
Log.v ("This is the "+i+"th iteration","This is the "+i+"th iteration");
images[i] =mAdapter.getPersonList(i+1);
Log.v("Persons",images[i]);
}
return null;
}
}
static class Task extends AsyncTask<Void, Void, Void> {
adapter mAdapter;
Task(adapter adapter) {
mAdapter = adapter;
}
@Override
protected Void doInBackground(Void... params) {
// the code from the Runnable using the mAdapter instead of info.
return null;
}
new Task(info).execute((Void)null);