Reputation: 905
I'm very new to Java which is required for a Blackberry App development project (which is what I'm doing now). My issue is that I am trying to use this class I found online (attached below) to implement a notification feature in my application. As I said I'm extremely new to Java so I don't know how to call the setVisible1
from another class, say UserInterface
. I have tried MyAppIndicator._indicator.setVisible1(true,1);
but this produces an error of:
"The method setVisible1(boolean, int) is undefined for the type ApplicationIndicator".
Any help would be appreciated. Thanks!
public class MyAppIndicator
{
public ApplicationIndicator _indicator;
public static MyAppIndicator _instance;
private MyAppIndicator () {}
public static MyAppIndicator getInstance() {
if (_instance == null) {
_instance = new MyAppIndicator ();
}
return(_instance);
}
public void setupIndicator() {
//Setup notification
if (_indicator == null) {
ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance();
_indicator = reg.getApplicationIndicator();
if(_indicator == null) {
ApplicationIcon icon = new ApplicationIcon(EncodedImage.getEncodedImageResource ("status_icon_24x24.png"));
_indicator = reg.register(icon, false, true);
_indicator.setValue(0);
_indicator.setVisible(false);
}
}
}
public void setVisible1 (boolean visible, int count) {
if (_indicator != null) {
if (visible) {
_indicator.setVisible(true);
_indicator.setValue(count);
} else {
_indicator.setVisible(false);
}
}
}
}
Upvotes: 0
Views: 6023
Reputation: 9314
boolean visible = ...; // you need to set something here int count = ...; // you need to set something, here MyAppIndicator mai = MyAppIndicator.getInstance(); mai.setVisible1(visible, count);
new ClassName
. However, MyAppIndicator declares the constructor as private, so other classes cannot invoke it. Instead, myAppIndicator provides a getInstance() method that will handle object construction for you.You should also read the Java Tutorial - they've got a far better description of all this.
Upvotes: 0
Reputation: 31433
In order to call methods from Classes in Java, you need instance of the class (or if the class is 'static' you can directly use the methods, as 'static' basically means only 1 instance).
In your case MyAppIndicator
implements Singleton:
public static MyAppIndicator _instance;
private MyAppIndicator () {};
public static MyAppIndicator getInstance() {
if (_instance == null) {
_instance = new MyAppIndicator ();
}
return(_instance);
}
This means that when you call getInstance() it will return instance of MyAppIndicator
if such an instance already exists, or it will create a new one, if there's no instance.
After acquiring an instance of a class, you simply call it's methods, if they have the appropriate access modifiers. In your case setVisible1
has access modifier 'public' so you will be able to call it outside of MyAppIndicator
.
So in code:
a) acquiring instance:
MyAppIndicator instanceOfMyAppIndicator = MyAppIndicator.getInstance();
b) calling the method:
instanceOfMyAppIndicator.setVisible1(true,1);
Upvotes: 1
Reputation: 1763
You can do the following:
MyAppIndicator myAppIndicator = MyAppIndicator.getInstance();
myAppIndicator.setVisible1(true,1);
Upvotes: 0
Reputation: 111
Looking at the above code, you should call
MyAppIndicator.getInstance().setVisible1(true,1);
than
MyAppIndicator._indicator.setVisible1(true,1);
Upvotes: 0