-2

So... I use this public class right now and it works fine (MainActivity.java) For my tabs in my app. But I want to do something with the tabs and need to use buttons and textViews and other stuff. But I cant because I need to use two extends and two implements for them both to work.

public class MainActivity extends ActionBarActivity implements TabListener 

How it should look/work, how it need to look and work

public class MainActivity extends ActionBarActivity, Activity implements TabListener, OnClickListener

Any fix or knowledge in how to fix this? (I use Android Eclipse)

Gold Toazt
  • 3
  • 1
  • 4
  • You can't extends several classes directly. You can do `A extends B` and the `B extends C` if you have to. However, implementing multiple interfaces shouldn't pose any problems. – AntonH May 05 '14 at 20:18
  • You can do A extends B and the B extends C Could you do that with my example I don't really understand how and what you mean with that? Also does it work wit "implements" too? – Gold Toazt May 05 '14 at 20:29
  • `public class MATemp extends Actionbar {...}`, then `public class MainActivity extends MATemp implements TabListener, OnClickListener {...}`. If you want to make sure that MATemp won't be instantiated, make it abstract with `public abstract class MATemp ...`. This is basic Java, so you probably should go and learn about inheritance from a book or tutorial before going further. – AntonH May 05 '14 at 20:30
  • All I have been doing is watching tutorials ^-^ Started coding about 5-6 days ago :p I think I got it so what your saying is that: `public abstract class MATemp extends ActionbarActivity public class MainActivity extends MATemp implements TabListener, OnClickListener` – Gold Toazt May 05 '14 at 20:54
  • Yes, except **you can't do it in one go**. You have to do it separately, which is why I put them as separate in my examples. – AntonH May 05 '14 at 20:56
  • `public abstract class MATemp extends ActionbarActivity public class MainActivity extends MATemp implements TabListener, OnClickListener` Right? – Gold Toazt May 06 '14 at 09:33
  • As long as `MATemp` and `MainActivity` are 2 seperate classes, looks good. – AntonH May 06 '14 at 12:12
  • Nop, cant get it working :/ – Gold Toazt May 06 '14 at 17:06
  • Did you create 2 different classes, extending the appropriate classes? If yes, open another question, post your code and error message/reason why it isn't working. – AntonH May 06 '14 at 20:46

2 Answers2

0

See http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html section "Abstract Classes Compared to Interfaces".

If you want to define behaviour that multiple classes can implement, the most common approach is to use an interface.

jordan
  • 959
  • 7
  • 17
0

Your MainActivity is a kind of Activity, but the activity is not an ActionBar. It may have an ActionBar or make use of one, but it is not an ActionBar. Therefore, trying to get the MainActivity class to extend ActionBar is the wrong concept.

I'm not sure what you want to do (I personally haven't tried using the ActionBar class directly). If you just want your activity to do stuff with the action bar, just declare a variable of type ActionBar:

ActionBar actionBar;

and assign it to getActionBar() or getSupportActionBar() when you need it. If you really want to add functionality to the ActionBar provided by Android, maybe you could define a class that extends ActionBar, but I don't know how you'd tell Android to use your new class for the action bar it sets up. See this tutorial.

EDIT: After I posted this answer, the question was edited, so that your class now extends ActionBarActivity:

public class MainActivity extends ActionBarActivity, Activity implements TabListener, OnClickListener

That's different. However, ActionBarActivity already extends Activity, therefore you don't need to put Activity in the above class definition. It will automatically extend Activity.

ajb
  • 31,309
  • 3
  • 58
  • 84
  • I think you misunderstood the question, I do have all those things are you mentioning for my tabs. The problem is that I also need extends Activity and implements Tablistener. For a save button – Gold Toazt May 05 '14 at 21:12
  • @GoldToazt It looks like you changed the question after I answered it. – ajb May 05 '14 at 21:29