I have created an application using the texttospeech api and I have all the functionality within one class. I would like to split this into several classes but when I do so I have a null exception error.
The texttospeech api has onclick buttons. Within these buttons I try to call a method from another class for the functionality.
I extend the class 1 with the current class I am using.
I then add the method image() within the class 1:
public void image() {
if(currentHelloIndex==0){
alertDialog.show();
}
else if (currentHelloIndex == 2) {
Image.setImageResource(R.drawable.books);
} else if (currentHelloIndex == 3) {
Image.setImageResource(R.drawable.mic);
}
Currently no variables are declared in class 1 as it is using the variables in the main class.
I then call this method in the main class. This doesn't seem to be working the class 1 has no onCreate method it is just a standard class which extends the main class.
I would appreciate any help on this as I need to separate the functionality into separate classes.
Edit:
currentHelloIndex is an int which is set to 0 in the main class
if the button is clicked an currentHelloIndex is 0 an alertdialog in the main class will appear
if the button is clicked and currentHelloIndex is 2 this will set the Image which is an ImageView in the main class with the image set.
I have put into the main class: static SoundGameScore sound;
Within the main class I have called sound.Image(); in an onclick. Please can someone let me know what I have done wrong, thanks.
You should use some of the refactoring functionality in your Java IDE (you ARE using a Java IDE, right?)
Related
I've been working on Xamarin for the past couple of years along with Android studio and I decided to create an application for a friend (full source code here https://github.com/nekrull/waiter don't be too harsh please :) )
The idea is that there is a base activity which exchanges fragments when a new screen should appear.
Fragments have everything that has to do with user interaction and the activity they are attached to handles the business logic.
To do this I have a base class CoreActivity/DataActivity which has some methods most Fragments use (like blocking the back button) and some helper methods (like calling a method on an attached fragment of a specific class) , a CoreInteraction that responds to this activity and
CoreFragment/AttachedFragment which is used as the base of all view fragments
so for example the view fragment would look like this:
public class GroupsFragment extends AttachedFragment<GroupsFragment.GroupsInteraction> {
//this is what we expect to be able to call in the parent
public interface GroupsInteraction extends CoreInteraction {
Group get_shown_group();
void new_group();
void select_parent();
}
}
which is basically a fragment that expects its attached activity to be able to respond to the interaction methods.
the activity fragment would look like this:
public class MainActivity extends DataActivity<MainData> implements
GroupsFragment.GroupsInteraction, (other interactions here) {
}
The problem is that since the application I'm working on has only one Activity with many small screens, the code inside the base activity will get big, that does not cause a problem with the application or compiling or anything else. But it makes it really hard to find what I'm looking for easily.
What I used to do in Xamarin is something like this:
public partial class MainActivity : DataActivity<MainData> {
}
for the initialization activity and then each interaction would get its own file like this:
public partial class MainActivity : GroupsInteraction {
}
It had the same effect (since the class is compiled as a single class) but the code would be tidy and easy to read.
Obviously there are no partial classes in Java, but is there a way to delegate the implementation of an interface to another class?
Something along the lines of saying "when you're invoking a method from interface a, invoke it from that class" without actually writing stuff like :
public Group get_shown_group() {
return new GroupHandler(this).get_shown_group();
}
public void new_group() {
new GroupHandler(this).new_group();
}
public void select_parent() {
new GroupHandler(this).select_parent();
}
Thanks in advance for any help you can provide
Something along the lines of saying "when you're invoking a method from interface a, invoke it from that class"
Taking you literally what you describe is plain delegation, a class does not implement some or any functionality itself, instead it wraps a class implementing the desired functionality, calling the methods of said wrapped class. You could even switch implementation at runtime, just changing the wrapped class as you go (assuming the classes share a common interface, of course). Of course that does not "spare" you from writing the delegations yourself.
class Wrapper implements GroupsInteraction {
private final GroupInteraction gi;
public Wrapper(GroupsInteraction gi) {
this.gi = gi;
}
Group get_shown_group() {
return this.gi.get_shown_group();
}
// ... other interface impls
}
Additionally, you should keep the GroupHandler as a member instead of creating a new Object each time, so
public Group get_shown_group() {
return new GroupHandler(this).get_shown_group();
}
becomes
public Group get_shown_group() {
return this.groupHandler.get_shown_group();
}
You can try Delegation Pattern
BaseActivity {
MyDelegateClass delegate;
void example() {
delegate.example();
}
}
P.S. both activity and delegate implements same interface
Details here
I created a Wizard, then I created a WizardPage namely FrontPage and added it to wizard in wizard's addPage method.
Since I have only one page in addPage method, the Next button not shown. I want it to be shown.
In the nextPressed method of the FrontPage, I created an instance of another java class that the class runs the program written in another language. In several points of that program, I need to create wizardPage dynamically. For this purpose I created a java class to contribute to the program. This class namely interfaceRule create an instances of wizardPage in other plugin.
My questions is how can I add this wizardPages to the main wizard? And also how can I return the value that user select in wizardPage to interfaceRule class?
edit:
public IWizardPage getNextPage() {
boolean isNextPressed = "nextPressed".equalsIgnoreCase(Thread.currentThread().getStackTrace()[2].getMethodName());
if (isNextPressed) {
boolean validatedNextPress = this.nextPressed();
if (!validatedNextPress) {
return this;
}
}
return super.getNextPage();
}
There are several ways to control the Next button in your class extending Wizard
You can call setForcePreviousAndNextButtons(true) in the wizard constructor to force the buttons always to be shown.
You can override the needsPreviousAndNextButtons method to decide if the buttons are required dynamically.
You can override the getNextPage method:
public IWizardPage getNextPage(final IWizardPage page)
to control exactly which page is shown next (also getPreviousPage).
I am still fairly new to Android and I am trying to implement Achievements inside my app. I basically want to replicate the achievements implemented in the "Type-a-Number Challenge" sample app given on the Google play developer site here.
I have a first activity that contains the methods and classes to handle the achievements, and a second activity where I have the variables that would be forwarded to the first activity for "processing". I copied the code that I believed was necessary for doing this, but I am always getting a null pointer exception when calling the listener inside the second class.
Here is my listener in the second activity:
public interface Listener {
public void onEnteredScore(int score);
}
Listener mListener = null;
public void setListener(Listener l) {
mListener = l;
}
The null pointer exception is flagged here when I call the listener as such (where mRequestedScore is different to 0):
mListener.onEnteredScore(mRequestedScore);
The first activity's class implements the second activity listener like this:
public class FirstActivity extends BaseGameActivity
implements SecondActivity.Listener
And includes the onEnteredScore method as such
#Override
public void onEnteredScore(int requestedScore) {
checkForAchievements(requestedScore);
pushAccomplishments();
}
I am not entirely sure if the error appears because the listener is expecting a click or some action by the user, or the "linkage" is not being established properly between both activities.
I looked around for similar issues but haven't found anything yet.
Apologies if the mistake is obvious.
Thanks in advance for any help provided.
Your "linkage" isn't established, hence the null.
If you want to pass data between Activities (not fragments), you'll need to use the Intent framework - see How do I pass data between Activities in Android application?
The Interface method that you are following in the Type a Number Challenge is useful for passing data between Fragments and the parent Activity. That is what it is being used for in that example. You don't appear to be using Fragments.
For achievements, you are probably better served using a class attached to your FirstActivity. That class can then implement your Listener interface (if the interface is even useful at that point). Using a separate activity is far more heavyweight than is required.
I am developing a Java ME program. The different forms are located in separate classes. I tried to switch display between main MIDlet and a class and succeeded. How to do the same between two classes? I am just a beginner in Java ME.
I use following code for the same,
First display a static Display variable in Midlet
private static Display display;
Now initialize the dislplay variable in class Constructor
public MyMidlet() {
display = Display.getDisplay(this);
}
Now declare a getDisplay() method in Midlet class
public static Display getDisplay () {
return display;
}
Now you can use this getDisplay() method to get the current Display's object and then set any class's form
MyMidlet.getDisplay().setCurrent(form);
Simplification is:
Display.getDisplay(this).setCurrent(screen);
Where screen is an instance of LCDUI (Form, Alert...) or intance of Canvas object.
The this is an instance of the MIDlet
NEWBIE ALERT!
Here's the situation. I've got an Android ListActivity class (AppWindow) that contains all the methods that create and update the UI for my application. It includes a method (refreshWindow) that calls setListAdapter, and therefore must be non-static. So far, I've been using a separate class (FileHandler) to perform manipulations on files that are referenced by the AppWindow class. I've reached a point where I want to call the refreshWindow method when a certain file manipulation has been performed. However, since the refreshWindow method is non-static, it seems that I would need to instantiate AppWindow and call the method through that instance. However, I'm not sure how to do this or if it's even a good idea. Perhaps I just need to move all of the FileHandler logic into AppWindow, although I'd prefer to keep them separate.
Here's a description of the situation in code form:
AppWindow.java
...
public class AppWindow extends ListActivity {
...
void refreshWindow() {
...
setListAdapter(new ListAdapter());
...
}
...
}
FileHandler.java
...
class FileHandler extends Activity {
...
static void doStuffToFiles() {
...
AppWindow appWindow = new AppWindow();
appWindow.refreshWindow();
...
}
...
}
Should I be doing this? If so, how do I properly instantiate AppWindow?
Create a static member variable; say act; in FileHandler
On creation of ListActivity set the variable act using 'this' variable of activity
Now you can access activity instance from FileHandler.
Update the UI must be in the UI thread. You can use handler to handle the message. The handler's constructor can contain the activity, so you can use it.