IllegalStateException:在onSaveInstanceState之后 - 屏幕旋转后无法执行此操作

时间:2014-07-25 15:42:47

标签: android illegalstateexception

我也是在精确的背景下得到它,这里给出的解决方案(IllegalStateException: Can not perform this action after onSaveInstanceState with ViewPager)不起作用。

这是代码:应该是一个用于测试的工作代码;我希望。

MainActivity.java

public class MainActivity extends FragmentActivity {
  final static int INIT_NETWORK_DONE = 1;
  final static int EXIT_APPLICATION = -1;

  private Site site = new Site(this);
  private WifiManager wifi = null;
  Handler mHandler = null;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    site.setUrls();

    if (savedInstanceState == null) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        fragmentTransaction.replace(R.id.frame_container, new Fragment_init(site)).commit();

    }
  }
   . . .
  @Override
  public void onSaveInstanceState(Bundle saveInstanceState) {

       //super.onSaveInstanceState(saveInstanceState);
  }
}

Fragment_init.java

public class Fragment_init extends Fragment {
    Fragment fragment = null;
    private InitTask mInitTask = null;
    // Taille maximale du téléchargement
    public final static int MAX_SIZE = 100;
    // Identifiant de la boîte de dialogue
    public final static int ID_DIALOG = 0;
    public final static int DO_INIT_WIFI = 1;

    private Site site = null;


    public Fragment_init() {
    }    

    public Fragment_init(Site _site) {
        site = _site;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_init, container, false);

        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        if (savedInstanceState == null) {
            Animation animation = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.animator.welcome_anim);
            ImageView logoSite = (ImageView)getActivity().findViewById(R.id.imageAvenArmand);
            logoSite.startAnimation(animation);

                // Do the init
                mInitTask = new InitTask(Fragment_init.this, site, getFragmentManager());
                // On l'exécute
                mInitTask.execute(0);
        }
    }

    // L'AsyncTask est bien une classe interne statique
    static class InitTask extends AsyncTask<Integer, Integer, Integer> {
        // Référence faible à l'activité
        private Fragment_init mActivity = null;
        private Site site = null;
        Context context = null;
        private FragmentManager fragmentManager = null;

        public InitTask (Fragment_init pActivity, Site pSite, FragmentManager _fragmentManager) {
            mActivity = pActivity;
            context = mActivity.getActivity();
            site = pSite;
            fragmentManager = _fragmentManager;
          }

        @Override
        protected void onPreExecute () {
        }

        @Override
        protected void onPostExecute (Integer result) {
            if(result != 1) {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(mActivity.getActivity());
                alertDialog.setTitle(R.string.label_titleAlertInit);
            } else {
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                fragmentTransaction.replace(R.id.frame_container, new Fragment_selectLanguage(site)).commitAllowingStateLoss();
            }
        }

        @Override
        protected Integer doInBackground (Integer... arg0) {
            URL url = null;
            BufferedInputStream buf;
            ArrayList<Language> languages = null;

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {

            }
            return 1;
        }

        @Override
        protected void onProgressUpdate (Integer... prog) {
        }

        @Override
        protected void onCancelled () {
        }

        private int processStream(InputStream inputStream) {
            // Création du parser XML
            XmlPullParserFactory factory;
            int lineNumber = 0;

            return (1);

        }
    }

    @Override
    public void onSaveInstanceState(Bundle saveInstanceState) {
        //super.onSaveInstanceState(saveInstanceState);

    }
}

activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:screenOrientation="portrait"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.cabbonline.ndguidelt.MainActivity" >
</FrameLayout>

fragment_init.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragmentInit"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context="com.cabbonline.ndguidelt.MainActivity" >

   <ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="15dp"
    android:src="@drawable/launcher_icon" />

</RelativeLayout>

无论如何,我认为不调用super.onSaveInstanceState()会导致上下文保存没有问题吗?

所以如果你在图像淡出时旋转屏幕,你应该在commit()上调用IllegalStateException

所以我的解决方法是在此过渡屏幕中阻止屏幕旋转。好吧,对我来说没问题,但我怀疑它可能是你们大多数人的答案。无论如何,它可以帮助。

所以我在fragment_init()中的onCreateView()中调用它。

    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

然后我在下一个片段中的onCreateView()中调用它:

    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

: - /

还有其他想法吗?

3 个答案:

答案 0 :(得分:0)

使用commitAllowingStateLoss()而不是commit()

if (savedInstanceState == null) {
    FragmentTransaction fragmentTransaction =getSupportFragmentManager().beginTransaction();
    fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    fragmentTransaction.replace(R.id.frame_container, new Fragment_init(site)).commitAllowingStateLoss();

} 

答案 1 :(得分:0)

答案 2 :(得分:0)

所以我使用这里解释的wonderfull消息处理程序实现解决了我的问题: How to handle Handler messages when activity/fragment is paused

向Akagami指向我的帖子。

此致