I have 2 classes in one .java file and it runs fine without errors or anything (the second class is used as a timer and changes variables every second) everything works but it wont call methods properly. Any idea of why this would be??? Heres my code of the second class.
Code:
class MyTime extends TimerTask{
//java.text.DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());
public game timecall2= new game();
public MyTime(Context ctx) {
// create internal instance
Context ctx2;
}
@Override
public void run() {
game.sec--;
if(game.sec==-1){game.sec=59;game.min--;}
game.Title2(); // set text in title bar
}
}
It would be easier if you explain more cleary, what you want to do.
Or post more code.
public game timecall2= new game();
I think, game is your first class?
Then you want to use the variable sec of the class game?
-> are they declared as static or why you don't call it over the object timecall2 you created?
Sry, but without more Code/Information to unterstand your problem, it's difficult to help. Also don't know, how skilled your are in programming.
*game* is the first class
I've tried calling the method through *timecall2* object but it doesn't help.
I've tried declaring the function as both static and no-static but nothing seems to change.
I've got lots of programming experience in other languages, but I'm less experienced with Java
And what's going wrong now?
Why don't you debug trough the code. Should be the easiest ways to find the problem.
You generally should use getters and setters for accessing variables inside of another class. Meaning that you have a method to set the value of the variable and a method to retrieve the value
Code:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MyClass extends Activity {
private static int mMin = 0;
private static int mSec = 0;
private TextView titleText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView titleText = (TextView) findViewById(R.id.titleText);
}
public static int getSec() {
return mSec;
}
public static void setSec(int newVal) {
mSec = newVal;
}
public static int getMin() {
return mMin;
}
public static void setMin(int newVal) {
mMin = newVal;
}
public static void setTitleText(String newVal) {
if (titleText != null) {
titleText.setText(newVal);
}
}
}
Notice the "static" modifier of the class methods.
Generally, you wouldn't instantiate this activity class. Especially if it already lives in memory. What I am guessing that you are doing here is that "game" is your activity and that MyTime is an object that you are using from inside of the activity and that you need to modify variables that live in the main Activity.
If that is the case then you would just do something like this:
Code:
import java.util.TimerTask;
import com.myapp.game.MyActivity;
class MyTime extends TimerTask {
private int sec = 0;
private int min = 0;
@Override
public void run() {
sec = MyActivity.getSec();
min = MyActivity.getMin();
sec--;
if(sec==-1) {
sec=59;
min--;
}
String title = String.valueOf(min)+":"+String.valueOf(sec); //obviously you need formatting
MyActivity.SetTitle(title); // set text in title bar
MyActivity.setSec(sec);
MyActivity.setMin(min);
}
}
That said... I just did the above as an example. This is entirely the wrong way to make a game timer. There is a better example here
From what I remember from C++, you would have a "base" class and then other classes beneath that base class.
To use the base class methods, the other classes had to be "friends". I'm a little rusty on my JAVA syntax...is "extends" the same thing as a friend class?
Java doesn't have friend functions, and "extends" means that the class is a subclass of whatever its extending.
To access a function/variable between classes that said function/variable must be static. Beware tho when do this depending on you implementation you must check for null variables. Since its static you can access you dont need a class object to access them through.
ak. SomeClass.function();
instead of using SomClass sc = new SomeClass(); sc.function();
since u can access it at any time its contents may not be initialized and be null. So ALWAYS check for null varaibles! lol. That or u can have one variable of that class to check if its class is initialized. such as..
SomeFunction.isInit();
where isInit(); is
private static initialized = false;
public static boolean isInit()
{
return initialized;
}
where in your onCreate & on onDestroy functions you set the initialized variable accordingly.
or..u could just do if(SomeClass.this!=null) lol :S
/me stops writting wot
Thanks for the input everyone. I've realised the problem (but still can't fix it). I can call methods in other classes from my timer class.... but my main class that has the methods that I need implements OnClickListener (public class game extends Activity implements OnClickListener) so it is ONLY updating the view methods when something is clicked on. How should I go about fixing it so that I can call methods that will update when a timer calls them (e.g. I display the remaining time in the title bar and it doesn't update the current time UNLESS a button is clicked on)
Why not run the timer as a thread in the activity class itself?
Related
Hi,
A few weeks ago I started developing an Android app but I've gut one problem
If the user changed the orientation of his phone the Activity is of course newly created. If there is ProgressDialog opened, I simply open a new one and the user does not realize it, but if I show an AlertDialog containing a few hundred elements and the user scrolls a bit he/she will realize it after I recreate the AlertDialog because the dialog will start again with the first element and the user has to scroll newly to the element he wants.
How I handle the "ListDialog":
At first I have two classes which simplify the ListDialog because I use it a few times...
ListDialog class:
Code:
public class ListDialog {
public static int CHOOSE_MODE_ONLINE = 0x01;
public static int CHOOSE_MODE_BOOKMARK = 0x02;
public static int CHOOSE_MODE_LOCAL = 0x03;
public static int CHOOSE_CHAPTER_ONLINE_DOWNLOAD = 0x04;
public static int CHOOSE_CHAPTER_BOOKMARK_DOWNLOAD = 0x05;
public static int CHOOSE_CHAPTER_ONLINE_READ = 0x06;
public static int CHOOSE_CHAPTER_BOOKMARK_READ = 0x07;
public static int CHOOSE_CHAPTER_LOCAL_READ = 0x08;
public static int CHOOSE_CHAPTER_LOCAL_DELETE = 0x09;
public static int GOTO_PAGE = 0x0A;
public static void show(Context context, String title, CharSequence[] elems, final ListMethodInvoker invoker)
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title);
builder.setItems(elems, new DialogInterface.OnClickListener() {
%mail%Override
public void onClick(DialogInterface dialog, int which) {
invoker.invoke(which);
}
});
builder.setOnCancelListener(new OnCancelListener() {
%mail%Override
public void onCancel(DialogInterface dialog) {
invoker.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
ListMethodInvoker class:
Code:
public class ListMethodInvoker {
public void invoke(int id)
{
}
public void cancel()
{
}
}
and now I create the dialog:
Code:
ApplicationController.get().addOpenedDialog(ListDialog.CHOOSE_MODE_ONLINE);
ListDialog.show(OnlineActivity.this,
mangaController.getManga().getMangaName(),
new CharSequence[]{"Add to Bookmarks", "Download a Chapter", "Read a Chapter"},
new ListMethodInvoker()
{
%mail%Override
public void invoke(int id)
{
ApplicationController.get().removeOpenedDialog(ListDialog.CHOOSE_MODE_ONLINE);
switch(id)
{
case 0: addBookmark(mangaController.getManga()); break;
case 1:
ApplicationController.get().addOpenedDialog(ListDialog.CHOOSE_CHAPTER_ONLINE_DOWNLOAD);
handleChapter(ChapterMode.Download);
break;
case 2:
ApplicationController.get().addOpenedDialog(ListDialog.CHOOSE_CHAPTER_ONLINE_READ);
handleChapter(ChapterMode.Read);
break;
}
}
%mail%Override
public void cancel()
{
ApplicationController.get().removeOpenedDialog(ListDialog.CHOOSE_MODE_ONLINE);
}
});
I also add the ID of the dialog to my ApplicationController which allows me to remember if a dialog has been openend and I can recreate it when onCreate(...) is called again.
(The ApplicationController uses the singleton design pattern which always allows me to retrieve the same instance of the ApplicationController.)
Thanks in advance
best regards
mike
btw: If you wonder why I write %mail% instead of the correct symbol, I get the following exception message if I use it: To prevent spam to the forums, new users are not permitted to post outside links in their messages. All new user accounts will be verified by moderators before this restriction is removed.
I'm wanting to "do something" (actually play an mp3 sound) when someone has finished entering data in an EditText object in my activity, but I'm not sure how to tell when this has occurred. In VB, there is a event called lostfocus, but I don't know how to do it in java. I'm guessing that it has something to do with OnClickListener to see when the user is actually in the EditText object (let's call it et_01), but how do I know when they've gone to some other object?
I'd appreciate any specific help, as I'm pretty new to java.
How about OnFocusChangeListener?
http://developer.android.com/reference/android/view/View.OnFocusChangeListener.html
I did some searching and it seems like I need to use OnFocusChangeListener. I found this code snippet (below), but I'm too dumb to figure out how to plug it into my app. If my EditText that I'm watching is called et_01, can somebody please tell me 2 things...
1. How do I tie the OnFocusChangeListener to my et_01?
2. Where (specifically) does this code go in my activity? Inside my extends Activity...after the extends Activity...where oh where?
Code:
OnFocusChangeListener focusListener = new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
/* When focus is lost check that the text field
* has valid values.
*/
if (!hasFocus) {
validateInput(v);
}
}
You have to connect a TextView object to your actual textview, then override the OnFocusChangeListener of that textview object.
Here's a complete implementation using your snippit (You have to implement validateInput() yourself):
Code:
package com.myapp.mytest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.TextView;
public class MyTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.et_01);
tv.setOnFocusChangeListener(new OnFocusChangeListener(){
@Override
public void onFocusChange(View v,boolean hasFocus){
/* When focus is lost check that the text field
* has valid values.
*/
if (!hasFocus) {
validateInput(v);
}
}
});
}
}
Thank you, thank you, thank you! That is some nice help. I have one follow up question:
In my own activity, I should be able to put the code below in just under my own call: setContentView(R.layout.main);
In other words, I don't need to create a new class to do this. Correct? I apologize for my ignorance.
Code:
TextView tv=(TextView)findViewById(R.id.et_01);
tv.setOnFocusChangeListener(new OnFocusChangeListener(){
@Override
public void onFocusChange(View v,boolean hasFocus){
/* When focus is lost check that the text field
* has valid values.
*/
if (!hasFocus) {
validateInput(v);
}
}
});
Gene Poole said:
You have to connect a TextView object to your actual textview, then override the OnFocusChangeListener of that textview object.
Here's a complete implementation using your snippit (You have to implement validateInput() yourself):
Code:
package com.myapp.mytest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.TextView;
public class MyTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.et_01);
tv.setOnFocusChangeListener(new OnFocusChangeListener(){
@Override
public void onFocusChange(View v,boolean hasFocus){
/* When focus is lost check that the text field
* has valid values.
*/
if (!hasFocus) {
validateInput(v);
}
}
});
}
}
Click to expand...
Click to collapse
Yes.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (stupid forum says my response was too short)
OK, I'm having an issue that I don't understand. In the code below, I'm getting an error on the MediaPlayer mpWeight = MediaPlayer.create(this, R.raw.mppig);
Holding my cursor over .create says:
The method create(Context, int) in the type MediaPlayer is not applicable for the arguments (new View.OnFocusChangeListener(){}, int)
What does that mean, and more importantly, how do I resolve it?
Here's the whole routine:
Code:
TextView tv=(TextView)findViewById(R.id.weight);
tv.setOnFocusChangeListener(new OnFocusChangeListener(){
@Override
public void onFocusChange(View v,boolean hasFocus){
/* When focus is lost check that the text field
* has valid values.
*/
if (!hasFocus) {
float tempweight = Float.parseFloat(et_weight.getText().toString());
if(tempweight > 200){
MediaPlayer mpWeight = MediaPlayer.create(this, R.raw.mppig);
mpWeight.start();
}
}
}
});
It has to do with the "this" pointer you are passing to the create() method. Since you are creating this within the OnFocusChangeListener() class, that it the "this" pointer. OnFocusChangeListener() does not resolve to a type "context" whereas if you'd created your media player within your Activity, Activity does resolve to a context.
To resolve this, make a class member of your activity that keeps a copy of the context. Assign it in the activity's OnCreate:
Code:
public class MyTest extends Activity {
[COLOR="blue"]protected Context mContext=null;[/COLOR]
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
[COLOR="Blue"]mContext=this;[/COLOR]
TextView tv=(TextView)findViewById(R.id.et_01);
tv.setOnFocusChangeListener(new OnFocusChangeListener(){
@Override
public void onFocusChange(View v,boolean hasFocus){
/* When focus is lost check that the text field
* has valid values.
*/
if (!hasFocus) {
float tempweight = Float.parseFloat(et_weight.getText().toString());
if(tempweight > 200){
MediaPlayer mpWeight = MediaPlayer.create([COLOR="blue"]mContext[/COLOR], R.raw.mppig);
mpWeight.start();
}
}
}
});
Gene, you are awesome! Thanks again for the help.
As with many things in life, this has led me to a new problem, that I do believe will be the end of this subject, if I can get it resolved.
In my activity, I have a few EditText objects mixed in with a few Spinner objects. The new problem is that if you have your cursor in an EditText object and the next item in the activity is a Spinner, the focus doesn't leave the EditText when the Spinner is selected. The focus of any of my EditText objects only leaves those objects IF the next thing selected is another EditText.
I'm unsure how to resolve this. Is there possibly a way to force the focus out of the EditText once a Spinner (or any other object) is touched? I would have thought that would happen automatically, but it doesn't seem to be doing so.
Gene Poole said:
It has to do with the "this" pointer you are passing to the create() method. Since you are creating this within the OnFocusChangeListener() class, that it the "this" pointer. OnFocusChangeListener() does not resolve to a type "context" whereas if you'd created your media player within your Activity, Activity does resolve to a context.
To resolve this, make a class member of your activity that keeps a copy of the context. Assign it in the activity's OnCreate:
Click to expand...
Click to collapse
I'm using the sample Google provides, FragmentTabsPager to work with tabs in my application. I'm having a lot of trouble switching the default class for my own, which is nothing more than Preferences right now. Can anyone offer some suggestions?
From the sample:
Code:
mTabsAdapter.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
LoaderCursorSupport.CursorLoaderListFragment.class, null);
My class:
Code:
public class Preferences extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
}
I think the "trick" they use to connect ViewPager with TabHost isn't set up correctly to handle Intents, but I could be wrong. I'm just unsure how to get setContent working with these tabs. Any thoughts?
I'd like to register a listener inside AccessibilityService extented class but I don't know how to achieve that as I don't have a instance of it (it is started using intent). Or maybe there is another way of getting callback from this class? I just want to notify another class when the "onAccessibilityEven()" is trigerred.
Code:
public class NotifyService extends AccessibilityService {
// declaration of the interface
public interface Listener {
public void onNotifyChange(boolean newNotification);
}
// registration of the listener
public void registerListener(Listener listener) {
mListener = listener;
}
// ...
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if(mListener != null) {
mListener.onNotifyChange(true);
}
}
}
Can you build an handler in the other class? If yes, just send a message and the handler in the other class will receive it If you need an example I will write it.
Do you mean the same handler which is usually used for communicating between threads? If you could give me some example would be great.
Hi all, im having a go at developing a simple app. i have little experience with Java and Android development. i have a little test app at the moment and have created a new class, im trying to create a new instance of this class on a button click. it fails to do so, i cant for the life of me see why so.. can someone shed any light on this?
Thanks
Debuging this shows it hitting the "LocationFactory locationf = new LocationFactory();" line and throwing an exception-
"java.lang.NullPointerException"
Main
Code:
package com.example.testapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends Activity {
private static final Context Context = null;
protected static final String TAG = null;
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
[user=439709]@override[/user]
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void mainButton(View view) throws IOException {
try {
LocationFactory locationf = new LocationFactory();
Toast.makeText(this, locationf.getAddress(),Toast.LENGTH_LONG).show();
} catch (Exception e)
{
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}
}
Class
Code:
package com.example.testapp;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import java.io.IOException;
import java.util.Locale;
import java.util.List;
public class LocationFactory
{
private static final Context Context = null;
Geocoder geocoder = new Geocoder(Context, Locale.getDefault());
LocationManager manager = (LocationManager) Context.getSystemService(android.content.Context.LOCATION_SERVICE);
public double Latitude = 0.0;
public double Longitude = 0.0;
public LocationFactory()
{
}
public String getAddress() throws IOException
{
String ReturnAddress = "";
String Address = "", City = "", Country = "";
List<Address> addresses = null;
if(manager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
// Use GPS Radio Location
Location GPSlocation = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Latitude = GPSlocation.getLatitude();
Longitude = GPSlocation.getLongitude();
}
else
{
// Use Cell Tower Location
Location NETlocation = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Latitude = NETlocation.getLatitude();
Longitude = NETlocation.getLongitude();
}
if(Latitude > 0 && Longitude > 0)
{
addresses = geocoder.getFromLocation(Latitude, Longitude, 1);
if(!addresses.isEmpty())
{
Address = addresses.get(0).getAddressLine(0);
City = addresses.get(0).getAddressLine(1);
Country = addresses.get(0).getAddressLine(2);
}
}
ReturnAddress = Address + " " + City + " " + Country;
return ReturnAddress;
}
}
I don't see anywhere in your code where you are calling the mainButton(View view) method. In the Android lifecycle, the onCreate method is the equivalent of a normal Java program's main() method, which means that code execution begins with the first line of onCreate(). Not knowing what you're trying to do, a good start would be to call your mainButton() method AFTER setContentView() in onCreate().
Side note: your mainButton() method has a View parameter that is never used. Is there a reason for that?
Android activity lifecycle: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
You have to use an intent on that button click, use the method onClickListener and define the intent in the androidmanifest.xml
e.g
Code:
Button button = (Button) findViewById(R.id.[B]button[/B]) // replace latter button with actual id defined in main xml.
button.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent("[B]com.example.packagename.CLASSNAME[/B]")); // this should be your own package name.
}
});
Also define this in android manifest under the <application> and </application>
Code:
<activity
android:name=".[B]CLASSNAME[/B]"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="[B]com.example.packagename.CLASSNAME[/B]" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Change the values of BOLD text according to your own values.
I tried to help you as far as I understood your question. Please let me know if you face any problem I would be more than happy to help you. Rest I am also in the learning phase so you can always PM me if you face any problem.
Hit thanks if I have helped you in any way.
coolbud012 said:
You have to use an intent on that button click, use the method onClickListener and define the intent in the androidmanifest.xml
Click to expand...
Click to collapse
Nope! He didn't say that he wanted to launch a new Activity when the button is clicked. He wants to create a new instance of his LocationFactory Class.
jpepin said:
Nope! He didn't say that he wanted to launch a new Activity when the button is clicked. He wants to create a new instance of his LocationFactory Class.
Click to expand...
Click to collapse
Oops yeah right read that now...I thought he want to start an activity... Anyways tried to delete my reply but not getting an option to delete.
There are many flaws in his code. And the other thing is if its his first app and if he has low level of programming experience then according to me it would be a next to impossible app for him, as per his code and what he is trying to implement.
I think he should rather start up with small apps, understand things and then move on to complex apps.
P.S - its just my opinion
Click to expand...
Click to collapse
Agreed that he should start small...which is exactly why your suggestion for creating and handling Intents makes no sense. Before that, he should first understand the activity lifecycle. Until then, he can just stick to trivial single-activity apps to gain experience.
OP: This code should be placed in the onCreate method:
Code:
Button button = (Button) findViewById(R.id.your_button_ID_here)
button.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View arg0) {
mainButton(); // get rid of the View parameter in this method...it's not needed
}
});
This will cause a new instance of your LocationFactory to be created, and will also cause your Toast message to be displayed.
thanks for the replies. yes you are right in that i am inexperienced, but this is just a test app for me to play around with and learn on. i tend to learn better by doing rather than constantly reading. thanks for your suggestions ill look into them
osmorgan said:
thanks for the replies. yes you are right in that i am inexperienced, but this is just a test app for me to play around with and learn on. i tend to learn better by doing rather than constantly reading. thanks for your suggestions ill look into them
Click to expand...
Click to collapse
I also believe in the same, I also keep on doing experiments and testing things out.
What I would suggest is that start with a small app and understand the insights on how android works and all...
Thanks
Potential Solution
Alright, I think I've found your problem. Have a look at where you define your variables in your LocationManager class:
Code:
private static final Context Context = null;
Geocoder geocoder = new Geocoder(Context, Locale.getDefault());
LocationManager manager = (LocationManager) Context.getSystemService(android.content.Context.LOCATION_SERVICE);
This is your problem:
Code:
Context Context = null;
If your context is null, and you use it to create a geocoder and call Context.getSystemService, you'll hit a null pointer. You're trying to access an object (the Context) that doesn't even exist
I'd recommend you pass the context in the LocationManager constructor and then instantiate your objects there. That's standard java procedure.
Code:
private Context mContext = null;
Geocoder geocoder = null;
LocationManager manager = null;
public double Latitude = 0.0;
public double Longitude = 0.0;
public LocationFactory(Context context)
{
this.mContext = context;
this.geocoder = new Geocoder(context, Locale.getDefault());
this.manager = (LocationManager) Context.getSystemService(android.content.Context.LOCATION_SERVICE);
}
I also renamed Context to mContext - it's generally a good idea to keep the instance's name separate from the class name.
Try that - it should work. Please feel free to ask any more questions - this is how I learned, and I think it's the best way!