【问题标题】:findViewById(android.R.id.statusBarBackground) and findViewById(android.R.id.navigationBarBackground) return null?findViewById(android.R.id.statusBarBackground) 和 findViewById(android.R.id.navigationBarBackground) 返回 null?
【发布时间】:2018-03-28 11:47:51
【问题描述】:

我试过NougatOreo。这两款手机都是安卓系统,两个视图都可以在屏幕上看到。我的sharedElementTransition 需要这些视图。这个问题在here 之前被问过,但没有答案。这发生在很多人身上,他们在 Alex Lockwood 的回答中讨论了here,但没有明确的解决方案。

我从我的活动中拨打 findViewById() 并尝试了两种方法

findViewById(android.R.id.statusBarBackground);

getWindow().getDecorView().findViewById(android.R.id.statusBarBackground);

我在setContentView() 之后调用了findViewById()

Possibly related,但与 progressBar 不同,statusBarnavigationBar 视图几乎一直存在。

代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        getWindow().setSharedElementEnterTransition(getTransitionSet());
        getWindow().setSharedElementExitTransition(getTransitionSet());
        getWindow().setSharedElementReturnTransition(getTransitionSet());
        getWindow().setSharedElementReenterTransition(getTransitionSet());
        ActivityCompat.postponeEnterTransition(this);
        setEnterSharedElementCallback(mCallback);
    }
    setContentView(R.layout.summary_image_preview);
    navigationBar = getWindow().getDecorView().findViewById(android.R.id.navigationBarBackground);
    statusBar = getWindow().getDecorView().findViewById(android.R.id.statusBarBackground);
    mToolbar = (Toolbar) findViewById(R.id.toolBar);
    // etc...

}

Toolbar 是我的内容视图中的普通视图,它工作正常。但是statusBarnavigationBar are null

【问题讨论】:

  • 能否请您发布更多代码,以便我们分析并尝试定位错误?不看代码很难告诉你问题出在哪里。
  • 已更新代码。除了我所说的之外,它并没有什么太多。

标签: android view statusbar navigationbar findviewbyid


【解决方案1】:

ViewsonCreate() 函数中返回 null。我在FragmentonViewCreated() 中检查了相同的Views,它们不为空。我通过将findViewById() 延迟到我需要它的时候解决了这个问题。

出于某种原因,虽然onCreate() 中提供了其他视图,但您的应用程序外部的视图只有在创建Fragment View 之后才能访问

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 2016-09-18
    相关资源
    最近更新 更多