无法从视图中设置ViewPager

时间:2016-03-27 18:27:32

标签: android android-fragments android-viewpager fragmentmanager

我需要完成自定义锁屏应用程序。有一个没有ViewPager的工作版本(只有一页)。

我能够通过将根视图直接添加到winManager来设置用户无法关闭的全屏窗口。

我有一个带ViewPager的其他版本(2张幻灯片)。它在一个正常的应用程序案例中工作,但是当我尝试在我添加到winManager的根视图(wrapperView)中创建它时(就像在其他版本中一样),应用程序崩溃了。不同之处在于onCreate方法。

ScreenLock2.onCreate:

...

/*Working version*/

    setContentView(R.layout.screen_lock_fragment)
    ViewPager mPager = (ViewPager) findViewById(R.id.pager);

/*Working version end*/


/*Not working version*/

    wrapperView = new RelativeLayout(getBaseContext());
    View.inflate(this, R.layout.screen_lock_fragment, wrapperView);
    WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
    winManager = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE);
    winManager.addView(wrapperView, localLayoutParams);

    ViewPager mPager = (ViewPager) wrapperView.findViewById(R.id.pager);

/*Not working version end*/

ScreenSlidePagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);

...

ScreenLock2.ScreenSlidePagerAdapter类:

public class ScreenSlidePagerAdapter extends FragmentPagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return ScreenLockFragment.newInstance(position);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        return super.instantiateItem(mPager, position);
    }

    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public int getCount() {
        return 2;
    }
}

ScreenLockFragment:

public class ScreenLockFragment extends Fragment {
    int page;

    public static ScreenLockFragment newInstance(int page) {
        ScreenLockFragment fragmentFirst = new ScreenLockFragment();
        Bundle args = new Bundle();
        args.putInt("pageInt", page);
        fragmentFirst.setArguments(args);
        return fragmentFirst;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        page = getArguments().getInt("pageInt", 1);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ScreenLock2 screenLock2 = (ScreenLock2) getActivity();
        container = screenLock2.mPager;
        switch (page) {
            case 0:
                rootView = (ViewGroup) inflater.inflate(R.layout.screen_lock, container, false);
                break;
            case 1:
                rootView = (ViewGroup) inflater.inflate(R.layout.screen_lock_main, container, false);
                break;
        }
        return rootView;
    }   
}

错误消息如下:

java.lang.IllegalArgumentException: No view found for id 0x7f0e00fc (com.dynauth.login:id/pager) for fragment ScreenLockFragment{172ec3e #0 id=0x7f0e00fc android:switcher:2131624188:1}
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。我将FragmentPagerAdapter替换为标准的PagerAdapter。