This question already has answers here:
Can I use implements and extends at the same time?
(3 answers)
Closed 2 years ago.
So i have public class GPSTracker extends Activity { .. } and after that public final class GPSTracker implements LocationListener . the second one i found in a tutorial, the whole location tracking process happens here, but as far as i understand, i need to include onCreateOptionsMenu and onCreate methods in order to make it work. The question is; How do i put these two together?
here is the full code, so that you can analyze better.
Any help would be much appreciated. Thanks in advance.
Cou can combine them as following:
public class GPSTracker extends Activity implements LocationListener {
}
and put all your code of both classes into this one. The onCreatOptionsMenu is only necessary if you're using a menu. The onCreate should be used because your GPSTracker class is an Activity.
You can not have two public classes in same file but you may include another public class within one.
public class A{
public class B{
//some code
}
}
OR
You can have following implementation.
public class GPSTracker extends Activity implements LocationListener{}
Related
This Question i specific to Android and the use of a Base Activity that can be extended by all other Activities. In general w.r.t Java i understand the use and functioning of abstract classes. I my case i have a Base Activity with some common constants ,functions and implements some Interfaces. My Question is should it be abstract or can i leave it as a normal Activity. Is there a specific reason for it to be abstract.
//Standard BaseActivity
public abstract class BaseActivity extends Activity {
//common code
}
//Usage
public class MyActivity extends BaseActivity {
}
This also seems to work if i defined BaseActivity as follows
//Non Abstract Class
public class BaseActivity extends Activity {
}
Hence the question is there a specific reason to use abstract.
In both cases i dont define the BaseActivity in Manifest.
You can refer the following way for defining a BaseActivity.
It’s always been a good idea to make a baseActivity in android
project.
What, I am suggesting is to create an activity from android.app.Activity called baseActivity and make the 3 custom activity to extend from baseActivity.
For example,
public abstract class MyAppBaseActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
And your custom activity
public class MyCustomActivity extends MyAppBaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Notice that, MyAppBaseActivity is an abstract class. The reason is, we are not going to instantiate it. That’s why you don’t need to add the MyAppBaseActivity class in AndroidManifest.xml file.
Now the question is, why would you do that? Means, why you need to
make an baseActivity class and then make your other activities as a
subclass of it?
Well, here are few very generic reasons
1. You can avoid code duplication of you application wise common ui tasks. Suppose you want to show progressDialog in different activities. Just write code to show a progressDialog in your baseActivity and call that method from other activity.
2. Your application menu should be consistent across different application. For example, you have a settings screen. Option menu is containing the navigation to settings screen. You should display the settings throughout the application, means regardless of activity. User want to access the settings screen from anywhere in application. This feature can be easily achieved, but you have to override onCreateOptionsMenu in each of your activity or, you can override it once in your baseActivity.
I am new to Android and I know that we can not extends multiple classes. Please tell me how to do that? Please Correct my code.
public class Tab1 extends Fragment,ListActivity {
}
You cannot extend multiple classes in Activity or Fragment,
Like this
public class MainActivity extends AppCompatActivity, baseClass{}
So what you can do is to create a new java BaseClass file that extends AppCompatActivity. The MainActivity will extent only BaseClass, Like this
public class MainActivity extends BaseClass{} //mainactivity extents baseclass
public class BaseClass extends AppCompatActivity{}//baseclass extents appcompatactivity
The advantage is that you can implement some base functions in the BaseClass, such as Toast, checking for internet connection, etc.
Please checkout the images in case of Activity.
MainActivity extents BaseClass,
BaseClass extents AppCompatActivity
In Case of Fragments
MyFragment extents BaseFragment,
BaseFragment extents Fragment
I hope it will be helpfull, Thank you.
The short answer is that you can't. Java doesn't allow for multiple inheritance. What you can do is implement multiple interfaces. So if you want to make a Fragment that has a ListView in it you can do something like this:
public class Tab1 extends Fragment implements BaseAdapter {
}
Unfortunately JAVA does not allowed you to extends multiple classes into one class. you can use Inner class or make your 2nd class a Interface you can include multiple interface but not multiple class. Inner class would be more suitable.
the clip of the code written for using voice interaction which is used in another Java file I have already tried importing android.app.VoiceInteractor but its not working
You may have imported all necessary classes in your code. As your snippet you forgot to extend that, But you are already extending AppCompatActivity so that you can't extend another class in the same Activity. What you need to do is create another class inside MyVoiceActivity and extend VoiceInteractor.ConfirmationRequest in that class.
Your Activity will look something like this
public class MyVoiceActivity extends AppCompatActivity{
class Confirm extends VoiceInteractor.ConfirmationRequest {
//All method you wanted goes here
}
}
I'm creating an AndEngine game for Android in eclipse & java. I created my own activity class and added it to application attributes in my Android manifest. But it said "com.myname.projectname.Activity does not extend android.app.Application". It extends SimpleBaseGameActivity but I heard that it should work well in this status.
What may the problem be? And how can I solve this?
I think in your project , there is some Application_class and inside another class you are using , Application_class instance in your Activity , and that Application_class is not extending Application class .
public class ApplicationClass extends Application
{
public static Context appContext;
}
I just want to know if I am supposed to extend every class with the following:
public class [name goes here] extends Activity
first, let me explain. below you will see an example of 4 classes I have in my project (of so many!). The Main class of course has to extend Activity. Well, within the main class are buttons. One button is for the location. I have Location extends Activity. Now, should this attend Activity, or should it extend Main? What about LocationA? Should this extend Location or Main?
public class Main extends Activity{
public class Location extends Activity{
public class LocationA extends Activity{
public class LocationB extends Activity{
Thanks for ALL of your help!
Unless you want to share a common functionality between all your activities, All your activities should extend Activity class