android 方法不覆盖其超类中的方法

w8f9ii69  于 2022-12-28  发布在  Android
关注(0)|答案(2)|浏览(160)

我试图设置我的操作栏的标题,但是它给了我错误信息 “方法不覆盖其超类中的方法”
以下是我有问题的部分:

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActivity().getActionBar().setTitle(mTitle);
}

下面是整个类的代码(不完整,因为我还在写):

package com.android.styles.radius360;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.Menu;

/**
 * Created by user on 3/23/2017.
 */

public class HomePageFragment extends Fragment {

    private String[] mOptions;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private CharSequence mTitle;

    private ActionBarDrawerToggle mDrawerToggle;
    private CharSequence mDrawerTitle;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.home_page, container, false);

        mOptions = getResources().getStringArray(R.array.options_array);
        mDrawerLayout = (DrawerLayout) v.findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) v.findViewById(R.id.left_drawer);

        // Set the adapter for the list view
        mDrawerList.setAdapter(new ArrayAdapter<String>(getActivity(),
                R.layout.drawer_list_item, mOptions));
        // Set the list's click listener
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        mTitle = mDrawerTitle = getActivity().getTitle();
        mDrawerLayout = (DrawerLayout) v.findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout,
                R.string.drawer_open, R.string.drawer_close) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getActivity().getActionBar().setTitle(mTitle);
                getActivity().invalidateOptionsMenu(); // Creates call to onPrepareOptionsMenu()
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getActivity().getActionBar().setTitle(mDrawerTitle);
                getActivity().invalidateOptionsMenu(); // Creates call to onPrepareOptionsMenu()
            }
        };

        // Set the drawer toggle as the DrawerListener
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        return v;
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        super.onPrepareOptionsMenu(menu);
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActivity().getActionBar().setTitle(mTitle);
    }

    /* The click listner for ListView in the navigation drawer */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    }

    /** Swaps fragments in the main content view */
    private void selectItem(int position) {
        // Create a new fragment and specify the planet to show based on position
        if(position == 0)
        {
            FragmentManager fragmentManager = getFragmentManager();

            SettingsPageActivity settings = new SettingsPageActivity();
            Fragment fragment = settings.createFragment();
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame, fragment)
                    .commit();

        }
        // Insert the fragment by replacing any existing fragment

        // Highlight the selected item, update the title, and close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerLayout.closeDrawer(mDrawerList);
    }

}
tnkciper

tnkciper1#

Fragment或它的任何超类似乎都没有public void setTitle(CharSequence title)方法,所以你不能使用@Override注解,你可以删除那个注解,这样就可以了。

dtcbnfnu

dtcbnfnu2#

用户michaeak的回答是正确的,但我只是补充一下:检查重写方法的拼写。在下面的例子中,我得到了同样的错误,因为'OnPause()'应该是'onPause'加上一个小写的'o':

@Override
protected void OnPause() { // OnPause should be onPause. It gets rid of the error
    super.onPause();
    mFirebaseAuth.removeAuthStateListener(mAuthStateListener);
}

相关问题