简体   繁体   中英

Cannot override onResume() in Fragment attempting to assign weaker access privileges; was public

Can someone help me, I got this error in android studio:

onResume() in JournalFragment cannot override onResume() in Fragment attempting to assign weaker access privileges; was public

The code below is the JournalFragment.java:

public class JournalFragment extends Fragment {

Toolbar toolbar;
RecyclerView recyclerView;
Adapter adapter;
TextView noItemText;
SimpleDatabase simpleDatabase;
Context context;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    setHasOptionsMenu(true);
    super.onCreate(savedInstanceState);
    View view=inflater.inflate(R.layout.fragment_journal, container, false);



    toolbar = view.findViewById(R.id.toolbar);
    toolbar.setTitleTextColor(getResources().getColor(R.color.white));
    ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
    noItemText = view.findViewById(R.id.noItemText);
    simpleDatabase = new SimpleDatabase(context);
    List<Note> allNotes = simpleDatabase.getAllNotes();
    recyclerView = view.findViewById(R.id.allNotesList);
    context = container.getContext();
    if(allNotes.isEmpty()){
        noItemText.setVisibility(View.VISIBLE);
    }else {
        noItemText.setVisibility(View.GONE);
        displayList(allNotes);
    }
    return view;

}

private void displayList(List<Note> allNotes) {
    recyclerView.setLayoutManager(new LinearLayoutManager(context));
    adapter = new Adapter(context,allNotes);
    recyclerView.setAdapter(adapter);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Add your menu entries here
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.main_menu,menu);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId() == R.id.add){
        Toast.makeText(context, "Add New Note", Toast.LENGTH_SHORT).show();
        Intent i = new Intent(getActivity(), AddNote.class);
        startActivity(i);
    }
    return super.onOptionsItemSelected(item);
}

@Override
protected void onResume() {
    super.onResume();
    List<Note> getAllNotes = simpleDatabase.getAllNotes();
    if(getAllNotes.isEmpty()){
        noItemText.setVisibility(View.VISIBLE);
    }else {
        noItemText.setVisibility(View.GONE);
        displayList(getAllNotes);
    }
}

}'

Thank you in advance!

In Java inheritance you cannot change the access modifiers down. Let's look at an example: You have a base class with some function as public and someone else from outside tries to activate the function. But wait, the dynamic type is your class and you changed down the access modifier so someone from the outside can't use the function. The user experience something he didn't anticipate as he knew he can use the function. That's the reason Java restrict this kind of behavior and your solution is to change onResume back to public.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM