【问题标题】:Cannot override onResume() in Fragment attempting to assign weaker access privileges; was public无法覆盖 Fragment 中的 onResume() 尝试分配较弱的访问权限;是公开的
【发布时间】:2021-05-24 15:37:33
【问题描述】:

谁能帮帮我,我在 android studio 中遇到了这个错误:

JournalFragment 中的 onResume() 不能覆盖 Fragment 中的 onResume() 试图分配较弱的访问权限;是公开的

下面的代码是 JournalFragment.java:

公共类 JournalFragment 扩展 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);
    }
}

}'

提前谢谢你!

【问题讨论】:

    标签: java android


    【解决方案1】:

    在 Java 继承中,您不能向下更改访问修饰符。 让我们看一个例子: 您有一个基类,其中某些功能是公共的,而外部的其他人试图激活该功能。但是等等,动态类型是你的类,你改变了访问修饰符,所以外部的人不能使用这个函数。用户体验到了他没有预料到的东西,因为他知道他可以使用该功能。这就是 Java 限制这种行为的原因,您的解决方案是将 onResume 改回 public。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多