程式包com.android.internal.R不存在

weixin_34007291發表於2017-03-07

解決辦法:反射

        int[] textAppearanceStyleArr = new int[0];
        int titleStyle = 0;
        int iconStyle = 0;
        try {
            Class clasz = Class.forName("com.android.internal.R$styleable");
            Field field = clasz.getDeclaredField("MenuItem");
            field.setAccessible(true);
            textAppearanceStyleArr = (int[])field.get(null);

            field = clasz.getDeclaredField("MenuItem_title");
            field.setAccessible(true);
            titleStyle = (Integer)field.get(null);

            field = clasz.getDeclaredField("MenuItem_icon");
            field.setAccessible(true);
            iconStyle = (Integer)field.get(null);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }


        TypedArray a = mActivity.obtainStyledAttributes(attrs,
                textAppearanceStyleArr);

        String itemTitle = (String) a.getText(titleStyle);
        int itemIconResId = a.getResourceId(iconStyle, 0);
        Logger.e("qiaoevent", "BaseActionBar -> readItem: itemTitle:" + itemTitle);
        Logger.e("qiaoevent", "BaseActionBar -> readItem: itemIconResId:" + itemIconResId);
        a.recycle();

歡迎關注【Funny新青年】微信公眾號~

相關文章