我已经创建了一个小的测试应用程序来代表我的问题。 我使用ActionBarSherlock来实现标签与(Sherlock)片段。

我的代码: TestActivity.java

public class TestActivity extends SherlockFragmentActivity {
    private ActionBar actionBar;

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

    private void setupTabs(Bundle savedInstanceState) {
        actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        addTab1();
        addTab2();
    }

    private void addTab1() {
        Tab tab1 = actionBar.newTab();
        tab1.setTag("1");
        String tabText = "1";
        tab1.setText(tabText);
        tab1.setTabListener(new TabListener<MyFragment>(TestActivity.this, "1", MyFragment.class));

        actionBar.addTab(tab1);
    }

    private void addTab2() {
        Tab tab1 = actionBar.newTab();
        tab1.setTag("2");
        String tabText = "2";
        tab1.setText(tabText);
        tab1.setTabListener(new TabListener<MyFragment>(TestActivity.this, "2", MyFragment.class));

        actionBar.addTab(tab1);
    }
}

TabListener.java

public class TabListener<T extends SherlockFragment> implements com.actionbarsherlock.app.ActionBar.TabListener {
    private final SherlockFragmentActivity mActivity;
    private final String mTag;
    private final Class<T> mClass;

    public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz) {
        mActivity = activity;
        mTag = tag;
        mClass = clz;
    }

    /* The following are each of the ActionBar.TabListener callbacks */

    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        SherlockFragment preInitializedFragment = (SherlockFragment) mActivity.getSupportFragmentManager().findFragmentByTag(mTag);

        // Check if the fragment is already initialized
        if (preInitializedFragment == null) {
            // If not, instantiate and add it to the activity
            SherlockFragment mFragment = (SherlockFragment) SherlockFragment.instantiate(mActivity, mClass.getName());
            ft.add(android.R.id.content, mFragment, mTag);
        } else {
            ft.attach(preInitializedFragment);
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        SherlockFragment preInitializedFragment = (SherlockFragment) mActivity.getSupportFragmentManager().findFragmentByTag(mTag);

        if (preInitializedFragment != null) {
            // Detach the fragment, because another one is being attached
            ft.detach(preInitializedFragment);
        }
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // User selected the already selected tab. Usually do nothing.
    }
}

MyFragment.java

public class MyFragment extends SherlockFragment {

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

        new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException ex) {
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void result){
                getResources().getString(R.string.app_name);
            }

        }.execute();
    }
}

我已经添加了线程。休眠部分模拟下载数据。onPostExecute中的代码是模拟Fragment的使用。

当我在横向和纵向之间快速旋转屏幕时,我在onPostExecute代码中得到一个异常:

java.lang.IllegalStateException: Fragment MyFragment{410f6060} not 附属于活动

我认为这是因为在此期间已经创建了一个新的MyFragment,并在AsyncTask完成之前附加到活动。onPostExecute中的代码调用一个未附加的MyFragment。

但我该怎么解决呢?


当前回答

你的代码的问题是你使用AsyncTask的方式,因为当你在睡眠线程期间旋转屏幕:

Thread.sleep(2000) 

AsyncTask仍然在工作,这是因为你没有在片段重建(当你旋转)之前正确地取消onDestroy()中的AsyncTask实例,当这个相同的AsyncTask实例(旋转后)运行onPostExecute(),这试图用getResources()与旧的片段实例(无效实例)找到资源:

getResources().getString(R.string.app_name)

这相当于:

MyFragment.this.getResources().getString(R.string.app_name)

因此,最后的解决方案是在你旋转屏幕时片段重建之前管理AsyncTask实例(如果它仍然工作,则取消),如果在过渡期间取消,则在重建后通过布尔标志重新启动AsyncTask:

public class MyFragment extends SherlockFragment {

    private MyAsyncTask myAsyncTask = null;
    private boolean myAsyncTaskIsRunning = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(savedInstanceState!=null) {
            myAsyncTaskIsRunning = savedInstanceState.getBoolean("myAsyncTaskIsRunning");
        }
        if(myAsyncTaskIsRunning) {
            myAsyncTask = new MyAsyncTask();
            myAsyncTask.execute();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean("myAsyncTaskIsRunning",myAsyncTaskIsRunning);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(myAsyncTask!=null) myAsyncTask.cancel(true);
        myAsyncTask = null;

    }

    public class MyAsyncTask extends AsyncTask<Void, Void, Void>() {

        public MyAsyncTask(){}

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            myAsyncTaskIsRunning = true;
        }
        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ex) {}
            return null;
        }

        @Override
        protected void onPostExecute(Void result){
            getResources().getString(R.string.app_name);
            myAsyncTaskIsRunning = false;
            myAsyncTask = null;
        }

    }
}

其他回答

