【问题标题】:Instant run does not work with layout-21即时运行不适用于 layout-21
【发布时间】:2016-09-01 19:40:47
【问题描述】:

这是一个在 AS 中即时运行不起作用的情况:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    toolbar.setElevation(visible ? getResources().getDimension(R.dimen.elevation_toolbar) : 0);
} else {
    View toolbarShadow = findViewById(R.id.toolbar_shadow);
    toolbarShadow.setVisibility(visible ? View.VISIBLE : View.GONE);
}

layout 中的视图具有R.id.toolbar_shadow。都很好。

但是,如果您在 layout-21 中有一个不包含 R.id.toolbar_shadow 的视图,那么当您将应用程序编译为例如Api 23 设备 gradle 将失败:

Error:(1046, 42) error: cannot find symbol variable toolbar_shadow

有解决这个问题的想法吗?


更新:按要求布局:

res/layout/actionbar.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    ... >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        ... />

    <View
        android:id="@+id/toolbar_shadow"
        ... />

</LinearLayout>

res/layout-21/actionbar.xml:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
            ... />

【问题讨论】:

  • 你能显示布局文件吗?至少是相关部分。
  • 它可以正确构建吗?没有使用 Instant Run?
  • 是的:没有即时运行很好。即时运行失败并出现上述错误。

标签: android android-studio android-gradle-plugin android-studio-2.0 android-instant-run


【解决方案1】:

好的发现解决方案手动添加了资源中缺少的id:

<!--To make Instant Run work if these ids are not in 21+-->
<item name="toolbar_shadow" type="id"/>

【讨论】:

  • 这很奇怪,可能提交错误报告?
猜你喜欢
  • 2016-10-01
  • 1970-01-01
  • 2019-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-09
  • 2016-06-11
相关资源
最近更新 更多