weird warning in my disposeSplashScene - java

I am new Programmer ,
And I using andengine to develope a game, i juss keep on gettin this weird warning called
The method disposeSplashScene() from the type SceneManager is never used locally
i wanted to initialize my splash scene and dispose is when its no longer required
here is the code
public void createSplashScene(OnCreateSceneCallback pOnCreateSceneCallback)
{
ResourcesManager.getInstance().loadSplashScreen();
splashScene = new SplashScene();
currentScene = splashScene;
pOnCreateSceneCallback.onCreateSceneFinished(splashScene);
}
private void disposeSplashScene()
{
ResourcesManager.getInstance().unloadSplashScreen();
splashScene.disposeScene();
splashScene = null;
}

That warning means you're never calling the disposeSplashScreen function anywhere. Either you need to call it somewhere, or the function itself is unneeded and can be deleted.
If this function is meant to be called from an outside class, it should be public instead of private.

Related

Cannot change active tab in Swing, "Non static method cannot be referenced"

I'm currently having an issue with Swing, where I am trying to get a button from Tab 1, once clicked on, to go to tab 2. The two blocks of code that are at work here, as far as I know, are as follows:
// Variables declaration - do not modify
private javax.swing.JTabbedPane jTabbedPane;
// End of variables declaration
private void addTabs() {
this.jTabbedPane.add("Home page", new Home());
this.jTabbedPane.add("Nieuwe Gebruiker", new UserNew());
this.jTabbedPane.add("Terugkerende gebruiker", new UserReturning());
this.jTabbedPane.add("Ingelogde gebruiker", new UserReturning());
this.jTabbedPane.add("Administrator", new Admin());
}
public void setTab(String tabName) {
this.jTabbedPane.setSelectedIndex(jTabbedPane.indexOfTab(tabName));
}
And:
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO Go to user logged in page
FullHouse.setTab("UserLoggedIn");
}
However, the latter piece of code is giving an error on calling the setTab method:
non-static method setTab(String) cannot be referenced from a static context
I have tried making the setTab method static, but then Netbeans complains about the jTabbedPane not being static. I am not able to make the jTabbedPane static, as Netbeans won't allow me to edit the code in this manner.
How can I fix this?
You need to have an instance of FullHouse that you can call setTab on.
In other words, the compiler is asking, WHICH FullHouse do you want to call setTab on? You hopefully instantiated it somewhere.
Can't give more specifics without more details from your first code block.

How to call a private method of a class in different package

There is a BookView.class that has a private method defined as below
public class BookView{
private boolean importBook(String epubBookPath){
//The function that adds books to database.
}
}
I am trying to call this function from a different package. My code is
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
/*Now we add the book information to the sqlite file.*/
TextView textView=(TextView)findViewById(R.id.textView1);
String filename = textView.getText().toString();
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String epubBookPath = baseDir+filename;
Log.i("epubBookPath:",epubBookPath); //No errors till here!
try {
Method m=BookView.class.getDeclaredMethod("importBook");
m.setAccessible(true);//Abracadabra
//I need help from here! How do i pass the epubBookPath to the private importBook method.
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
Intent in = new Intent(getApplicationContext(),
CallEPubUIActivity.class);
startActivity(in);
}
EDIT:
I found another public method in the jar file which is doing the above work.
public void jsImportBook(String epubBookPath) {
if (!BookView.this.importBook(epubBookPath))
return;
BookView.this.createBookshelf();
}
If you want to do that you should make it public or make a public wrapper method it.
If thats not possible, you can work your way around it, but thats ugly and bad and you should have really good reasons to do so.
public boolean importBook(String epubBookPath){
//The function that adds books to database.
}
or
public boolean importBookPublic(String epubBookPath){
return importBook(epubBookPath);
}
private boolean importBook(String epubBookPath){
//The function that adds books to database.
}
Also note that if you CAN'T access the method directly in a third-party library than it is most likely INTENDED that way. Take a look at the call hierarchy of the private method and see if you find a public method that does the call to the private one and that also does what you need.
Libraries are often designed in a way that a public method does some checking (all Parameters given, authenticated etc.) and then pass the call to the private method to do the actual work. You almost never want to work around that process.
With reflection, you'll need an instance of BookView to invoke the method with (unless it's a static method).
BookView yourInstance = new BookView();
Method m = BookView.class.getDeclaredMethod("importBook");
m.setAccessible(true);//Abracadabra
Boolean result = (Boolean) m.invoke(yourInstance, "A Path"); // pass your epubBookPath parameter (in this example it is "A Path"
The method you are looking for is Method#invoke(Object, Object...)
Use reflection to get you method and set Accessible as true, then invoke the method using BookView Object instance and required parameters(path string) using statement as below:
Boolean result = (Boolean)method.invoke(bookObject, epubBookPath);
Sample code as below:
Method method = BookView.getDeclaredMethod("importBook");
method.setAccessible(true);
Boolean result = (Boolean)method.invoke(bookObject, epubBookPath);
Private methods cannot be accessed outside the class it is defined.
Make it Public.

java callback, unscribe listener inside of interface implementation

