Android在AsyncTask

时间:2015-11-03 14:48:42

标签: android android-fragments android-asynctask

各位大家好

我有一个托管FrameLayout的Activity,因为我希望将此活动维护为应用程序的主要活动,并根据菜单选项更改不同的碎片。

问题在于片段中我正在做各种AsyncTask从服务器中提取数据,我知道在这个论坛中有关于这个主题的各种方法,但我找不到合适的方法。我的情况,所以我需要你的帮助来完成这项工作。

这是活动的一个片段:

public class Dashboard extends AppCompatActivity {
    protected NavigationView navigationView;
    protected ListView lv_menu_gateway;
    protected ListView lv_menu_options;
    protected AppManager manager;
    public Identity myIdentity;
    protected List<Device> devices;
    public String lastDeviceConnected;
    public DeviceData deviceData;
    protected ProgressBar pb_dashboardGeneral;
    protected List<com.realstatediary.jperera.rapidsentrymaster.poco.Menu> menus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        manager = AppManager.getManager(getApplication());
        setContentView(R.layout.activity_dashboard);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        pb_dashboardGeneral = (ProgressBar)findViewById(R.id.pb_dashboardGeneral);
        pb_dashboardGeneral.setVisibility(View.VISIBLE);
        pb_dashboardGeneral.bringToFront();
        navigationView = (NavigationView) findViewById(R.id.nav_view);
        lv_menu_gateway = (ListView)findViewById(R.id.lv_menu_gateway);
        lv_menu_options = (ListView)findViewById(R.id.lv_menu_options);
        new LoadIdentityObject(this).execute();
        GetListOfMenusOptions();
        lv_menu_options.setAdapter(new ListMenuAdapter(this, menus));
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_layout_container, new fragmentDashboard());
        transaction.commit();

        lv_menu_gateway.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                manager.SaveSharedLastDeviceConnected(devices.get(position).getDeviceNumber());
                new LoadDeviceData(getParent()).execute();
                drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
            }
        });
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
}

这是Fragment的代码,你可以看到我有4个不同的嵌套AsynckTask,我用它来提取我需要在屏幕上显示的整个数据:

public class fragmentDashboard extends Fragment {

    private OnFragmentInteractionListener mListener;
    protected AppManager manager;
    protected Identity myIdentity;
    protected Account myAccount;
    protected String lastDeviceConnected;
    protected DeviceData deviceData;
    protected TextView lb_account_info_name;
    protected TextView lb_account_address;
    protected List<Notification> notifications;
    protected ListView lv_dashboard_notifications;
    protected GridView gv_house_mode;
    protected List<HouseMode> houseModes;
    protected GridView gv_sensors_devices;
    protected List<Sensor> sensors;

    public static fragmentDashboard newInstance() {
        return new fragmentDashboard();
    }

