Java Question sdk drawable objects lose their reference...

DevBaby

Active Member
Licensed User
Longtime User
Facebook gives source files that you have to import into eclipse, when you import these files, eclipse will automatically generate a R.java class that contains static integer identifiers for "drawable" objects that are later referenced in code...

Drawable crossDrawable = getContext().getResources().getDrawable(R.drawable.com_facebook_close);

The R.Drawable.com_facebook_close returns an integer value reference for png files included in the SDk.

The problem is once I export this sdk and reference in b4a, the r.drawable class becomes obsolete because the reference pointers get lost when I build my b4a project with its own res/drawable folder and objects.

I put the png files in my b4a project, but the Drawable crossDrawable = getContext().getResources().getDrawable(R.drawable.com_facebook_close); will return a reference integer that no longer maps to my b4a resoucre items. I then get a java.lang.NoClassDefFoundError: com.facebook.android.R$drawable error.

I imagined that eclipse devs do not run into this because because a new r class is rebuilt when they compile their final project.
 

warwound

Expert
Licensed User
Longtime User
Hi again!

Take a look at this method from my AndroidResources library:

B4X:
/**
    * Get a application resource Drawable by ResourceName.
    *Returns Null if the Drawable is not found.
    */
   public static Drawable GetApplicationDrawable(String ResourceName) {
      int resourceId = BA.applicationContext.getResources().getIdentifier(ResourceName, "drawable", BA.packageName);
      if (resourceId == 0) {
         return null;
      } else {
         return BA.applicationContext.getResources().getDrawable(resourceId);
      }
   }

You could add that method to the Facebook SDK class and then replace:

B4X:
Drawable crossDrawable = getContext().getResources().getDrawable(R.drawable.com_facebook_close);

With:

B4X:
Drawable crossDrawable = GetApplicationDrawable("com_facebook_close");

That should work as long as your B4A project contains com_facebook_close.png in it's Objects\res\drawable folder.

Or if you don't want to add the new method then:

B4X:
Drawable crossDrawable = BA.applicationContext.getResources().getDrawable(BA.applicationContext.getResources().getIdentifier("com_facebook_close", "drawable", BA.packageName));

Martin.
 
Last edited:

DevBaby

Active Member
Licensed User
Longtime User
Thanks so much for your help,

Prior to your response, I did try

B4X:
Drawable crossDrawable = getContext().getResources().getDrawable( getContext().getResources().getIdentifier(“close”, “drawable”, getContext().getPackageName()) );

which is similar to the last option you gave wiithout the BA reference you cited, which is probably why it did not work.

I put in a cheat workaround because an older version of the sdk required only one png file to implement. I loaded the close.png in my B4A code, wrapped it in a drawable object and passed the object to the sdk in the method call from B4A.

The new version of the sdk has many png files to load, so your code will help me going forward. Thanks again.
 

warwound

Expert
Licensed User
Longtime User
I just noticed an error in my last post and edited it.
That last code example should have been:

Or if you don't want to add the new method then:

B4X:
Drawable crossDrawable = BA.applicationContext.getResources().getDrawable(BA.applicationContext.getResources().getIdentifier("com_facebook_close", "drawable", BA.packageName));

Martin.
 
Top