Duplicate id @+id/image in appcompat_v7 abc_activi

2019-06-18 04:44发布

问题:

While creating the apk I got the following error :

"Duplicate id @+id/image, already defined earlier in this layout .... abc_activity_chooser_view.xml:58 in layout (appcompat_v7)"

So I cleaned it, still the same. There is indeed duplicate id @+id/image in this file :

<?xml version="1.0" encoding="utf-8"?>
<view xmlns:android="http://schemas.android.com/apk/res/android"
class="android.support.v7.internal.widget.ActivityChooserView$InnerLayout"
android:id="@+id/activity_chooser_view_content"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
style="?attr/activityChooserViewStyle">

<FrameLayout
    android:id="@+id/expand_activities_button"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:focusable="true"
    android:addStatesFromChildren="true"
    android:background="?attr/actionBarItemBackground">

    <ImageView android:id="@+id/image"
        android:layout_width="32dip"
        android:layout_height="32dip"
        android:layout_gravity="center"
        android:layout_marginTop="2dip"
        android:layout_marginBottom="2dip"
        android:layout_marginLeft="12dip"
        android:layout_marginRight="12dip"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true" />

</FrameLayout>

<FrameLayout
    android:id="@+id/default_activity_button"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:focusable="true"
    android:addStatesFromChildren="true"
    android:background="?attr/actionBarItemBackground">

    <ImageView android:id="@+id/image"
        android:layout_width="32dip"
        android:layout_height="32dip"
        android:layout_gravity="center"
        android:layout_marginTop="2dip"
        android:layout_marginBottom="2dip"
        android:layout_marginLeft="12dip"
        android:layout_marginRight="12dip"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true" />

  </FrameLayout>

</view>

Any ideas of how to deal with this ? Obviously I can't just rename it. I can skip checking it in Lint, but the error still persists and I don't think this is the best solution. Any more reliable solutions ?

回答1:

You are getting a duplicate ID error because there is a view with the same name in android.support.v7.internal.widget.ActivityChooserView$InnerLayout. The simple answer is to use a new name for your ImageView and update the name in your code anywhere you reference it.

May I ask why you are using an internal framework widget directly in your layout? Internal layouts are not meant to be used directly and are subject to change at any time, which may break your code in the future. The better solution is to create your own layout or use a public framework widget with similar functionality. Use of an internal widget is done at your own risk.