我在recyclerView
中放置了fragment
,但似乎看不出错误在哪里。我尝试了许多修复程序,但该应用程序始终崩溃。
有人可以帮助我让recyclerView
显示在fragment
上吗?
我的Java文件代码
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
((MainActivity) getActivity()).setTitle("VIEW ALL MODULES");
recyclerView = (RecyclerView) getActivity().findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
moduleList = new ArrayList<>();
Module m1 = new Module(1, 1, 2, 3, 4, "ONT4101", "DEV SOFT", "NMU", "ITC", "Haskins");
Module m2 = new Module(2, 1, 5, 37, 44, "AIN4002", "AI", "NMU", "ITC", "THUG LIFE");
Module m3 = new Module(3, 2, 2, 13, 44, "ONT4300", "PROJECT", "NMU", "ITC", "Haskins");
moduleList.add(m1);
moduleList.add(m2);
moduleList.add(m3);
moduleAdapter = new ModuleAdapter(getActivity(), moduleList);
recyclerView.setAdapter(moduleAdapter);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_view_all_module, container, false);
}
XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewAllModuleFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
适配器类
public class ModuleAdapter extends RecyclerView.Adapter<ModuleAdapter.ModuleViewHolder>{
private Context mCtx;
private List<Module> moduleList;
public ModuleAdapter(Context mCtx, List<Module> moduleList) {
this.mCtx = mCtx;
this.moduleList = moduleList;
}
@NonNull
@Override
public ModuleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.from(parent.getContext())
.inflate(R.layout.module_list, parent, false);
return new ModuleViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ModuleViewHolder holder, int position) {
Module module = moduleList.get(position);
holder.textModCodeView.setText(module.getModcode());
holder.textModuleDescView.setText(module.getModdesc());
holder.textEducatorView.setText("Educator: "+ module.getEducator());
holder.textNoOfTestView.setText("Tests: "+ String.valueOf(module.getNooftest()));
holder.textNoOfAssView.setText("Assignments: "+ String.valueOf(module.getNoofass()));
holder.textNoOfPracView.setText("Practicals: "+ String.valueOf(module.getNoofprac()));
holder.textNoOfExamView.setText("Exams: "+ String.valueOf(module.getNoofexam()));
Random r = new Random();
int red=r.nextInt(255 - 0 + 1)+0;
int green=r.nextInt(255 - 0 + 1)+0;
int blue=r.nextInt(255 - 0 + 1)+0;
GradientDrawable draw = new GradientDrawable();
draw.setShape(GradientDrawable.RECTANGLE);
draw.setColor(Color.rgb(red,green,blue));
holder.textModCodeView.setBackground(draw);
}
@Override
public int getItemCount() {
return moduleList.size();
}
class ModuleViewHolder extends RecyclerView.ViewHolder{
TextView textModCodeView,textModuleDescView,textEducatorView,textNoOfTestView,textNoOfAssView,textNoOfPracView,textNoOfExamView;
public ModuleViewHolder(View itemView) {
super(itemView);
textModCodeView = itemView.findViewById(R.id.textModCodeView);
textModuleDescView = itemView.findViewById(R.id.textModuleDescView);
textEducatorView = itemView.findViewById(R.id.textEducatorView);
textNoOfTestView = itemView.findViewById(R.id.textNoOfTestView);
textNoOfAssView = itemView.findViewById(R.id.textNoOfAssView);
textNoOfPracView = itemView.findViewById(R.id.textNoOfPracView);
textNoOfExamView = itemView.findViewById(R.id.textNoOfExamView);
}
}
}
我已经导入了RecyclerView
和CardView
的依赖项。
请帮忙。
答案 0 :(得分:1)
此行从您的活动中获取“回收者”视图。我认为那不是你的意图。
recyclerView = (RecyclerView) getActivity().findViewById(R.id.recyclerView);
如果您确实在片段中定义了回收站视图,则应该从该片段中放大该布局,然后从那里找到recyclerview。
将片段中的视图放大并找到recyclerview的正确方法是public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_view_all_module, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
return rootView;
}
答案 1 :(得分:1)
此行有错误,使您的应用崩溃:
recyclerView = (RecyclerView) getActivity().findViewById(R.id.recyclerView);
由于回收者视图不是MainActivity的一部分,因此在MainAcitvity的上下文中找到它会导致崩溃。您将需要从片段的XML文件中找到“膨胀的视图”,如下所示:
View view = inflater.inflate(R.layout.fragment_view_all_module, container, false);
recyclerView = view.findViewById(R.id.recyclerView);
然后在“回收者视图”上设置适配器和布局管理器。
一个建议:由于您的片段布局仅由一个Recycler View组成,因此您应该考虑使用FrameLayout作为该Recycler View的容器而不是LinearLayout,尽管这与崩溃无关,但是如果您打算仅添加RecyclerView(即单个项目),那么FrameLayout是一个更好的选择。
答案 2 :(得分:1)
首先,您需要填充片段的布局,然后初始化RecyclerView
。试试这个
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
((MainActivity) getActivity()).setTitle("VIEW ALL MODULES");
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_view_all_module, container, false);
recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
moduleList = new ArrayList<>();
Module m1 = new Module(1, 1, 2, 3, 4, "ONT4101", "DEV SOFT", "NMU", "ITC", "Haskins");
Module m2 = new Module(2, 1, 5, 37, 44, "AIN4002", "AI", "NMU", "ITC", "THUG LIFE");
Module m3 = new Module(3, 2, 2, 13, 44, "ONT4300", "PROJECT", "NMU", "ITC", "Haskins");
moduleList.add(m1);
moduleList.add(m2);
moduleList.add(m3);
moduleAdapter = new ModuleAdapter(getActivity(), moduleList);
recyclerView.setAdapter(moduleAdapter);
return view;
}
希望这会有所帮助。
答案 3 :(得分:0)
implementation 'com.google.android.material:material:1.2.1' // 在应用级 gradle 中使用这个