1

I'm working through some android tutorials right now in preparation for a two week camp at American university. I'm seventeen, so excuse me if this is a stupid question. I did some research and tried several things, but I can't get it to work.

I'm working off of the google tab / fragment example found here: http://developer.android.com/reference/android/app/TabActivity.html

My problem is when overriding createTabContent, and onTabChanged I keep getting the error must override a superclass method. Here's my code:

package matthews.zack.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;

public class _testActivity extends FragmentActivity {

ListView list;
Button save;
RadioGroup radioGroup;
RadioButton selectedType;
TabHost tabHost;
// People p = new People();
public List<People> people = new ArrayList<People>();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    onInitialize();

    if (savedInstanceState != null) {
        tabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
    }
}

private void onInitialize() {

    tabHost = (TabHost) findViewById(R.id.tabHost);
    tabHost.setup();

     * TabManager tManager = new TabManager(this, tabHost,
     * R.id.realtabcontent); tManager.addTab(
     * tabHost.newTabSpec("details").setIndicator("Details",
     * getResources().getDrawable(R.drawable.goldstar)), null, null);
     * tManager.addTab( tabHost.newTabSpec("list").setIndicator("List",
     * getResources().getDrawable(R.drawable.bluestar)), null, null);
     *

     * TabSpec spec = tabHost.newTabSpec("tab1");
     * spec.setContent(R.id.listView1); spec.setIndicator("List",
     * getResources().getDrawable(R.drawable.goldstar));
     * 
     * TabSpec spec2 = tabHost.newTabSpec("tab2");
     * spec2.setContent(R.id.details); spec2.setIndicator("Details",
     * getResources().getDrawable(R.drawable.bluestar));
     * 
     * tabHost.addTab(spec); tabHost.addTab(spec2);
     */

    PeopleAdapter adapter = new PeopleAdapter();
    list = (ListView) findViewById(R.id.listView1);
    list.setAdapter(adapter);

    radioGroup = (RadioGroup) findViewById(R.id.type);
    // radioGroup.check(R.id.coWorker);
    radioGroup
            .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    // TODO Auto-generated method stub

                    if (selectedType != (RadioButton) findViewById(radioGroup
                            .getCheckedRadioButtonId())) {
                        selectedType = (RadioButton) findViewById(radioGroup
                                .getCheckedRadioButtonId());
                    }
                }
            });

    save = (Button) findViewById(R.id.button1);
    save.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            People p = new People();

            // TODO Auto-generated method stub
            EditText firstName = (EditText) findViewById(R.id.editText1);
            EditText lastName = (EditText) findViewById(R.id.editText2);
            p.setFirstName(firstName.getText().toString());// firstName.getText().toString());
            p.setLastName(lastName.getText().toString());// lastName.getText().toString());
            p.setType(selectedType.getText().toString());

            people.add(p);
        }
    });
}

static class PeopleHolder {
    private TextView name;
    private TextView title;
    private ImageView icon;

    PeopleHolder(View row) {
        name = (TextView) row.findViewById(R.id.name);
        title = (TextView) row.findViewById(R.id.title);
        icon = (ImageView) row.findViewById(R.id.icon);
    }

    void populateForm(People p) {
        name.setText(p.getFirstName() + " " + p.getLastName());
        title.setText(p.getType().toString());
        if (p.getType().equals("Family")) {
            icon.setImageResource(R.drawable.android);

        }

        else if (p.getType().equals("Friend")) {
            icon.setImageResource(R.drawable.xbox);
        }

        else {
            icon.setImageResource(R.drawable.yinyang);
        }
    }

}

class PeopleAdapter extends ArrayAdapter<People> {

    PeopleAdapter() {
        super(_testActivity.this, android.R.layout.simple_list_item_1,
                people);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        PeopleHolder holder = null;
        if (row == null) {
            LayoutInflater inflater = getLayoutInflater();
            row = inflater.inflate(R.layout.row, null);
            holder = new PeopleHolder(row);
            row.setTag(holder);
        }

        else {
            holder = (PeopleHolder) row.getTag();
        }

        holder.populateForm(people.get(position));

        return (row);
    }

}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("tab", tabHost.getCurrentTabTag());
}

public static class TabManager implements TabHost.OnTabChangeListener {
    private final FragmentActivity activity;
    private final TabHost host;
    private final int containerID;
    private final HashMap<String, TabInfo> tabs = new HashMap<String, TabInfo>();
    TabInfo lastTab;

    static final class TabInfo {
        private final String tag;
        private final Class<?> clss;
        private final Bundle args;
        private Fragment fragment;

        TabInfo(String _tag, Class<?> clss, Bundle args) {
            this.tag = _tag;
            this.clss = clss;
            this.args = args;
        }
    }

    static class DummyTabFactory implements TabHost.TabContentFactory {
        private final Context context;

        public DummyTabFactory(Context context) {
            this.context = context;
        }

        public View createTabContent(String tag) {
            View v = new View(context);
            v.setMinimumWidth(0);
            v.setMinimumHeight(0);

            return v;
        }
    }

    public TabManager(FragmentActivity activity, TabHost host,
            int containerID) {
        this.activity = activity;
        this.host = host;
        this.containerID = containerID;
        this.host.setOnTabChangedListener(this);
    }

    public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
        tabSpec.setContent(new DummyTabFactory(activity));
        String tag = tabSpec.getTag();

        TabInfo info = new TabInfo(tag, clss, args);

        info.fragment = activity.getSupportFragmentManager()
                .findFragmentByTag(tag);

        if (info.fragment != null && !info.fragment.isDetached()) {
            FragmentTransaction ft = activity.getSupportFragmentManager()
                    .beginTransaction();
            ft.detach(info.fragment);
            ft.commit();
        }

