【问题标题】:Error when Android change orientation in AsyncTaskAndroid 在 AsyncTask 中更改方向时出错
【发布时间】:2015-11-03 14:48:41
【问题描述】:

大家好

我有一个承载 FrameLayout 的 Activity,因为我想将此 Activity 维护为应用程序的 Main Activity,根据菜单选项使用不同的 Fragment 进行更改。

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

这是 Activity 的片段:

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));
            }
        }
    }

}

对于片段类“fragmentDashboard”,我必须设置不同的 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 调用时,屏幕旋转一切正常。

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

在此致谢

【问题讨论】:

  • 请发布完整的 logcat 输出
  • 感谢@Nanoc,它完成了。
  • 这是一个相当普遍的问题,一般可以通过这个问题的答案stackoverflow.com/questions/32503305/…来解决
  • @ElliotM 感谢您的回答,但我在这里阅读了一些帖子,解释了为什么这种方法不是最佳选择。

标签: android android-fragments android-asynctask


【解决方案1】:

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

new Message(0, R.string.msg_account_ok_account)

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

【讨论】:

  • 非常感谢您的回答,您的评论是正确的,但是,我删除了 doInBackground 中的所有 getResources() 但我仍然遇到问题,但现在在 onPostExecute 方法中,在方法onPostExecuteMethod 非常有必要使用它,我认为无法删除
  • AsyncTask 有很多问题,主要问题 - 它们与活动生命周期的联系很差。你可以尝试其他的后台解决方案。至于我最好是github.com/ReactiveX/RxJava
  • 非常感谢您,我会检查您的建议,所以我会将您的答案标记为正确的,再次感谢。
  • 很高兴为您提供帮助!如果你为你的项目选择 RxJava,你可以试试github.com/evant/rxloader。它将帮助您处理活动 Android 的活动生命周期。您也可以尝试替代解决方案 - Bolts-Android
  • 再次感谢,是的,我会检查的。有人不喜欢这个帖子,因为给帖子打负分,我不知道为什么?我们是来帮忙的,你不觉得吗?
【解决方案2】:

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

也许您可以在 asynctask 运行时存储对资源的静态引用。

这应该可以解决您的问题。

希望这会有所帮助。

【讨论】:

  • 不要对资源使用静态引用!我对此提出警告的原因是对上下文的静态引用会导致内存泄漏!
  • 意思是在 asynctask 运行时存储它,然后将其设置为 null。还是那么糟糕吗?
  • 没有告诉我什么新鲜事,仍然认为如果你在完成工作后将其设置为 null 应该没有任何问题。
  • 无论如何,这不是最好的架构解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-21
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-25
  • 1970-01-01
相关资源
最近更新 更多