Results 1 to 15 of 15
Thread: Really n00bish question...
- 06-07-2010, 11:53 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 6
- Rep Power
- 0
Really n00bish question...
So I figure this has to be possible but I don't know how to do it.
If it makes any difference, I'm coding Java for Android.
First post here so go easy :)
==
I was assigned to write a program to solve a real world problem in one of my classes. It's due next Monday (one week). The only hangup I have left to solve is possibly the easiest.
I have one package, com.xxx.android.yyy, and in that package I have private class 1. In that private class, I have a public void, and in that there are a few "if" statements:
Java Code:public void onLocationChanged(Location location) { if(location!=null){ if(location.hasSpeed()){ mySpeed = location.getSpeed(); if(mySpeed>maxSpeed) maxSpeed = mySpeed; tv.setText("\nCurrent speed: " + mySpeed*2.2369 + " mph, Max speed: " + maxSpeed*2.2369 + " mph"); if(mySpeed>=limitSpeed){ //***Need to call public class from pkg com.xxx.android.zzz*** }
Within that if statement [if(mySpeed>=limitSpeed)], I need to call a public class from package com.xxx.android.zzz
How do I do that?
EDIT: I think this might be in the wrong forum. I'm sorry if it is. In the event that I'm right, feel free to move it to the appropriate place :)Last edited by Copaman; 06-07-2010 at 11:57 PM.
- 06-08-2010, 12:01 AM #2
Private classes should be only used in nested classes. There is no way to instantiate a private class unless it's nested in a public class.
"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 06-08-2010, 12:08 AM #3
Not sure about your question. Is the public class you want to "call" in the package ...zzzI need to call a public class from package com.xxx.android.zzz
or is the "calling" code in the ...zzz package?
By "calling" do you mean use new to create a new instance of the class?
Or do you mean calling a method in the class?
Then is the question: How do I get a reference to the class so I can call one of its methods?
- 06-08-2010, 12:09 AM #4
Member
- Join Date
- Jun 2010
- Posts
- 6
- Rep Power
- 0
- 06-08-2010, 12:14 AM #5
Member
- Join Date
- Jun 2010
- Posts
- 6
- Rep Power
- 0
The class I need to use from the zzz package plays a very important role in the overall program.
Unfortunately I need it to perform that role in the private class within the yyy package, when mySpeed is greater than limitSpeed.
here:
I need that to do it's thing when mySpeed > limitSpeed.Java Code:public class AirplaneChange extends Activity { public void method() { Context context = getApplicationContext(); boolean isEnabled = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0)== 1; Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);
- 06-08-2010, 12:15 AM #6
In that case, the same way you would reference any other class:
Java Code://Assume private class is called ActionListener. //Assume constructor takes no paramaters. ActionListener listener = new ActionListener(); //Assume there is at least one method that takes no paramaters listener.[U]method[/U]();
"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 06-08-2010, 01:03 AM #7
Member
- Join Date
- Jun 2010
- Posts
- 6
- Rep Power
- 0
Would that private be from my first package, yyy?
- 06-08-2010, 01:09 AM #8
Only if the public class that has the nested private class is in the package yyy. Example:
Both classes (A and B) are in the package yyy because A is in the package yyy and B is part of A. I hope that makes sense.Java Code:package yyy; public class A { ... private class B { ... } }"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 06-08-2010, 01:23 AM #9
Member
- Join Date
- Jun 2010
- Posts
- 6
- Rep Power
- 0
Yes they are and yes it does.
From the above example, the one with ActionListener, would I just throw the public void from my public class AirplaneChange in post #5 after the listener.method(); ?
- 06-08-2010, 01:37 AM #10
if you put listener.method() before the void method, wouldn't it look like this?
Java Code:package yyy; public class A { ActionListener listener = new ActionListener(); //discouraged. listener.method(); //Pretty sure this will give you java.lang.Error Exception public void publicMethod() { ... } private class ActionListener { public void method() { ... } } }"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 06-08-2010, 01:57 AM #11
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Perhaps you could attach zip file and give us a couple of places to look? I'm not really visualizing your problem at this point. All I know is it has to do with package structure, and that's not really helping me. If for whatever reason you can't attach the code, I'd understand, but it would make things easier :)
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 06-08-2010, 02:07 AM #12
What does "perform that role" mean?I need it to perform that role in the private class within the yyy package
The code in a private class should be able to create new instances of public classes.
Have you tried coding yet? Do you get errors? Given an error, we'd have something to work with. All this theoretical talk is hard to follow. Let's have code.
- 06-08-2010, 03:13 AM #13
Member
- Join Date
- Jun 2010
- Posts
- 6
- Rep Power
- 0
I was hesitant to post code but I suppose I can.
Here you go... this was package yyy in my first post.
Java Code:package com.ryan.android.cellsafe; import android.app.Activity; import android.os.Bundle; import android.provider.Settings; import android.provider.Settings.System; import android.widget.TextView; import android.content.Context; import android.content.ContentResolver; import android.content.Intent; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; public class CellSafe extends Activity { private TextView tv; private LocationManager lm; private LocationListener ll; double mySpeed, maxSpeed, limitSpeed; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); tv = new TextView(this); setContentView(tv); maxSpeed = mySpeed = 0; limitSpeed = 7.8232; lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); ll = new SpeedoActionListener(); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll); } private class SpeedoActionListener implements LocationListener { @Override public void onLocationChanged(Location location) { if(location!=null){ if(location.hasSpeed()){ mySpeed = location.getSpeed(); if(mySpeed>maxSpeed) maxSpeed = mySpeed; tv.setText("\nCurrent speed: " + mySpeed*2.2369 + " mph, Max speed: " + maxSpeed*2.2369 + " mph"); if(mySpeed>=limitSpeed){ //***AREA IN QUESTION*** } } } } @Override public void onProviderDisabled(String provider) { //TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { //TODO Auto-generated method stub } @Override public void onStatusChanged(String provider, int status, Bundle extras) { //TODO Auto-generated method stub } } }
And this is package zzz
Java Code:package com.ryan.android.airplanechange; import android.provider.Settings; import android.app.Activity; import android.content.Context; public class AirplaneChange extends Activity { public void method() { Context context = getApplicationContext(); boolean isEnabled = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1; Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1); } }
As you can see, I've marked the area in question in the CellSafe package. AirplaneChange switches Airplane Mode on. I need that to happen when mySpeed is greater than or equal to limitSpeed.
- 06-08-2010, 04:17 AM #14
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Right. That really does help. I thought it was what you meant, but it would have been just as hard to explain without code as it was to understand it (and I still wasn't sure what you meant originally - code helps)
At the beginning of the CellSafe method, with the imports, (preferably before or after) add an import for com.ryan.android.airplanechange.AirplaneChange. At the area in question, just use AirplaneChange as you would any other class. In code:
Hopefully this helps.Java Code://import import com.ryan.android.airplanechange.AirplaneChange; //at area in question AirplaneChange airChange = new AirplaneChange(); //register airChange with whatever you need to //I don't know android, but judging from the interface name (Activity) //you'd queue it with something to be run ASAP
Singing Boyo
P.S. Change the package name. com.ryan.android.airplanechange is just unwieldy and long... find an abbreviation, or a different package name based on other contents. If it's the only class in the package, you may want to move it.If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 06-08-2010, 06:13 AM #15
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Just one more piece of advice, if you use some package only once, instead of bloating up your imports, you can use it directly:
Functionallity is of course the same as just importing the Scanner class, it's only a matter of style and preference.Java Code://some method that needs keyboard input void someMethod() { java.util.Scanner sc = new java.util.Scanner(System.in); }Ever seen a dog chase its tail? Now that's an infinite loop.
Similar Threads
-
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
Question
By Java_Fanatic in forum New To JavaReplies: 10Last Post: 10-13-2009, 11:18 PM -
question
By zizou147 in forum New To JavaReplies: 13Last Post: 07-04-2008, 07:05 PM -
Question
By ayoood in forum New To JavaReplies: 16Last Post: 05-21-2008, 02:23 PM -
question about rmi
By leonard in forum New To JavaReplies: 1Last Post: 08-06-2007, 04:19 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks