java 空指针异常
java NullPointer Exception
目前正在使用 java 和 android studio 开发移动应用程序。 (初学者)我有 3 个片段(ToDo、Doing、Done),顶部有一个 tablayout(ToDo、Ongoing、Finished)和一个带有添加按钮的工具栏。
添加按钮应该创建一个列表,允许用户修改每个list(Recyclerviewer)
的内容
获取错误:java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
下面是截图
代码
public class kanbanManagement extends Fragment {
// TODO: Rename and change types of parameters
TabLayout tabLayout;
private ViewPager2 viewPager2;
private MyFragment adapter;
private RecyclerView recyclerView2;
private com.example.myhouse.recyclerKanbanAdapter recyclerKanbanAdapter;
ArrayList<kanbanItems>listKanban;
Button btn_Add;
public kanbanManagement() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_kanban_management, container, false);
tabLayout=view.findViewById(R.id.tabLayout);
viewPager2=view.findViewById(R.id.viewPager2);
tabLayout.addTab(tabLayout.newTab().setText("To Do"));
tabLayout.addTab(tabLayout.newTab().setText("On going"));
tabLayout.addTab(tabLayout.newTab().setText("Finished"));
FragmentManager fragmentManager = getParentFragmentManager();
adapter = new MyFragment(fragmentManager , getLifecycle());
viewPager2.setAdapter(adapter);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager2.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
tabLayout.selectTab(tabLayout.getTabAt(position));
}
});
recyclerView2=view.findViewById(R.id.taskList);
listKanban= new ArrayList<>();
btn_Add=view.findViewById(R.id.btnAdd);
recyclerKanbanAdapter= new recyclerKanbanAdapter(getActivity(),listKanban);
btn_Add.setOnClickListener(v -> {
recyclerView2.setAdapter(recyclerKanbanAdapter);
});
return view;
}
}
抱歉,我没有完全理解您的代码。因此,无需检查您的完整代码。我的猜测是您没有使用 LayoutManager
来显示您的 listKanban
。所以再次添加 layoutManager
代码和 运行。
在 onCreateView
.
中添加以下代码
LinearLayoutManager myLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
recyclerView2.setLayoutManager(myLayoutManager); // your recyclerView where you want to show your `ArrayList`.
如果问题还没有解决,请告诉我。
目前正在使用 java 和 android studio 开发移动应用程序。 (初学者)我有 3 个片段(ToDo、Doing、Done),顶部有一个 tablayout(ToDo、Ongoing、Finished)和一个带有添加按钮的工具栏。
添加按钮应该创建一个列表,允许用户修改每个list(Recyclerviewer)
获取错误:java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
下面是截图
代码
public class kanbanManagement extends Fragment {
// TODO: Rename and change types of parameters
TabLayout tabLayout;
private ViewPager2 viewPager2;
private MyFragment adapter;
private RecyclerView recyclerView2;
private com.example.myhouse.recyclerKanbanAdapter recyclerKanbanAdapter;
ArrayList<kanbanItems>listKanban;
Button btn_Add;
public kanbanManagement() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_kanban_management, container, false);
tabLayout=view.findViewById(R.id.tabLayout);
viewPager2=view.findViewById(R.id.viewPager2);
tabLayout.addTab(tabLayout.newTab().setText("To Do"));
tabLayout.addTab(tabLayout.newTab().setText("On going"));
tabLayout.addTab(tabLayout.newTab().setText("Finished"));
FragmentManager fragmentManager = getParentFragmentManager();
adapter = new MyFragment(fragmentManager , getLifecycle());
viewPager2.setAdapter(adapter);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager2.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
tabLayout.selectTab(tabLayout.getTabAt(position));
}
});
recyclerView2=view.findViewById(R.id.taskList);
listKanban= new ArrayList<>();
btn_Add=view.findViewById(R.id.btnAdd);
recyclerKanbanAdapter= new recyclerKanbanAdapter(getActivity(),listKanban);
btn_Add.setOnClickListener(v -> {
recyclerView2.setAdapter(recyclerKanbanAdapter);
});
return view;
}
}
抱歉,我没有完全理解您的代码。因此,无需检查您的完整代码。我的猜测是您没有使用 LayoutManager
来显示您的 listKanban
。所以再次添加 layoutManager
代码和 运行。
在 onCreateView
.
LinearLayoutManager myLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
recyclerView2.setLayoutManager(myLayoutManager); // your recyclerView where you want to show your `ArrayList`.
如果问题还没有解决,请告诉我。