2 myq 26 myq_26 于 2013.01.23 17:17 提问

AsyncTask中的 PostExecute() 不能加载

我执行了下面的代码:

package com.crumbin.tabs;

//java package
import java.io.IOException;
import java.util.ArrayList;

import org.json.JSONException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;

import com.crumbin.main.R;
import com.crumbin.models.User;
import com.crumbin.utilities.UserFollowingListAdapter;


public class HomeActivity extends Activity{

     final ArrayList<User> users1 = new ArrayList<User>();  
     User user;
//  public ArrayList<User> users1 = new ArrayList<User>();
     private UserFollowingListAdapter csl;
        private ListView lv;
   @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.user_main_tab_home);
      lv = (ListView) findViewById(R.id.user_crumbs_list); 

     // ListView lv = (ListView) findViewById(R.id.user_crumbs_list);
      AsyncLoader task = new AsyncLoader();
      task.execute();
     }
      private    class AsyncLoader extends AsyncTask<String,Void, String> {
               ProgressDialog dialog;

            @Override

            protected void onPreExecute(){
                 String pa = "helllo";
                   return;
            }
            @Override
            protected String doInBackground(String... pa) {
                String response = "";

                    try {

                      try {
                            user = new User("4eeb34c6d80e8f1214000000");
                            user.getFollowingCrumbsUpList();
                            for(User u : user.following){
                                users1.add(u);
                            }
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

            return response;
            }

            protected void onPostExecute(String result) {
                //dialog.dismiss();
                ListView lv = (ListView) findViewById(R.id.user_crumbs_list);

                final UserFollowingListAdapter csl = new UserFollowingListAdapter(HomeActivity.this,R.layout.user_crumbs_list_item,users1,this);
                    lv.setAdapter(csl);
                return;
            }

       }
       void showError(){
       new AlertDialog.Builder(this)
       .setTitle(" Oops , Server down :( ")
       .setPositiveButton("Ok", new DialogInterface.OnClickListener() {

           @Override
           public void onClick(DialogInterface arg0, int arg1) {
               // TODO Auto-generated method stub

           }
           //
       }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int whichButton) {
               // Do nothing.
           }
       }).show();

     }
}

Error:

01-22 13:11:34.596: E/AndroidRuntime(2464): FATAL EXCEPTION: main
01-22 13:11:34.596: E/AndroidRuntime(2464): java.lang.ClassCastException: com.crumbin.tabs.HomeActivity$AsyncLoader cannot be cast to android.app.Activity
01-22 13:11:34.596: E/AndroidRuntime(2464):     at com.crumbin.utilities.UserFollowingListAdapter.<init>(UserFollowingListAdapter.java:40)
01-22 13:11:34.596: E/AndroidRuntime(2464):     at com.crumbin.tabs.HomeActivity$AsyncLoader.onPostExecute(HomeActivity.java:106)
01-22 13:11:34.596: E/AndroidRuntime(2464):     at com.crumbin.tabs.HomeActivity$AsyncLoader.onPostExecute(HomeActivity.java:1)
01-22 13:11:34.596: E/AndroidRuntime(2464):     at android.os.AsyncTask.finish(AsyncTask.java:602)
01-22 13:11:34.596: E/AndroidRuntime(2464):     at android.os.AsyncTask.access$600(AsyncTask.java:156)
01-22 13:11:34.596: E/AndroidRuntime(2464):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
01-22 13:11:34.596: E/AndroidRuntime(2464):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-22 13:11:34.596: E/AndroidRuntime(2464):     at android.os.Looper.loop(Looper.java:137)
01-22 13:11:34.596: E/AndroidRuntime(2464):     at android.app.ActivityThread.main(ActivityThread.java:4424)
01-22 13:11:34.596: E/AndroidRuntime(2464):     at java.lang.reflect.Method.invokeNative(Native Method)
01-22 13:11:34.596: E/AndroidRuntime(2464):     at java.lang.reflect.Method.invoke(Method.java:511)
01-22 13:11:34.596: E/AndroidRuntime(2464):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-22 13:11:34.596: E/AndroidRuntime(2464):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-22 13:11:34.596: E/AndroidRuntime(2464):     at dalvik.system.NativeStart.main(Native Method)

没有数据加载,循环甚至没有执行到 OnpostExecute()。错误可能是什么呢?我没获得错误信息,因为程序没执行到那一块。
我把数据加载到list 中,还是不能正常运行。如果不使用 AsyncTask,可以正常运行。
这是什么问题呢?

2个回答

CSDN_Ask
CSDN_Ask   2013.01.28 11:26
已采纳

doInBackground()postExecute()中把 dialog.dismiss();这一行删除。
postExecute() 方法中添加下面的代码

csl = new  UserFollowingListAdapter(HomeActivity.this,R.layout.user_list_item,users1,this);
lv.setAdapter(csl);
nana4ever
nana4ever   2013.01.24 10:12

你的AsyncTask类应该在onCreate()方法外,不应该在onCreate()方法里面

 UserFollowingListAdapter csl;

     @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.user_main_tab_home);
          .............
          ListView lv = (ListView) findViewById(R.id.user_crumbs_list);

          csl = new UserFollowingListAdapter(HomeActivity.this,
                                   R.layout.user_crumbs_list_item,users1,this);
          lv.setAdapter(csl);
          new AsyncLoader().execute(user);  
     }
 class AsyncLoader extends AsyncTask<User,Void, String> {

    ..................
    protected void onPostExecute(String result) {
       csl.notifyDataSetChanged(); // refresh your ListView
    }
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!