gpt4 book ai didi

android - 无法从 View 设置 ViewPager

转载 作者:行者123 更新时间:2023-11-30 01:27:33 25 4
gpt4 key购买 nike

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

通过将 Root View 直接添加到 winManager,我能够设置用户无法关闭的全屏窗口。

我有另一个带有 ViewPager 的版本(2 张幻灯片)。它在正常的应用程序案例中工作,但是当我尝试在我添加到 winManager 的 Root View (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;
}
}

屏幕锁 fragment :

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)

最佳答案

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

关于android - 无法从 View 设置 ViewPager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36250993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com