我找到了非常简单的答案:isAdded():

如果片段当前被添加到其活动中,则返回true。

@Override
protected void onPostExecute(Void result){
    if(isAdded()){
        getResources().getString(R.string.app_name);
    }
}

为了避免在Fragment没有附加到Activity时调用onPostExecute,需要在暂停或停止Fragment时取消AsyncTask。那么isAdded()就不再需要了。然而,保持这种检查是明智的。

你的代码的问题是你使用AsyncTask的方式,因为当你在睡眠线程期间旋转屏幕:

Thread.sleep(2000) 

AsyncTask仍然在工作,这是因为你没有在片段重建(当你旋转)之前正确地取消onDestroy()中的AsyncTask实例,当这个相同的AsyncTask实例(旋转后)运行onPostExecute(),这试图用getResources()与旧的片段实例(无效实例)找到资源:

getResources().getString(R.string.app_name)

这相当于:

MyFragment.this.getResources().getString(R.string.app_name)

因此,最后的解决方案是在你旋转屏幕时片段重建之前管理AsyncTask实例(如果它仍然工作,则取消),如果在过渡期间取消,则在重建后通过布尔标志重新启动AsyncTask:

public class MyFragment extends SherlockFragment {

    private MyAsyncTask myAsyncTask = null;
    private boolean myAsyncTaskIsRunning = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(savedInstanceState!=null) {
            myAsyncTaskIsRunning = savedInstanceState.getBoolean("myAsyncTaskIsRunning");
        }
        if(myAsyncTaskIsRunning) {
            myAsyncTask = new MyAsyncTask();
            myAsyncTask.execute();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean("myAsyncTaskIsRunning",myAsyncTaskIsRunning);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(myAsyncTask!=null) myAsyncTask.cancel(true);
        myAsyncTask = null;

    }

    public class MyAsyncTask extends AsyncTask<Void, Void, Void>() {

        public MyAsyncTask(){}

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            myAsyncTaskIsRunning = true;
        }
        @Override
        protected Void doInBackground(Void... params) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException ex) {}
            return null;
        }

        @Override
        protected void onPostExecute(Void result){
            getResources().getString(R.string.app_name);
            myAsyncTaskIsRunning = false;
            myAsyncTask = null;
        }

    }
}

当加载首选项的应用程序设置活动可见时,我遇到了类似的问题。如果我要更改其中一个首选项,然后使显示内容旋转并再次更改首选项,它将崩溃,并提示片段(我的preferences类)没有附加到活动。

当调试时,它看起来像PreferencesFragment的onCreate()方法在显示内容旋转时被调用两次。这已经够奇怪的了。然后我在块外添加了isAdded()检查,它将指示崩溃,它解决了这个问题。

下面是更新首选项摘要以显示新条目的侦听器的代码。它位于我的Preferences类的onCreate()方法,它扩展了PreferenceFragment类:

public static class Preferences extends PreferenceFragment {
    SharedPreferences.OnSharedPreferenceChangeListener listener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // ...
        listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
            @Override
            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
                // check if the fragment has been added to the activity yet (necessary to avoid crashes)
                if (isAdded()) {
                    // for the preferences of type "list" set the summary to be the entry of the selected item
                    if (key.equals(getString(R.string.pref_fileviewer_textsize))) {
                        ListPreference listPref = (ListPreference) findPreference(key);
                        listPref.setSummary("Display file content with a text size of " + listPref.getEntry());
                    } else if (key.equals(getString(R.string.pref_fileviewer_segmentsize))) {
                        ListPreference listPref = (ListPreference) findPreference(key);
                        listPref.setSummary("Show " + listPref.getEntry() + " bytes of a file at once");
                    }
                }
            }
        };
        // ...
    }

我希望这能帮助到其他人!

问题是您正在尝试使用getResources(). getstring()访问资源(在本例中是字符串),它将尝试从Activity获取资源。请看Fragment类的源代码:

 /**
  * Return <code>getActivity().getResources()</code>.
  */
 final public Resources getResources() {
     if (mHost == null) {
         throw new IllegalStateException("Fragment " + this + " not attached to Activity");
     }
     return mHost.getContext().getResources();
 }

mHost是保存Activity的对象。

因为可能没有附加Activity,所以getResources()调用将抛出一个Exception。

恕我直言,公认的解决方案是行不通的,因为你只是在隐藏问题。正确的方法是从其他始终保证存在的地方获取资源,比如应用程序上下文:

youApplicationObject.getResources().getString(...)

在你的片段上添加这个

Activity activity;
@Override
public void onAttach(@NonNull Context context) {
    super.onAttach(context);
    activity = context instanceof Activity ? (Activity) context : null;
}

然后用activity改变getContext()、getActivity()、requireActivity()或requireContext()