Java Question How can I have in my Java libray Show an alert box (MsgBox)?

Amalkotey

Active Member
Licensed User
Longtime User
Hello,

how can I have in my Java libray Show an alert box (MsgBox)? The following code works for an Eclipse Android project properly.

B4X:
public int OK_MessageBox(String strMessage){
    AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
    alertbox.setMessage(strMessage);
    alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
            // Click
        }
    });
    alertbox.show();
    return 0;
}

The bug is probably the "Builder":

B4X:
new AlertDialog.Builder(this);

The builder needs according to SDK a reference to the Activity. I get this under a android project without problems. If I use extends embed the Activity class, I always get a "NullPointerExceptiion". Thank you for your help in advance.

best regards
Amalkotey
 

Amalkotey

Active Member
Licensed User
Longtime User
Hi Erel,

thank you for your quick help. What parameters required by the constructor and what constructors are available in the BA class?

B4X:
OK_MessageBox(new BA(null, null, null, null, null), "This is a test message!");

best regards
Amalkotey
 

agraham

Expert
Licensed User
Longtime User
Without going into too much detail a BA instance must always be provided by Basic4android, you cannot create an instance yourself. If your OK_MessageBox is being called from Basic4android code then you don't need to provide a BA. It is a hidden parameter that the compiler fills in so it is not visible in the Basic4ppc code.

If you want to call it within the Java code you need to get the compiler to give you an instance of it by defining a method that is called from Basic4android. Initialize is the obvious one


B4X:
private BA ba
...
public void Initialize(BA ba)
{
   this.ba = ba;
}

As the BA is provided by the compiler the Basic4android code is just
B4X:
Initialize
 
Top