找不到用于片段LockscreenDefaultFragment的id 0x7f0f0085(lockscreen_viewpager)的视图{b73c8bb#0 id = 0x7f0f0085}

时间:2016-06-02 10:33:43

标签: android android-viewpager android-adapter android-windowmanager

每当我尝试将活动(包括viewpager)添加到windowManager中时,它都会崩溃并出现上述错误。我为viewpager尝试了setId这样的解决方案,并创建了自定义viewpager来懒惰调用setAdapter但不幸的是它仍然崩溃。从我怀疑是在没有调用setContentview时导致此问题的适配器,但我不知道如何以及为什么。任何帮助,将不胜感激。

编辑1 :如果我们没有使用setContentView,那么viewpager适配器如何在其中找到视图?

LockScreenActivity

  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    lockView = View.inflate(this, R.layout.activity_lockscreen, null);

    mViewPager = (ViewPager) lockView.findViewById(R.id.lockscreen_viewpager);
    mPageAdapter = new LockscreenAdapter(getSupportFragmentManager());
    mViewPager.setPageTransformer(true, new ZoomOutPageTransformer(mViewPager));
    mViewPager.setAdapter(mPageAdapter);
    mViewPager.setCurrentItem(1);
    mViewPager.addOnPageChangeListener(this);

    mLockUtil = LockLayer.getInstance(this);
    mLockUtil.setLockView(lockView);
    mLockUtil.lock();


    ImageView wallpaper = (ImageView) lockView.findViewById(R.id.activity_lockscreen_wallpaper);
    final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
    BitmapDrawable wallpaperDrawable = ((BitmapDrawable) wallpaperManager.getDrawable());
    if (wallpaperDrawable != null) {
        try {
            wallpaper.setImageBitmap(wallpaperDrawable.getBitmap());
        } catch (IOError ignored) {
        }
    }
}

LockscreenAdapter扩展了FragmentStatePagerAdapter

 private List<Fragment> mItems = new ArrayList<>();

public LockscreenAdapter(FragmentManager fm) {
    super(fm);
    populate();
}

private void populate() {
    mItems.clear();
    mItems.add(LockscreenDefaultFragment.newInstance(LockscreenFragment.PAGE_DEFAULT));
    mItems.add(LockscreenFragment.newInstance(LockscreenFragment.PAGE_LOCK));
    mItems.add(LockscreenDefaultFragment.newInstance(LockscreenFragment.PAGE_DEFAULT));
}

@Override
public Fragment getItem(int position) {
    return mItems.get(position);
}

@Override
public int getCount() {
    return mItems.size();
}

LockLayer

public static synchronized LockLayer getInstance(Activity act){
    if(mLockLayer == null){
        mLockLayer = new LockLayer(act);
    }
    return mLockLayer;
}

private LockLayer(Activity act) {
    mActivty = act;
    init();
}

private void init(){
    isLocked = false;
    mWindowManager = mActivty.getWindowManager();
    mLockViewLayoutParams = new LayoutParams();
    mLockViewLayoutParams.width = LayoutParams.MATCH_PARENT;
    mLockViewLayoutParams.height = LayoutParams.MATCH_PARENT;
    mLockViewLayoutParams.type = LayoutParams.TYPE_SYSTEM_ERROR;
}
public synchronized void lock() {
    if(mLockView!=null && !isLocked){
        mWindowManager.addView(mLockView, mLockViewLayoutParams);
    }
    isLocked = true;
}
public synchronized void unlock() {
    if(mWindowManager!=null && isLocked){
        mWindowManager.removeView(mLockView);
    }
    isLocked = false;
}
public synchronized void setLockView(View v){
    mLockView = v;
}

1 个答案:

答案 0 :(得分:0)

尝试了多种方法和测试之后,将viewpager插入windowmanager,对我来说唯一的方法是将FragmentStatePagerAdapter更改为PagerAdapter,每当使用FragmentStatePagerAdapter时,它似乎总是会搜索并从根视图中获取片段,最终会导致崩溃。