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. 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...
Mostly, things work well, I can launch the CaptureActivity and receive the scanned data in my activity that launched it. However, I get some really strange behaviour, which I think may be related to resource IDs开发者_JAVA技巧 conflicting between my project and the /android ZXing project. This will take some explaining so please bear with me...
I launch the CaptureActivity with this code:
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:
<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:
<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...". 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? 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.
Note: I am using CaptureActivity as a rapid prototype / proof of concept only. I will implement my own activity leveraging the ZXIng library for the final app.
Ok, it seems like the Android platform has a few wrinkles when using library projects. I noticed that the R class of my project and the R class of the ZXIng library project were conflicting all over the place e.g.
public static final class id {
...
public static final int mainmenu_btn_scan=0x7f070028;
...
And in the library project's R class:
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. Android will give priority to resources ids in the main project.
This excellent article gave the solution: http://blog.blackmoonit.com/2010/12/android-sharing-resources-in-eclipse.html
In CaptureActivity (the activity I was calling in the library), I replaced
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. 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.
See here for more info: http://devmaze.wordpress.com/2011/05/22/android-application-android-libraries-and-jar-libraries/
use project->clean..
to regenerate R
Class
精彩评论