Amalkotey Active Member Licensed User Longtime User Feb 27, 2011 #1 Hello, how can I have in my Java libray Show an alert box (MsgBox)? The following code works for an Eclipse Android project properly. Code: Copy 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": Code: Copy 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
Hello, how can I have in my Java libray Show an alert box (MsgBox)? The following code works for an Eclipse Android project properly. Code: Copy 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": Code: Copy 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
Erel B4X founder Staff member Licensed User Longtime User Feb 27, 2011 #2 Mark your class with @ActivityObject. Change the method signature to: Code: Copy public int OK_MessageBox(BA ba, String strMessage) Then you can access the activity with ba.activity.
Mark your class with @ActivityObject. Change the method signature to: Code: Copy public int OK_MessageBox(BA ba, String strMessage) Then you can access the activity with ba.activity.
Amalkotey Active Member Licensed User Longtime User Feb 27, 2011 #3 Hi Erel, thank you for your quick help. What parameters required by the constructor and what constructors are available in the BA class? Code: Copy OK_MessageBox(new BA(null, null, null, null, null), "This is a test message!"); best regards Amalkotey
Hi Erel, thank you for your quick help. What parameters required by the constructor and what constructors are available in the BA class? Code: Copy OK_MessageBox(new BA(null, null, null, null, null), "This is a test message!"); best regards Amalkotey
agraham Expert Licensed User Longtime User Feb 28, 2011 #4 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 Code: Copy private BA ba ... public void Initialize(BA ba) { this.ba = ba; } As the BA is provided by the compiler the Basic4android code is just Code: Copy Initialize
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 Code: Copy private BA ba ... public void Initialize(BA ba) { this.ba = ba; } As the BA is provided by the compiler the Basic4android code is just Code: Copy Initialize