gpt4 book ai didi

java - android 致命异常 : AsyncTask #2

转载 作者:行者123 更新时间:2023-12-01 22:31:01 26 4
gpt4 key购买 nike

我正在编写 Android 应用程序,但在使用异步任务时失败了,我有一个异步任务,并且它的 doInBackground() 方法出现错误,其他一切看起来都不错,但 doInBackground() 方法无法正常工作。在 doInBackground() 方法中,我编写了用于在调试时从数据库获取记录的代码,我发现记录是在数组列表中获取的,但它们没有被馈送到
onPostExecute(String result) 请帮帮我...

private class LoadMoreAsync extends AsyncTask<Context, Void, String> {
private String strAddress;

public LoadMoreAsync(Context mContext,String strAddress) {
/*this.strCardNo = strCardNo;
this.strFirstName = strFirstName;
this.strMiddleName = strMiddleName;*/
this.strAddress = strAddress;
/*this.strCardNo = strCardNo;*/
}

@Override
protected void onPreExecute() {
super.onPreExecute();
ToastUtils.showToast(mContext, "Loading More...");
}

@Override
protected String doInBackground(Context... arg0) {
mVoterArray = new ArrayList<AddressInfo>();
mVoterArray = extDbUtil.getAddresslist(strAddress , limit += 20);
if (mVoterArray.isEmpty() || mVoterArray == null)
return null;
else
return "";
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
IOUtils.stopLoading();
if (result == null) {
ToastUtils.showToast(mContext, "No Record Found!!");
} else {
int position = mListView.getLastVisiblePosition();
mListAdapter = new ListAdapterNew2(mContext,
android.R.layout.simple_list_item_1, mVoterArray);
mListView.setAdapter(mListAdapter);
mListView.setSelectionFromTop(position, 0);
mListAdapter.notifyDataSetChanged();
}
}
}

extDbUtil.getAddresslist(strAddress , limit += 20) 的代码;方法是

public ArrayList<AddressInfo> getAddresslist(String strAddress,int limit) {
open();
ArrayList<AddressInfo> mAddressArray = new ArrayList<AddressInfo>();
String mQuery = "SELECT Address1,TotalMale,TotalFemale,TotalVoters, SrNo FROM "
+ TABLE_ADDRESS;
String Where = "";
if (!strAddress.equals("NA")) {
Where = " WHERE EAddress1 Like '%" + strAddress + "%'";

}
Log.i("SearchQuery", mQuery + Where + " LIMIT " + limit);
Cursor mCursor = mDatabase.rawQuery(mQuery + Where + " LIMIT " + limit,
null);

if (mCursor != null) {
if (mCursor.moveToFirst()) {
do {
AddressInfo mAddress = new AddressInfo();
mAddress.setSrNO(mCursor.getString(mCursor
.getColumnIndex("SrNo")));
mAddress.setAddress(mCursor.getString(mCursor
.getColumnIndex("Address1")));
mAddress.setTotalMale(mCursor.getString(mCursor
.getColumnIndex("TotalMale")));
mAddress.setTotalFemal(mCursor.getString(mCursor
.getColumnIndex("TotalFemale")));
mAddress.setTotalVoter(mCursor.getString(mCursor
.getColumnIndex("TotalVoters")));

} while (mCursor.moveToNext());
}


}
mCursor.close();
CleanUp();
return mAddressArray;
}

出现的错误是

    01-03 22:59:05.310: D/memalloc(15369): ion: Mapped buffer base:0x51d44000 size:4096 offset:0 fd:87
01-03 22:59:05.310: I/SearchQuery(15369): SELECT Address1,TotalMale,TotalFemale,TotalVoters, SrNo FROM Addresses WHERE EAddress1 Like '%R%' LIMIT 20
01-03 22:59:05.330: W/dalvikvm(15369): threadid=16: thread exiting with uncaught exception (group=0x41a09600)
01-03 22:59:05.340: D/memalloc(15369): ion: Mapped buffer base:0x555da000 size:196608 offset:0 fd:84
01-03 22:59:05.340: D/memalloc(15369): ion: Mapped buffer base:0x51d4b000 size:4096 offset:0 fd:90
01-03 22:59:05.340: E/AndroidRuntime(15369): FATAL EXCEPTION: AsyncTask #3
01-03 22:59:05.340: E/AndroidRuntime(15369): java.lang.RuntimeException: An error occured while executing doInBackground()
01-03 22:59:05.340: E/AndroidRuntime(15369): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-03 22:59:05.340: E/AndroidRuntime(15369): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-03 22:59:05.340: E/AndroidRuntime(15369): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-03 22:59:05.340: E/AndroidRuntime(15369): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-03 22:59:05.340: E/AndroidRuntime(15369): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-03 22:59:05.340: E/AndroidRuntime(15369): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-03 22:59:05.340: E/AndroidRuntime(15369): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-03 22:59:05.340: E/AndroidRuntime(15369): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-03 22:59:05.340: E/AndroidRuntime(15369): at java.lang.Thread.run(Thread.java:864)
01-03 22:59:05.340: E/AndroidRuntime(15369): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
01-03 22:59:05.340: E/AndroidRuntime(15369): at android.os.Handler.<init>(Handler.java:121)
01-03 22:59:05.340: E/AndroidRuntime(15369): at android.widget.Toast$TN.<init>(Toast.java:322)
01-03 22:59:05.340: E/AndroidRuntime(15369): at android.widget.Toast.<init>(Toast.java:91)
01-03 22:59:05.340: E/AndroidRuntime(15369): at android.widget.Toast.makeText(Toast.java:238)
01-03 22:59:05.340: E/AndroidRuntime(15369): at com.phoenix.rajtantra.SearchByAdd$List_Async.doInBackground(SearchByAdd.java:284)
01-03 22:59:05.340: E/AndroidRuntime(15369): at com.phoenix.rajtantra.SearchByAdd$List_Async.doInBackground(SearchByAdd.java:1)
01-03 22:59:05.340: E/AndroidRuntime(15369): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-03 22:59:05.340: E/AndroidRuntime(15369): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-03 22:59:05.340: E/AndroidRuntime(15369): ... 5 more
01-03 22:59:05.540: D/memalloc(15369): ion: Mapped buffer base:0x5570a000 size:1536000 offset:0 fd:92
01-03 22:59:05.540: D/memalloc(15369): ion: Mapped buffer base:0x53c33000 size:4096 offset:0 fd:94

最佳答案

如果您阅读堆栈跟踪,它会告诉您根本原因:

Caused by: java.lang.RuntimeException: Can't create handler inside thread 
that has not called Looper.prepare()

another answer here on SO这详细地涵盖了这个问题。

关于java - android 致命异常 : AsyncTask #2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27757567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com