我知道在Async Task中出现此错误,我们尝试在doInBackGround中更新UI ...
但我无法在我所在的地方得到它。
我处于一个非常庞大且稳定的代码之间......
SaveThread saveThread = new SaveThread(....);
Thread thread = new Thread(saveThread);
thread.start();
class SaveThread implements Runnable {
...........
...........
@Override
public void run() {
list = saveMultiple(mContext, array);
}
}
}
内部的API saveMultiple(context,array)
{
Places placeItem = new Places().InitFromJsonObject(context,item);
}
内部API InitFromJsonObject(context,item);
{
new Offers(context, offer);
}
内部API提供(上下文,提供)----> API保存(上下文)---->
在save API中
{
new AsyncTask<Void,Void,Void>(){
@Override
protected Void doInBackground(Void... params) {
Long now = Long.valueOf(System.currentTimeMillis());
if(context != null){
Cursor cursor = context.getContentResolver().query(CONTENT_URI, null, OFFER_ID + "=?", new String[]{offer_id}, null);
ContentValues contentValues = new ContentValues();
contentValues.put(OFFER_ID, offer_id);
int count = cursor.getCount();
if (count == 0) {
contentValues.put(CREATED_AT, now);
contentValues.put(UPDATED_AT, now);
context.getContentResolver().insert(CONTENT_URI, contentValues);
} else {
cursor.moveToFirst();
contentValues.put(UPDATED_AT, now);
context.getContentResolver().update(CONTENT_URI, contentValues, OFFER_ID + "=?", new String[]{offer_id});
}
cursor.close();
}
return null; //To change body of implemented methods use File | Settings | File Templates.
}
}.execute();
}
}
Error Logs are
java.lang.ExceptionInInitializerError
at com.y2cf.hoppr.dao.provider.models.Offers.save(Offers.java:118)
at com.y2cf.hoppr.dao.provider.models.Offers.save(Offers.java:113)
at com.y2cf.hoppr.dao.provider.models.Offers.<init>(Offers.java:90)
at com.y2cf.hoppr.dao.provider.models.Places.InitFromJsonObject(Places.java:528)
at com.y2cf.hoppr.dao.provider.models.Places.saveMultiple(Places.java:785)
at com.y2cf.hoppr.dao.provider.models.Places$SaveThread.run(Places.java:1571)
at java.lang.Thread.run(Thread.java:1019)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
at android.os.AsyncTask.<clinit>(AsyncTask.java:152)
... 7更多
执行异步任务时出现此错误,但我认为我在更新内容值时没有更新任何UI ..
我知道它的编码很糟糕,因为我们在java本机线程中启动异步任务...但是为什么会出现这个错误?
任何建议??
答案 0 :(得分:0)
您必须在UIThread中创建内部AsyncTask。