【发布时间】: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