我尝试使用具有某些业务逻辑的片段,但是当它启动时,出现以下错误
片段必须是公共静态类,才能从中正确重新创建实例状态
我发现了一些类似的问题,但是由于我没有嵌套/异常类,我无法将其设为静态(或者我缺少某些东西?),因此答案对我的情况没有帮助。
我该如何解决这个问题?
...
class OverviewFragment extends Fragment {
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager layoutManager;
public OverviewFragment(){
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
loadRecords();
return inflater.inflate(R.layout.fragment_overview, container, false);
}
public void drawCategoryChart(Map<String,Integer> categories) {
...
}
public void drawUserChart(...) {
...
}
public void loadRecords(){
List<Record> records =
AppDatabase.getAppDatabase(getActivity().getApplicationContext()).recordDao().getAll();
...
RecyclerView recyclerView = (RecyclerView) getView().findViewById(R.id.spendingOverview);
layoutManager = new LinearLayoutManager(this.getActivity().getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
mAdapter = new RecordOverviewRecycleListAdapter(records);
recyclerView.setAdapter(mAdapter);
}
// Returns Map of all categories with their respective sums of all transactions
public Map<String,Integer> getCategorySums(List<Record> records){
Map<String,Integer> catSums = new HashMap<String, Integer>();
for (Record r : records){
if (!catSums.containsKey(r.getCategory())){
catSums.put(r.getCategory(),r.getAmount());
} else {
catSums.put(r.getCategory(),catSums.get(r.getCategory())+r.getAmount());
}
}
return catSums;
}
}
您的顶级文件在Android Framework中不可见,无法由FragmentManager
管理。
class OverviewFragment extends Fragment
您需要将自己的课程设为public。没有软件包默认设置,因为您现在拥有它。接下来应该定义您的片段。
public class OverviewFragment extends Fragment