    public fragmentDashboard() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
        manager = AppManager.getManager(this.getActivity().getApplication());
        myIdentity = manager.RetrieveSharedIdentityObject();
        lastDeviceConnected = manager.RetrieveSharedLastDeviceConnect();
        lb_account_address = (TextView)view.findViewById(R.id.lb_account_address);
        lb_account_info_name = (TextView)view.findViewById(R.id.lb_account_info_name);
        lv_dashboard_notifications = (ListView)view.findViewById(R.id.lv_dashboard_notifications);
        gv_house_mode = (GridView)view.findViewById(R.id.gv_house_mode);
        gv_sensors_devices = (GridView)view.findViewById(R.id.gv_sensors_devices);
        new LoadAccountInformation(getActivity()).execute();
        new LoadDeviceDataNotification(getActivity()).execute();
        new LoadDeviceHouseMode(getActivity()).execute();
        new LoadSensorsList(getActivity()).execute();
        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }

    public class LoadAccountInformation extends AsyncTask<Void, Void, Message>{

        public Activity activity;

        public LoadAccountInformation(Activity activity) {
            this.activity = activity;
        }

        @Override
        protected Message doInBackground(Void... params) {
            try{
                if(myIdentity != null) {
                    myAccount = manager.GetAccount(myIdentity.getServerAccount(), myIdentity.getServerAccountToken(), myIdentity.getIdentityAccountNumber());
                    if (myAccount != null) {
                        return new Message(0, getResources().getString(R.string.msg_account_ok_account));
                    }else{
                        return new Message(1, getResources().getString(R.string.msg_account_error_account));
                    }
                }else {
                    return new Message(1, getResources().getString(R.string.msg_account_error_identity));
                }
            }catch (Exception ex){
                return new Message(1, getResources().getString(R.string.msg_account_error_account));
            }
        }

        @Override
        protected void onPostExecute(Message message) {
            if(message.getMessageType() == 0){
                String name = "";
                String address = "";
                String city = "";
                String state = "";
                String zipCode = "";
                if(myAccount.getAccountFirstName() == null && myAccount.getAccountLastName() == null){
                    name = "";
                }else{
                    if(myAccount.getAccountFirstName() != null && myAccount.getAccountLastName() == null){
                        name = myAccount.getAccountFirstName();
                    }else {
                        if((myAccount.getAccountFirstName() == null) && (myAccount.getAccountLastName() != null)){
                            name = myAccount.getAccountLastName();
                        }else {
                            name = myAccount.getAccountLastName() + ", " + myAccount.getAccountFirstName();
                        }
                    }
                }
                if(myAccount.getAccountAddress1() == null && myAccount.getAccountAddress2() == null){
                    address = "";
                }else{
                    if(myAccount.getAccountAddress1() != null && myAccount.getAccountAddress2() == null){
                        address = myAccount.getAccountAddress1();
                    }else {
                        if(myAccount.getAccountAddress1() == null && myAccount.getAccountAddress2() != null){
                            address = myAccount.getAccountAddress2();
                        }else{
                            address = myAccount.getAccountAddress1() + ", " + myAccount.getAccountAddress2();
                        }
                    }
                }
                if(myAccount.getAccountCity() != null){
                    if(!address.isEmpty()){
                        city = ", " + myAccount.getAccountCity();
                    }else{
                        city = myAccount.getAccountCity();
                    }
                }
                if(myAccount.getAccountState() != null){
                    if(!address.isEmpty() || !city.isEmpty()){
                        state = ", " + myAccount.getAccountState();
                    }else{
                        state = myAccount.getAccountState();
                    }
                }
                if(myAccount.getAccountPostalCode() != null){
                    if(!address.isEmpty() || !city.isEmpty() || !state.isEmpty()){
                        zipCode = ", " + myAccount.getAccountPostalCode();
                    }else{
                        zipCode = myAccount.getAccountPostalCode();
                    }
                }
                lb_account_info_name.setText(name);
                String addressToShow = address + city + state + zipCode;
                lb_account_address.setText(addressToShow);
            }else {
                Toast.makeText(this.activity.getApplicationContext(), message.getMessageContent(), Toast.LENGTH_SHORT).show();
            }
        }
    }

    public class LoadDeviceDataNotification extends AsyncTask<Void, Void, Message>{

        private Activity activity;

        public LoadDeviceDataNotification(Activity activity){
            this.activity = activity;
        }

        @Override
        protected Message doInBackground(Void... params) {
            try{
                lastDeviceConnected = manager.RetrieveSharedLastDeviceConnect();
                if(lastDeviceConnected != null){
                    deviceData = manager.RetrieveDeviceDataObject();
                }
                return new Message(0, getString(R.string.msg_gateway_ok_devicedata));
            }catch (Exception ex){
                return new Message(1, getString(R.string.msg_gateway_error_devicedata));
            }
        }

        @Override
        protected void onPostExecute(Message message) {
            if(message.getMessageType() == 0){
                if(deviceData != null){
                    //Notifications
                    notifications = new ArrayList<>();
                    if(!deviceData.getDeviceDataComment().isEmpty()){
                        notifications.add(new Notification(1, deviceData.getDeviceDataComment()));
                    }else{
                        notifications.add(new Notification(1, getResources().getString(R.string.notification_default)));
                    }
                    lv_dashboard_notifications.setAdapter(new ListNotificationsAdapter(notifications, this.activity));
                }
            }
        }

    }

    public class LoadDeviceHouseMode extends AsyncTask<Void, Void, Message>{

        private Activity activity;

        public LoadDeviceHouseMode(Activity activity){
            this.activity = activity;
        }

        @Override
        protected Message doInBackground(Void... params) {
            try{
                lastDeviceConnected = manager.RetrieveSharedLastDeviceConnect();
                if(lastDeviceConnected != null){
                    deviceData = manager.RetrieveDeviceDataObject();
                }
                return new Message(0, getString(R.string.msg_gateway_ok_devicedata));
            }catch (Exception ex){
                return new Message(1, getString(R.string.msg_gateway_error_devicedata));
            }
        }

        @Override
        protected void onPostExecute(Message message) {
            if(message.getMessageType() == 0){
                if(deviceData != null){
                    LoadHouseModeGridView();
                }
            }
        }

    }

    protected void LoadHouseModeGridView(){
        //Building the list
        houseModes = new ArrayList<>();
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_home_48dp);
        if(deviceData != null) {
            if (deviceData.getDeviceDataMode() == 1) {
                houseModes.add(new HouseMode(1, getString(R.string.lb_house_mode_home), true, bitmap));
            } else {
                houseModes.add(new HouseMode(1, getString(R.string.lb_house_mode_home), false, bitmap));
            }
        }else{
            houseModes.add(new HouseMode(1, getString(R.string.lb_house_mode_home), false, bitmap));
        }
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_away_48dp);
        if(deviceData != null) {
            if (deviceData.getDeviceDataMode() == 2) {
                houseModes.add(new HouseMode(2, getString(R.string.lb_house_mode_away), true, bitmap));
            } else {
                houseModes.add(new HouseMode(2, getString(R.string.lb_house_mode_away), false, bitmap));
            }
        }else{
            houseModes.add(new HouseMode(2, getString(R.string.lb_house_mode_away), false, bitmap));
        }
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_night_48dp);
        if(deviceData != null) {
            if (deviceData.getDeviceDataMode() == 3) {
                houseModes.add(new HouseMode(3, getString(R.string.lb_house_mode_night), true, bitmap));
            } else {
                houseModes.add(new HouseMode(3, getString(R.string.lb_house_mode_night), false, bitmap));
            }
        }else{
            houseModes.add(new HouseMode(3, getString(R.string.lb_house_mode_night), false, bitmap));
        }
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_vacation_48dp);
        if(deviceData != null) {
            if (deviceData.getDeviceDataMode() == 4) {
                houseModes.add(new HouseMode(4, getString(R.string.lb_house_mode_vacation), true, bitmap));
            } else {
                houseModes.add(new HouseMode(4, getString(R.string.lb_house_mode_vacation), false, bitmap));
            }
        }else{
            houseModes.add(new HouseMode(4, getString(R.string.lb_house_mode_vacation), false, bitmap));
        }
        gv_house_mode.setAdapter(new ListHouseModeAdapter(getActivity(), houseModes));
    }

    public class LoadSensorsList extends AsyncTask<Void, Void, Message>{

        private Activity activity;

        public LoadSensorsList(Activity activity){
            this.activity = activity;
        }
        @Override
        protected Message doInBackground(Void... params) {
            try {
                sensors = Sensor.GenerateSensorList(manager);
                return new Message(0, getString(R.string.msg_sensors_ok));
            }catch (Exception ex){
                return new Message(1, getString(R.string.msg_sensors_error));
            }

        }

        @Override
        protected void onPostExecute(Message message) {
            if(message.getMessageType() == 0){
                gv_sensors_devices.setAdapter(new ListSensorsAdapter(activity, sensors));
            }
        }
    }

}