        tabs.put(tag, info);
        host.addTab(tabSpec);
    }

    public void onTabChanged(String tabId) {
        TabInfo newTab = tabs.get(tabId);
        if (lastTab != newTab) {
            FragmentTransaction ft = activity.getSupportFragmentManager()
                    .beginTransaction();
            if (lastTab != null) {
                if (lastTab.fragment != null) {
                    ft.detach(lastTab.fragment);
                }

            }

            if (newTab != null) {
                if (newTab.fragment == null) {
                    newTab.fragment =       Fragment.instantiate(activity,
                            newTab.clss.getName(), newTab.args);
                    ft.add(containerID, newTab.fragment, newTab.tag);

                }

                else {

                    ft.attach(newTab.fragment);
                }

            }

            lastTab = newTab;
            ft.commit();
            activity.getSupportFragmentManager()
                    .executePendingTransactions();

        }

        // TODO Auto-generated method stub

    }
}

}

Is this an eclipse problem, or did I make a careless mistake? I checked and double checked that I was using the proper classes and that they contained the methods I'm trying to override. Thanks in advance!

EDIT: Full code posted. I'm able to compile and run, but the app force closes as soon as it's launched. Please help!

Goo
  • 1,308
  • 1
  • 13
  • 29
Zack_074
  • 157
  • 1
  • 5
  • 12

4 Answers4

7

You're probably using a different Java version than the author of your example code... You have two choices:

  1. You should be able to remove the @Override lines causing the errors and run your app without any trouble.

  2. You can change your JDK Compiler's compliance level from 1.5 to 1.6 in Eclipse:

    Properties -> JDK Compiler -> Select "1.6" from the Compiler compliance level dropdown (Android only supports 1.5 and 1.6, it does not yet support 1.7)

Sam
  • 84,460
  • 18
  • 172
  • 171
0

Without seeing all the code of your class, it's hard to say what you problem is exactly.

Like Sam said, depending on the examples you are using, you may not need to use @Override. If you class doesn't "extend" anything like:

public class MyClass extends MySuperClass{...

Then you don't need the over ride. Overriding is done when you have a super class that you are inheriting methods from, and you want to change the way they function. Think of it as customizing methods that are stored in the class you are extending. If you don't have a super class, you don't need to override the methods, which may be causing your problem.

The other issue might be that you aren't overriding the correct methods within the super class.

But without seeing all the code, it is hard to give you something more specific.

Hope this helps.

BlackHatSamurai
  • 21,845
  • 20
  • 84
  • 147
  • I posted my full code, can you help me a little better? I'm getting a force close now. – Zack_074 Jul 05 '12 at 23:47
  • One things I've found with overriding in Eclipse, is that if you get rid of the override annotation, the do a Project > clean; it will give you an error saying "must override method..." from there you can hover over the error lines and you will get an option to have the IDE import the methods with the override annotation. That might work. – BlackHatSamurai Jul 06 '12 at 00:01
0

Older versions of Java don't like the use of @Override for Interface method overrides. Back then only Super Class methods were meant to be used with that annotation. This is changed after 1.5, so 1.6 and 1.7 don't throw warnings for @Override for overriding an Interface method.

You can either suppress this warning, remove the annotation (this doesn't hurt you at all, but if you type the method name or argument type wrong, Eclipse won't tell you), or you can change to 1.6 or higher.

CSAntol
  • 74
  • 5
  • Suppressing warnings is poor coding. It's like driving down the road with your check engine light on. Warnings are there for a reason. It would make more sense to upgrade your version of Java, than to just suppress the warnings. – BlackHatSamurai Jul 06 '12 at 00:14
  • 2
    The warning I am talking about is not there for a reason. It's a deprecated warning. Please do some research before voting down my response. You can see here that the ability to use the @Override annotation for interfaces was only added in 1.6 http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why It's not a real warning. Although, my post may not solve the OP's question, but that doesn't make it any less valid. – CSAntol Jul 06 '12 at 17:14
-1

I had a similar problem some time ago and solved it by changing the project's compilance level to 1.6.

To do this:

  • Right Click on the project folder (can be found in Eclipse's Project Explorer, left side)
  • Properties
  • Java Compiler
  • Uncheck "Enable project specific settings
  • Configure Workspace Settings...
  • And then change the compilance level from 1.5 to 1.6
Sergio Carneiro
  • 3,402
  • 3
  • 32
  • 50
  • 1
    I don't think that this will solve the problem. When you do something like changing the compliance level, you might be creating more problems than you are solving. Before anything is done, you should see all the code and make sure that there is something else. We are talking about an issue with over-riding a superclass method. He may not even have a super class, so why would he want to change the compliance level? – BlackHatSamurai Jul 05 '12 at 23:32
  • 1
    When I had this problem I read a topic somewhere and it said to change the compilance level to 1.6 because 1.5 had a problem with overrides. And it works perfectly after that. – Sergio Carneiro Jul 05 '12 at 23:37
  • Just because it works doesn't mean it's right. This could be compared to suppressing warnings. Every time you have an issue with an over ride, you don't just want to start changing your compliance level. There is usually a good reason why you are getting the warnings, and should try and figure out what they are before you go and start adjusting your settings. Ultimately it amounts to poor coding and should be avoided if possible. – BlackHatSamurai Jul 05 '12 at 23:40
  • It is not a solution to the error. It is just a measure or a solution out of luck. It sounds like designing an application for tablets but then changing its compatibility to phones when it does not work for tablets, instead of finding the real source of the issue. – Erol Jul 06 '12 at 01:31