我试图为我的操作栏设置标题,但它给了我错误信息"方法不会覆盖其超类中的方法"。我正在扩展片段。
这是我遇到问题的部分
@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);
}
}
答案 0 :(得分:3)
类Fragment
或其任何超级类似乎没有方法public void setTitle(CharSequence title)
,因此您无法使用@Override
注释。你可以删除那个注释,你应该没事。
答案 1 :(得分:0)
以上答案是正确的,但只需添加一下,检查覆盖方法的拼写即可。在以下情况下,由于出现“ OnPause()”应为“ onPause”,且“ o”较低的情况,因此我遇到了相同的错误
@Override
protected void OnPause(){ // OnPause should be onPause, get rids of the error
super.onPause();
mFirebaseAuth.removeAuthStateListener(mAuthStateListener);
}