对于片段类&#34; fragmentDashboard&#34;我必须使用不同的布局Xml,一个用于纵向,另一个用于横向,以便在用户旋转时管理不同的外观。

最后当我旋转设备时出现问题我收到以下错误并且应用程序崩溃:

11-03 09:43:48.890 29987-30025/com.realstatediary.jperera.rapidsentrymaster E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #2
11-03 09:43:48.890 29987-30025/com.realstatediary.jperera.rapidsentrymaster E/AndroidRuntime: Process: com.realstatediary.jperera.rapidsentrymaster, PID: 29987
11-03 09:43:48.890 29987-30025/com.realstatediary.jperera.rapidsentrymaster E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
11-03 09:43:48.890 29987-30025/com.realstatediary.jperera.rapidsentrymaster E/AndroidRuntime:     at android.os.AsyncTask$3.done(AsyncTask.java:304)
11-03 09:43:48.890 29987-30025/com.realstatediary.jperera.rapidsentrymaster E/AndroidRuntime:     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
11-03 09:43:48.890 29987-30025/com.realstatediary.jperera.rapidsentrymaster E/AndroidRuntime:     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
11-03 09:43:48.890 29987-30025/com.realstatediary.jperera.rapidsentrymaster E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
11-03 09:43:48.890 29987-30025/com.realstatediary.jperera.rapidsentrymaster E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
11-03 09:43:48.890 29987-30025/com.realstatediary.jperera.rapidsentrymaster E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-03 09:43:48.890 29987-30025/com.realstatediary.jperera.rapidsentrymaster E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-03 09:43:48.890 29987-30025/com.realstatediary.jperera.rapidsentrymaster E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818)
11-03 09:43:48.890 29987-30025/com.realstatediary.jperera.rapidsentrymaster E/AndroidRuntime:  Caused by: java.lang.IllegalStateException: Fragment fragmentDashboard{447e431} not attached to Activity
11-03 09:43:48.890 29987-30025/com.realstatediary.jperera.rapidsentrymaster E/AndroidRuntime:     at android.app.Fragment.getResources(Fragment.java:788)
11-03 09:43:48.890 29987-30025/com.realstatediary.jperera.rapidsentrymaster E/AndroidRuntime:     at com.realstatediary.jperera.rapidsentrymaster.activities.fragmentDashboard$LoadAccountInformation.doInBackground(fragmentDashboard.java:148)
11-03 09:43:48.890 29987-30025/com.realstatediary.jperera.rapidsentrymaster E/AndroidRuntime:     at com.realstatediary.jperera.rapidsentrymaster.activities.fragmentDashboard$LoadAccountInformation.doInBackground(fragmentDashboard.java:126)
11-03 09:43:48.890 29987-30025/com.realstatediary.jperera.rapidsentrymaster E/AndroidRuntime:     at android.os.AsyncTask$2.call(AsyncTask.java:292)
11-03 09:43:48.890 29987-30025/com.realstatediary.jperera.rapidsentrymaster E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-03 09:43:48.890 29987-30025/com.realstatediary.jperera.rapidsentrymaster E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
11-03 09:43:48.890 29987-30025/com.realstatediary.jperera.rapidsentrymaster E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
11-03 09:43:48.890 29987-30025/com.realstatediary.jperera.rapidsentrymaster E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
11-03 09:43:48.890 29987-30025/com.realstatediary.jperera.rapidsentrymaster E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818) 

我做了一些测试,问题出在AsyncTask调用上,当我评论AsyncTask调用时,屏幕旋转一切正常。

任何人都知道我必须采取什么方法来解决这个问题?

先谢谢

2 个答案:

答案 0 :(得分:0)

在方法doInBackground中,您调用方法getResources。但是,当屏幕旋转时,您的Activity会重新创建,而您的片段有时会没有附加到Activity。因此,您当时无法getResources。您可以在之前初始化消息,例如在AsyncTask构造函数中。或者您可以在类Message中使用,仅在构造函数中使用字符串资源引用,例如:

new Message(0, R.string.msg_account_ok_account)

(当然你需要修改构造函数)

答案 1 :(得分:-2)

您对getResources()的一个调用是抛出由重新创建片段创建的破折号条件引起的异常。

也许你可以在asynctask运行时存储一个静态引用资源。

那应该解决你的问题。

希望这有帮助。