I would like to implement system of callbacks which looks like this (pseudo code):
final Listener listener = ListenerCtrl.addListener(new Listener() {
void onNotify(String response){
ListenerCtrl.unsetListener(listener);
} }
This code mean that after received message, i want to unscribe from future notifications. I found very attractive have this action inside of callback.
Here is my actual implementation:
final WebServiceMsgListener wml = new WebServiceMsgListener()
{
public void onMsgNotify(JSONObject response, int ecode)
{
Log.v(TAG, "getSetStateProgressBar MSG_MGT_STATICINFO: onMsgNotify ecode" +
ecode);
authDelegate.unsetMsgListener(wml);
}
};
authDelegate.addMsgListener(NAOMsg.MSG_MGT_STATICINFO, wml);
Unfortunately, my current implementation show me eclipse error:"The local variable wml may not have been initialized"
Question: how I can get round this, to finally unscribe inside of callback and dont have this error ?
Change your code to:
authDelegate.unsetMsgListener(this);
this refers to the current object (whose onMsgNotify() is being executed at the time this statement is executed).
Note: Although, the variable wml is available to the new object, it has not yet been initialized at the time of the creation of the object, hence the error. It is initialized right after the object is fully created.

Calling getString() in constructor in Wicket gives error

I'm having some problems with localization in wicket.
This is the code:
private String displayString;
private TextField<String> myTextField;
public myPage(DomainObject domainObject){
if(domainObject != null)
displayString = domainObject.getDisplayString();
myTextField = new TextField<String>("myTextField", new PropertyModel<String>(this, "displayString"));
if(Strings.isEmpty(displayString))
displayString = getString("mandatory"); //<- error message here
}
The problem is that calling getString in the constructor results in an error message("...This can sometimes lead to an invalid or no localized resource returned...").
I want to use a PropertyModel for the TextField since I don't want to translate the string I get from domainObject.getDisplayString(). I don't want the changes made in the TextField to affect the value in domainObject directly.
It's possible to get rid of the error message by doing this instead of getString:
if(Strings.isEmpty(displayString))
displayString = new ResourceModel("mandatory").getObject(); //<- no error message
To my understanding, this is the same thing as calling getString (you just hack away the warnings, but the problem still exist).
A solution i thought of is this:
#Override
protected void onAfterRender() {
super.onAfterRender();
if(Strings.isEmpty(displayString))
displayString = getString("mandatory"); //<- no error message
}
Does anyone see a problem with this solution? Maybe I'm not thinking "wickety" enough?
Calling getString() requires the component to be inside a component hierarchy, where it can access it's parent to have the chance to fall back to properties defined there or further up in the tree. This isn't possible inside the component's constructor (as you add it to it's parent at a later point). Wicket 1.5 introduces the onInitialize function for these operations. With Wicket versions prior to this, there is an easy way to emulate this behaviour:
In your base component and page define a non-final empty method as
protected void onInitialize() {}
and add this to the onBeforeRender method:
protected void onBeforeRender() {
...
if (!hasBeenRendered()) {
onInitialize();
}
...
}
Then you can use an overridden onInitialize() method in any of your components to deal with stuff that has to wait until the component hierarchy is established.
What about a reusable behavior:
public class MandatoryBehavior extends AbstractBehavior {
public void onComponentTag(Component component, ComponentTag tag) {
if (((AbstractTextComponent)component).isRequired() && Strings.isEmpty(tag.get("value"))) {
tag.put("value", component.getString("mandatory"));
}
}
}
You'd have to check submitted values in a validator though.
HTML5 placeholders are even nicer.

Constructor being called again?

I have this constructor;
public UmlDiagramEntity(ReportElement reportElement, int pageIndex, Controller controller) {
super(reportElement.getX1(), reportElement.getY1(), reportElement.getX2(), reportElement.getY2());
setLayout(null);
this.pageIndex = pageIndex;
this.controller = controller;
reportElements = reportElement.getInternalReportElements();
components = new ArrayList<AbstractEntity>();
changedComponentIndex = -1;
PageListener p = new PageListener();
this.addMouseMotionListener(p);
this.addMouseListener(p);
setPage();
}
And I have an update method in the same class;
#Override
public void update(ReportElement reportElement) {
if (changedComponentIndex == -1) {
super.update(reportElement);
} else {
reportElements = reportElement.getInternalReportElements();
if (components.size() == reportElements.size()) {
if (!isCommitted) {
if (reportElement.getType() == ReportElementType.UmlRelation) {
if (checkInvolvementAndSet(changedComponentIndex)) {
anchorEntity(changedComponentIndex);
} else {
resistChanges(changedComponentIndex);
}
return;
}
}
..................goes on
When I follow the flow from the debugger, I see that when update is called, somewhere in the method, the program goes into the constructor and executes it all over again (super, pageIndex, etc.). Why does it go to the constructor :D I didn't tell it to go there.
I can make a deeper analysis and see where it goes to the constructor if you want. By the way, changedComponentIndex is a static variable.
I would find it far more probable that you are seeing it construct two different objects. You'd have to provide more information like a stack trace; here you haven't even shown the constructor being invoked!
The behaviour you describe is pretty much impossible. Either your code is different from what you've shown or you're not debugging the code you think you're debugging. Without complete code that we can run, that's all we can say.
Are you sure that update is not called indirectly from within the constructor, which would result in a breakpoint in update getting triggered.
Try setting a breakpoint at the start of the constructor and at the end, then one in update. When you hit the first constructor breakpoint, hit 'continue' and see which breakpoint gets triggered next.
Is this multi-threaded? Is it possible that the constructor for a different instance created on another thread is being called?

Categories