简体   繁体   English

Android drawable资源ID冲突?

[英]Android drawable resource id conflict?

I've set up the /android project of ZXing 1.7 as a library referenced by my main android app project. 我已经将ZXing 1.7的/ android项目设置为我的主要Android应用程序项目引用的库。 As a quick test / proof of concept, I have used the CaptureActivity, in the same manner as described here: http://damianflannery.wordpress.com/2011/06/13/integrate-zxing-barcod ... 作为快速测试/概念验证,我使用了CaptureActivity,其方式与此处描述的相同: http//damianflannery.wordpress.com/2011/06/13/integrate-zxing-barcod ...

Mostly, things work well, I can launch the CaptureActivity and receive the scanned data in my activity that launched it. 事实上,事情运作良好,我可以启动CaptureActivity并在启动它的活动中接收扫描数据。 However, I get some really strange behaviour, which I think may be related to resource IDs conflicting between my project and the /android ZXing project. 但是,我得到了一些非常奇怪的行为,我认为这可能与我的项目和/ android ZXing项目之间的资源ID冲突有关。 This will take some explaining so please bear with me... 这需要一些解释所以请耐心等待......

I launch the CaptureActivity with this code: 我使用以下代码启动CaptureActivity:

Button scanBtn = (Button)findViewById(R.id.mainmenu_btn_scan); 
scanBtn.setOnClickListener( 
    new View.OnClickListener() 
{ 
    public void onClick(View v) 
    { 
        Intent i = new Intent("com.google.zxing.client.android.SCAN"); 
        i.putExtra("SCAN_MODE", "QR_CODE_MODE"); 
        startActivityForResult(i, SCAN_CODE); 
    } 
}); 

mainmenu_btn_scan is defined in menu.xml: mainmenu_btn_scan在menu.xml中定义:

<LinearLayout android:id="@+id/mainmenu_view" style="@style/MainMenu" xmlns:android="http://schemas.android.com/apk/res/android">
    ...
    <Button android:id="@+id/mainmenu_btn_scan" 
                    android:drawableTop="@drawable/btn_scan_drawable" 
                    style="@style/MainMenuButton" 
                    android:text="Scan"/>
    ... 
</LinearLayout> 

btn_scan_drawable.xml is in /drawable, and is a drawable selector: btn_scan_drawable.xml在/ drawable中,是一个可绘制的选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/scan_btn_pressed" android:state_focused="true" android:state_pressed="true" /> 
    ...
</selector>

The stange problem is this: when I hold the phone in a lanscape orientation and launch the CaptureActivity, I see the scan_btn_pressed image stretched across the display, and another copy of it also stretched across the "Place a barcode inside the viewfinder rectangle text...". 困扰的问题是:当我以手势方向握住手机并启动CaptureActivity时,我看到scan_btn_pressed图像在显示屏上伸展,另一个副本也延伸到“在取景器矩形文本中放置条形码”。 “。 This only occurs when I launch the activity while holding the phone in landscape orientation. 仅当我以横向方向握住手机时启动活动时才会出现这种情况。

I discovered that if I replace 我发现如果我更换

android:drawableTop="@drawable/btn_scan_drawable" 

with

android:drawableTop="@drawable/scan_btn_pressed"

The problem goes away and everything works perfectly, but I want to get it working with the selector (and understand why the problem occurs). 问题消失了,一切都运行良好,但我想让它与选择器一起工作(并理解问题发生的原因)。 How can a drawable from my activity's UI mysteriously show up in another activity? 如何从我的活动的UI神秘地显示出另一个活动? I'm new to Android development, but I've been googling and working at this issue all day and I can't seem to find out why this would be happening apart from some vague references to resource ID conflicts in the android docs. 我是Android开发的新手,但我一直在谷歌上搜索并整天处理这个问题,我似乎无法找到为什么会发生这种情况,除了对android文档中的资源ID冲突的一些模糊引用。

Note: I am using CaptureActivity as a rapid prototype / proof of concept only. 注意:我只使用CaptureActivity作为快速原型/概念验证。 I will implement my own activity leveraging the ZXIng library for the final app. 我将利用ZXIng库为最终应用程序实现自己的活动。

Ok, it seems like the Android platform has a few wrinkles when using library projects. 好吧,看起来Android平台在使用库项目时有一些皱纹。 I noticed that the R class of my project and the R class of the ZXIng library project were conflicting all over the place eg 我注意到我的项目的R类和ZXIng库项目的R类在各地都存在冲突,例如

public static final class id {
    ...
    public static final int mainmenu_btn_scan=0x7f070028;
    ...

And in the library project's R class: 在图书馆项目的R类中:

public static final class id {
    ...
    public static final int share_app_button=0x7f070028;
    ...

This explains how a UI element from my activity was being picked up by an activity in the library project. 这解释了我的活动中的UI元素是如何被库项目中的活动拾取的。 Android will give priority to resources ids in the main project. Android将优先考虑主项目中的资源ID。

This excellent article gave the solution: http://blog.blackmoonit.com/2010/12/android-sharing-resources-in-eclipse.html 这篇优秀的文章提供了解决方案: http//blog.blackmoonit.com/2010/12/android-sharing-resources-in-eclipse.html

In CaptureActivity (the activity I was calling in the library), I replaced 在CaptureActivity(我在库中调用的活动)中,我替换了

viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);

With this 有了这个

viewfinderView = (ViewfinderView) findViewById(
    getResources().getIdentifier("viewfinder_view", "id", getPackageName()) );

This fixed the issue, although I'll need to go through and replace all direct id references with getIndentifier() to ensure there aren't any other conflicts. 这解决了这个问题,虽然我需要通过getIndentifier()替换所有直接id引用,以确保没有任何其他冲突。 Not an ideal solution. 不是理想的解决方案。 It feels like there ought to be some fairly straightforward extension to the aapt tool to ensure that the ids between R classes don't conflict. 感觉应该对aapt工具进行一些相当直接的扩展,以确保R类之间的ID不会发生冲突。

See here for more info: http://devmaze.wordpress.com/2011/05/22/android-application-android-libraries-and-jar-libraries/ 有关详细信息,请参阅此处: http//devmaze.wordpress.com/2011/05/22/android-application-android-libraries-and-jar-libraries/

使用project->clean..重新生成R

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM