错误提示:09-13 14:59:32.462 7428-7452/com.chen.zhou.myapplication4 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 Process: com.chen.zhou.myapplication4, PID: 7428 java.lang.RuntimeException: An error occurred while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:309) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) at java.util.concurrent.FutureTask.setException(FutureTask.java:223) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818) Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at android.os.Handler.<init>(Handler.java:200) at android.os.Handler.<init>(Handler.java:114) at android.app.Activity.<init>(Activity.java:754) at android.support.v4.app.BaseFragmentActivityGingerbread.<init>(BaseFragmentActivityGingerbread.java:34) at android.support.v4.app.BaseFragmentActivityHoneycomb.<init>(BaseFragmentActivityHoneycomb.java:29) at android.support.v4.app.BaseFragmentActivityJB.<init>(BaseFragmentActivityJB.java:29) at android.support.v4.app.FragmentActivity.<init>(FragmentActivity.java:75) at android.support.v7.app.AppCompatActivity.<init>(AppCompatActivity.java:61) at com.chen.zhou.myapplication4.ItemBean.<init>(ItemBean.java:8) at com.chen.zhou.myapplication4.MainActivity.getItemBean(MainActivity.java:58) at com.chen.zhou.myapplication4.MainActivity.access$000(MainActivity.java:22) at com.chen.zhou.myapplication4.MainActivity$NewAsyncTask.doInBackground(MainActivity.java:38) at com.chen.zhou.myapplication4.MainActivity$NewAsyncTask.doInBackground(MainActivity.java:34) at android.os.AsyncTask$2.call(AsyncTask.java:295) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818) 09-13 14:59:33.123 7428-7455/com.chen.zhou.myapplication4 E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f40c7d1a010源码如下:MainActivityandroid.os.AsyncTaskandroid.support.v7.app.AppCompatActivityandroid.os.Bundleandroid.util.Logandroid.widget.ListVieworg.json.JSONArrayorg.json.JSONExceptionorg.json.JSONObjectjava.io.BufferedInputStreamjava.io.BufferedReaderjava.io.IOExceptionjava.io.InputStreamjava.io.InputStreamReaderjava.io.UnsupportedEncodingExceptionjava.net.URLjava.util.ArrayListjava.util.ListMainActivity AppCompatActivity {
String =ListView (Bundle savedInstanceState) {
.onCreate(savedInstanceState)setContentView(R.layout.)= (ListView) findViewById(R.id.)NewAsyncTask().execute()}
String (InputStream is){
String result=InputStreamReader isr= String line={
isr = InputStreamReader(is)BufferedReader br=BufferedReader(isr)((line=br.readLine())!=){
result+=line}
} (UnsupportedEncodingException e) {
e.printStackTrace()} (IOException e) {
e.printStackTrace()}
result}
List<ItemBean>(String url) {
List<ItemBean> itemBeanlist = ArrayList<ItemBean>()ItemBean itemBean{
String jsonString = readUrl(URL(url).openStream())JSONObject jsonObject = JSONObject(jsonString)JSONArray jsonArray = jsonObject.getJSONArray()(i = i < i++) {
jsonObject = jsonArray.getJSONObject(i)itemBean = ItemBean()itemBean.= jsonObject.getString()itemBean.= jsonObject.getString()itemBean.= jsonObject.getString()itemBeanlist.add(itemBean)}
} (IOException e) {
e.printStackTrace()} (JSONException e) {
e.printStackTrace()}
itemBeanlist}
NewAsyncTask AsyncTask<StringVoidList<ItemBean>>{
List<ItemBean> (String... strings) {
getItemBean(strings[])}
(List<ItemBean> itemBeanList) {
.onPostExecute(itemBeanList)MyAdapter adapter=MyAdapter(itemBeanListMainActivity.).setAdapter(adapter)}
}
}ItemBean:android.support.v7.app.AppCompatActivityItemBean {
String String String }MyAdapter:android.content.Contextandroid.view.LayoutInflaterandroid.view.Viewandroid.view.ViewGroupandroid.widget.BaseAdapterandroid.widget.ImageSwitcherandroid.widget.ImageViewandroid.widget.TextViewjava.util.ListMyAdapter BaseAdapter {
List<ItemBean> LayoutInflater Context MyAdapter(List<ItemBean> itemBeanListContext context){
.=itemBeanList=LayoutInflater.(context)}
() {
.size()}
Object (i) {
.get(i)}
(i) {
i}
View (iView viewViewGroup viewGroup) {
ViewHolder viewHolder=(view==){
viewHolder=ViewHolder()view=.inflate(R.layout.)viewHolder.= (ImageView) view.findViewById(R.id.)viewHolder.= (TextView) view.findViewById(R.id.)viewHolder.= (TextView) view.findViewById(R.id.)view.setTag(viewHolder)}
{
viewHolder=(ViewHolder)view.getTag()}
viewHolder..setText(.get(i).)viewHolder..setText(.get(i).)viewHolder..setImageResource(R.mipmap.)view}
ViewHolder{
ImageView TextView TextView }
}
1 回答
ziom
TA贡献948条经验 获得超1109个赞
异常信息里说的很清楚了:
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
不能在子线程创建handler。你看一下你是不是在异步加载的doInBackground()里创建的handler
添加回答
举报
0/150
提交
取消