方法必须覆盖超类方法

时间:2012-07-05 23:14:34

标签: android eclipse

我现在正在研究一些Android教程,准备在美国大学进行为期两周的训练营。我十七岁,请原谅我这是一个愚蠢的问题。我做了一些研究并尝试了几件事,但我无法让它发挥作用。

我正在使用此处的google标签/片段示例:http://developer.android.com/reference/android/app/TabActivity.html

我的问题是当重写createTabContent时,onTabChanged我一直得到错误必须覆盖超类方法。这是我的代码:

包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扩展了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

    }
}

}

这是一个日食问题,还是我犯了一个粗心的错误?我检查并仔细检查我是否正在使用正确的类,并且它们包含我试图覆盖的方法。提前谢谢!

编辑:已发布完整代码。我能够编译并运行,但应用程序一旦启动就关闭。请帮忙!

4 个答案:

答案 0 :(得分:7)

您可能使用的Java版本与示例代码的作者不同......您有两种选择:

  1. 您应该能够删除导致错误的@Override行并毫无困难地运行您的应用。

  2. 您可以在Eclipse中将JDK编译器的合规性级别从1.5更改为1.6:

    属性 - &gt; JDK编译器 - &gt;从编译器合规性级别下拉列表中选择“1.6” (Android仅支持1.5和1.6,它还不支持1.7)

答案 1 :(得分:0)

如果没有看到班级的所有代码,很难说出你的问题究竟是什么。

Sam说,根据您使用的示例,您可能不需要使用@Override。如果你的课不“扩展”任何类似的东西:

public class MyClass extends MySuperClass{...

然后你不需要过度骑行。如果您有一个继承方法的超类,并且您想要更改它们的运行方式,则会执行覆盖。可以将其视为存储在您正在扩展的类中的自定义方法。如果您没有超类,则无需覆盖可能导致问题的方法。

另一个问题可能是你没有覆盖超类中的正确方法。

但是没有看到所有的代码,很难给你更具体的东西。

希望这有帮助。

答案 2 :(得分:0)

旧版本的Java不喜欢使用@Override进行接口方法覆盖。当时只有超类方法才能与该注释一起使用。这在1.5之后改变,因此1.6和1.7不会为@Override抛出警告以覆盖接口方法。

你可以抑制这个警告,删除注释(这根本不会伤害你,但如果你输入方法名称或参数类型错误,Eclipse就不会告诉你),或者你可以改为1.6或者更高。

答案 3 :(得分:-1)

前段时间我遇到过类似问题,并通过将项目的编制级别更改为1.6来解决问题。

要做到这一点:

  • 右键单击项目文件夹(可在Eclipse的Project Explorer中找到,左侧)
  • 属性
  • Java编译器
  • 取消选中“启用项目特定设置
  • 配置工作区设置...
  • 然后将编纂等级从1.5更改为1.6