总是得到“java.lang.IllegalStateException:递归进入executePendingTransactions”

时间:2015-10-26 15:21:02

标签: java android exception android-viewpager

在我的MainActivity中,我使用FragmentPagerAdapter的实例来创建两个片段。但我总是得到类似的东西:

 java.lang.IllegalStateException: Recursive entry to executePendingTransactions
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1544)
            at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1756)
            at android.support.v4.app.FragmentController.saveAllState(FragmentController.java:125)
            at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:487)
            at android.app.Activity.performSaveInstanceState(Activity.java:1182)
            at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1230)
            at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3019)
            at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3078)
            at android.app.ActivityThread.access$1000(ActivityThread.java:143)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1263)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4960)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)

这是我的代码:

public class MainActivity extends AppCompatActivity
    implements ManagerCallback,ViewPager.PageTransformer{
private MediaManager mediaManagers[]=new MediaManager[2];
private ViewPager mainPager;
private ArrayList<String> mediaData;
private static final float MIN_SCALE = 0.85f;
private static final float MIN_ALPHA = 0.5f;
private static final int IMAGE_RECREATE_REQUEST=1;
private static final int AUDIO_RECREATE_REQUEST=2;
private FragmentRefreshListener[] fragmentRefreshListeners;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mainPager=(ViewPager)findViewById(R.id.main_pager);
    updateAdapter();
    setFabButton();
    mainPager.setPageTransformer(false, this);
}
private void updateAdapter(){
    PagerAdapter pagerAdapter=new MyFragmentPagerAdapter(getSupportFragmentManager());
    mainPager.setAdapter(pagerAdapter);
    fragmentRefreshListeners=new FragmentRefreshListener[2];
}
private void setFabButton(){
    setSupportActionBar((Toolbar) findViewById(R.id.main_toolbar));
    final FloatingActionButton floatingActionButton=(FloatingActionButton)
            findViewById(R.id.floating_main);
    floatingActionButton.setImageDrawable(getResources().getDrawable(R.drawable.floating_but));
    float alpha=floatingActionButton.getAlpha();
    floatingActionButton.animate().alpha(0);
    floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = mainPager.getCurrentItem();
            switch (position % 2) {
                case 0: {
                    if (mediaManagers[mainPager.getCurrentItem()] != null) {
                        mediaData = ((ImageManager) mediaManagers[0]).getMediaFiles();
                        Intent intent = new Intent(MainActivity.this, ImageSelecter.class);
                        Toast.makeText(getApplicationContext(), "Click", Toast.LENGTH_SHORT).show();
                        intent.putExtra("pos", -1);
                        intent.putStringArrayListExtra("images", mediaData);
                        startActivityForResult(intent,IMAGE_RECREATE_REQUEST);
                        break;
                    }
                }
                case 1: {
                    if (mediaManagers[mainPager.getCurrentItem()] != null) {
                        mediaData = ((AudioManager) mediaManagers[1]).getMediaFiles();
                        Toast.makeText(getApplicationContext(),Integer.toString(mediaData.size()),Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(MainActivity.this, AudioList.class);
                        intent.putStringArrayListExtra("audioNames", mediaData);
                        intent.putExtra("position", mediaManagers[mainPager
                                .getCurrentItem()].directoriesNumber());
                        startActivityForResult(intent,AUDIO_RECREATE_REQUEST);
                    }
                    break;
                }
            }
        }
    });
    floatingActionButton.animate().alpha(alpha);
}
@Override
public void onActivityResult(int requestCode, int  resultCode,Intent data){
    if(requestCode==IMAGE_RECREATE_REQUEST){
        if(resultCode==RESULT_OK)
            fragmentRefreshListeners[0].onRefresh();
    }
    else if(requestCode==AUDIO_RECREATE_REQUEST){
        if(resultCode==RESULT_OK)
            fragmentRefreshListeners[1].onRefresh();
    }
}
public void transformPage(View view, float position) {
    int pageWidth = view.getWidth();
    int pageHeight = view.getHeight();

    if (position < -1) {
        view.setAlpha(0);

    } else if (position <= 1) {
        float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
        float verticalMargin = pageHeight* (1 - scaleFactor) / 2;
        float horizontalMargin = pageWidth * (1 - scaleFactor) / 2;
        if (position < 0) {
            view.setTranslationX(horizontalMargin - verticalMargin / 2);
        } else {
            view.setTranslationX(-horizontalMargin + verticalMargin / 2);
        }

        view.setScaleX(scaleFactor);
        view.setScaleY(scaleFactor);

        view.setAlpha(MIN_ALPHA +
                (scaleFactor - MIN_SCALE) /
                        (1 - MIN_SCALE));

    } else {
        view.setAlpha(0);
    }
}
private class MyFragmentPagerAdapter extends FragmentPagerAdapter{

    public MyFragmentPagerAdapter(android.support.v4.app.FragmentManager fm) {
        super(fm);
    }
    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        switch (position)
        {
            case 0:
            {
                ImageFragment imageFragment=ImageFragment.getInstance(MainActivity.this);
                fragmentRefreshListeners[0]=imageFragment;
                return imageFragment;
            }
            case 1:
            {
                AudioFragment audioFragment=AudioFragment.getInstance(MainActivity.this);
                fragmentRefreshListeners[1]=audioFragment;
                return audioFragment;
            }
        }
        Toast.makeText(MainActivity.this,"Error",Toast.LENGTH_SHORT).show();
        return null;
    }
    @Override
    public int getCount() {
        return 2;
    }
    public void  destroyItem(ViewGroup container, int position, Object object){

    }

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
@Override
public void setManager(MediaManager manager, int viewPosition){
    mediaManagers[viewPosition]=manager;
}

}

0 个答案:

没有答案