[英]Cannot override onResume() in Fragment attempting to assign weaker access privileges; was public
Can someone help me, I got this error in android studio:有人可以帮我吗,我在 android 工作室遇到了这个错误:
onResume() in JournalFragment cannot override onResume() in Fragment attempting to assign weaker access privileges;
JournalFragment 中的 onResume() 无法覆盖 Fragment 中的 onResume() 尝试分配较弱的访问权限; was public
是公开的
The code below is the JournalFragment.java:下面的代码是 JournalFragment.java:
public class JournalFragment extends Fragment {公共 class JournalFragment 扩展片段 {
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.在 Java inheritance 中,您不能向下更改访问修饰符。 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.
让我们看一个例子:你有一个基础 class,其中一些 function 是公共的,而外部的其他人试图激活 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.
但是等等,动态类型是您的 class,并且您更改了访问修饰符,因此外部人员无法使用 function。 The user experience something he didn't anticipate as he knew he can use the function.
用户体验到了他没有预料到的事情,因为他知道他可以使用 function。 That's the reason Java restrict this kind of behavior and your solution is to change onResume back to public.
这就是 Java 限制这种行为的原因,您的解决方案是将 onResume 改回 public。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.