使用 setNavigationBarDividerColor() 时出现 NoSuchMethodError?

NoSuchMethodError when using setNavigationBarDividerColor()?

当导航栏和我的应用程序内容颜色相同时,我试图在它们之间创建一个分隔线,Google 在他们最新的 Material Design 2.0 更新中就是这样做的到他们的应用程序,例如 Google 联系人和 Google 主页。我试着这样做:

getWindow().setNavigationBarDividerColor(Color.GRAY);

但是,我在尝试时得到 NoSuchMethodError

我正在尝试找到解决此问题的方法或另一种在导航栏上方放置分隔线的方法。

这是堆栈:

10-16 20:33:47.257 30336-30336/? E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.diamonddevelopment.scholarly, PID: 30336
        java.lang.NoSuchMethodError: No virtual method setNavigationBarDividerColor(I)V in class Landroid/view/Window; or its super classes (declaration of 'android.view.Window' appears in /system/framework/framework.jar!classes2.dex)
            at com.diamonddevelopment.scholarly.base.BaseActivity.styleNavBar(BaseActivity.java:101)
            at com.diamonddevelopment.scholarly.base.BaseActivity.setToolbars(BaseActivity.java:78)
            at com.diamonddevelopment.scholarly.fragments.TimetableFragment.onCreateView(TimetableFragment.java:79)
            at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
            at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
            at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
            at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
            at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
            at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
            at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
            at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
            at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
            at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
            at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
            at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
            at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:620)
            at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
            at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1413)
            at android.app.Activity.performStart(Activity.java:7061)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2874)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2989)
            at android.app.ActivityThread.-wrap11(Unknown Source:0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1688)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6753)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:482)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

setNavigationBarDividerColor()

  • 在导航栏和应用内容之间显示一条指定颜色的细线。

setNavigationBarDividerColor() 方法已 添加到 API 级别 28 中,因此它仅适用于 API 级别 28 及更高级别

您不能使用 setNavigationBarDividerColor() 低于 API 级别 28

的方法

当您使用 setNavigationBarDividerColor() 时,您将在屏幕截图下方收到警告检查

示例代码

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
     getWindow().setNavigationBarDividerColor(Color.GRAY)
  };