简体   繁体   English

无法覆盖 Fragment 中的 onResume() 尝试分配较弱的访问权限; 是公开的

[英]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.

相关问题 试图分配较弱的访问权限; 是isCancelled的公开错误 - attempting to assign weaker access privileges; was public error for isCancelled 泛型问题:clone()尝试分配较弱的访问权限 - Generics issue: clone() attempting to assign weaker access privileges Android AsyncTask错误,尝试分配较弱的访问权限。 - Android AsyncTask error, attempting to assign weaker access privileges. Android Studio:房间数据库实施时出现“尝试分配较弱的访问权限”错误 - Android Studio: "attempting to assign weaker access privileges" error on Room Database implementation java试图分配较弱的访问权限错误 - java attempting to assign weaker access privilege error 为什么java不允许为子类中的静态方法分配较弱的访问权限? - Why doesn't java allow to assign weaker access privileges to static methods in a child class? Java:“试图分配较弱的访问特权错误”(使用1.8编译JDK 1.6 Source) - Java: “attempting to assign weaker access privilege error” (Compiling JDK 1.6 Source with 1.8) 分配对从超类覆盖的方法的较弱访问 - Assign weaker access to method overriding from superclass 无法在Fragment的onResume()方法中检索View - Cannot retrieve View in onResume() method inside Fragment 无法在Android Studio 中获取所有@override 函数,例如onStart、onResume - Cannot get all the @override functions such as onStart, onResume in Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM