程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> android-AsyncTask中的 PostExecute() 不能加載

android-AsyncTask中的 PostExecute() 不能加載

編輯:編程綜合問答
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,可以正常運行。
這是什麼問題呢?

最佳回答:


doInBackground()postExecute()中把 dialog.dismiss();這一行刪除。
postExecute() 方法中添加下面的代碼

csl = new  UserFollowingListAdapter(HomeActivity.this,R.layout.user_list_item,users1,this);
lv.setAdapter(csl);
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved