Java Question Loading a Layout and Inflating it in Java

walterf25

Expert
Licensed User
Longtime User
Hello Java experts, i'm trying to wrap a simple AudioPlayer found here.
The problem i'm having is that i don't understand how to load a xml layout that is found in the res folder, in one of the examples they are loading the buttons, seekbar, etc... layout like this:

B4X:
mPlayMedia = findViewById(R.id.play);
mPauseMedia = findViewById(R.id.pause);
mMediaSeekBar = (SeekBar) findViewById(R.id.media_seekbar);
mRunTime = (TextView) findViewById(R.id.run_time);
mTotalTime = (TextView) findViewById(R.id.total_time);

My Question is if I use the #AdditionalResources in B4A how can i find and load this layouts?

This is the code i have so far in Java:

B4X:
public class GenAudioPlayer extends AbsObjectWrapper<AudioWife>{
   
    private static AudioWife audiowife;
    private ViewGroup mPlayerContainer;
   
    public void Initialize(BA ba, String uri) throws InstantiationException, IllegalAccessException{
        Uri ll = Uri.parse(uri);
        BA.Log("Uri parsed: " + ll);
        LayoutInflater inflater = (LayoutInflater) ba.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        String resourceName="aw_player";
        int resourceId = BA.applicationContext.getResources().getIdentifier(resourceName, "layout", BA.packageName);
        BA.Log("resourceId: " + resourceId);
        mPlayerContainer.findViewById(resourceId);
        AudioWife.getInstance().init(ba.context.getApplicationContext(), ll).useDefaultUi(mPlayerContainer, inflater);
    }

}

When I compile the library and run it in B4A it compiles just fine but it crashes with this error:
** Activity (main) Create, isFirst = true **


Uri parsed: /storage/emulated/0/Youtube_Mp3/sin bandera y llegaste tu.mp3


resourceId: 0


java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.ViewGroup.findViewById(int)' on a null object reference
at com.genesis.customaudioplayer.GenAudioPlayer.Initialize(GenAudioPlayer.java:34)
at com.genesis.mp3downloader.main._activity_create(main.java:365)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
at com.genesis.mp3downloader.main.afterFirstLayout(main.java:100)
at com.genesis.mp3downloader.main.access$100(main.java:17)
at com.genesis.mp3downloader.main$WaitForLayout.run(main.java:78)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)


at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.ViewGroup.findViewById(int)' on a null object reference

I'm assuming it can not find the resources, any ideas on how to fix this?

Thanks all in advanced.

Cheers,
Walter
 

warwound

Expert
Licensed User
Longtime User
The best solution i've found for libraries that need to access resources is to make the Eclipse project an 'android library' project instead of a 'standard java' library project.
Within the wrapper code in Eclipse you can now reference resources using the standard R.? syntax.

Now when you use the #AdditionalResources attribute in b4a you need to use the package name of the Eclipse android project.

Search google for info on using Eclipse to create an 'android library' project.
 

walterf25

Expert
Licensed User
Longtime User
The best solution i've found for libraries that need to access resources is to make the Eclipse project an 'android library' project instead of a 'standard java' library project.
Within the wrapper code in Eclipse you can now reference resources using the standard R.? syntax.

Now when you use the #AdditionalResources attribute in b4a you need to use the package name of the Eclipse android project.

Search google for info on using Eclipse to create an 'android library' project.
Hey warwound thanks for your reply, i apologize i forgot to update the thread, but i figured it out, I tried converting the project into a library but for some reason it didn't work, I will post soon the code so that others who encounter the same issue can see how i figured it out.

Thanks a lot!
Cheers,
Walter
 

bparent

Member
Licensed User
Longtime User
Hey warwound thanks for your reply, i apologize i forgot to update the thread, but i figured it out, I tried converting the project into a library but for some reason it didn't work, I will post soon the code so that others who encounter the same issue can see how i figured it out.

Thanks a lot!
Cheers,
Walter

I look forward to your posting on how you figured this out.
 
Top