R.java 有 2 个同名布局的成员

R.java having 2 members with same name layout

我可能问了一个蹩脚的问题,但我是 android 的新手,我找不到解决这个问题的方法。

当我在计算机中打开 R.java 文件以了解 setContentView(R.layout.activity_main) 的工作原理时。

所以我打开了我的 R.java 文件并简单地搜索了键 layout 所以我在我的 R.java 文件中找到了 2 个条目所以我很困惑编译器如何识别我当前正在尝试引用的 layout 成员。我正在放置 R.java 文件的片段,其中包含声明 layout 成员的行。

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.myapp;

public final class R {
    public static final int layout=0x7f0100a9;
        /** <p>Must be a reference to another resource, in the form "<code>@[+][<i>package</i>:]<i>type</i>:<i>name</i></code>"
or to a theme attribute in the form "<code>?[<i>package</i>:][<i>type</i>:]<i>name</i></code>".
         */

     public static final class layout {
        public static final int abc_action_bar_title_item=0x7f040000;
        public static final int abc_action_bar_up_container=0x7f040001;
        public static final int abc_action_bar_view_list_nav_layout=0x7f040002;
        public static final int abc_action_menu_item_layout=0x7f040003;
        public static final int abc_action_menu_layout=0x7f040004;
        public static final int abc_action_mode_bar=0x7f040005;
        public static final int abc_action_mode_close_item_material=0x7f040006;
        public static final int abc_activity_chooser_view=0x7f040007;
        public static final int abc_activity_chooser_view_list_item=0x7f040008;
        public static final int abc_alert_dialog_button_bar_material=0x7f040009;
        public static final int abc_alert_dialog_material=0x7f04000a;
        public static final int abc_dialog_title_material=0x7f04000b;
        public static final int abc_expanded_menu_layout=0x7f04000c;
        public static final int abc_list_menu_item_checkbox=0x7f04000d;
        public static final int abc_list_menu_item_icon=0x7f04000e;
        public static final int abc_list_menu_item_layout=0x7f04000f;
        public static final int abc_list_menu_item_radio=0x7f040010;
        public static final int abc_popup_menu_item_layout=0x7f040011;
        public static final int abc_screen_content_include=0x7f040012;
        public static final int abc_screen_simple=0x7f040013;
        public static final int abc_screen_simple_overlay_action_mode=0x7f040014;
        public static final int abc_screen_toolbar=0x7f040015;
        public static final int abc_search_dropdown_item_icons_2line=0x7f040016;
        public static final int abc_search_view=0x7f040017;
        public static final int abc_select_dialog_material=0x7f040018;
        public static final int activity_main=0x7f040019;
        public static final int notification_media_action=0x7f04001a;
        public static final int notification_media_cancel_action=0x7f04001b;
        public static final int notification_template_big_media=0x7f04001c;
        public static final int notification_template_big_media_narrow=0x7f04001d;
        public static final int notification_template_lines=0x7f04001e;
        public static final int notification_template_media=0x7f04001f;
        public static final int notification_template_part_chronometer=0x7f040020;
        public static final int notification_template_part_time=0x7f040021;
        public static final int select_dialog_item_material=0x7f040022;
        public static final int select_dialog_multichoice_material=0x7f040023;
        public static final int select_dialog_singlechoice_material=0x7f040024;
        public static final int support_simple_spinner_dropdown_item=0x7f040025;
    }
}

我想知道编译器如何在编写 setContentView(R.layout.acivity_main) 时解析实际的 layout 元素,因为有一个 int 变量和一个可用的同名内部 class layout.

一个是class,一个是int:从上下文看没有歧义。

当两种解释都可能时,它决定使用变量(而不是类型):

public class Test {
    public static void main(String... args) {
        {
            Value x = new Value();
            // use the variable x
            System.out.println(x.i);
            // System.out.println(x.j); // does not compile
        }
        // use the type x
        System.out.println(x.i);
    }
}

class x {
    static int i = 42;
    static int j = 1;
}
class Value {
    int i = 100;
}

输出为:

100
42

编辑:这不是你的问题。

你说的对,会有歧义:

public class Test {
    public static void main(String... args) {
        // does not compile
        System.out.println(R.layout.x);
    }
}

class R {
    static final class layout {
        static int x = 42;
    };
    static final int layout = 25;
}

其实有none是因为在生成的R.java中,int layout在一个内部类中:

public final class R {
    public static final class attr {
        public static final int layout = …;
    }
    public static final class layout {
        //…
    }
}