This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12个答案)
6个月前关闭。
我一直在尝试创建一个按钮,该按钮在单击时将新值添加到我的Arraylist中(contries),但是它没有添加我的item(卡),而是使应用程序崩溃了。我也在使用片段。这是我的代码。
那是我的FragmentPage类
这是我的CustomAdapter
还有我的LogcatText
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void androidx.recyclerview.widget.RecyclerView $ Adapter.notifyDataSetChanged()”
在com.example.barryblack.FragmentPage.InsertItem(FragmentPage.java:60)
在com.example.barryblack.FragmentPage $ 1.onClick(FragmentPage.java:43)
(12个答案)
6个月前关闭。
我一直在尝试创建一个按钮,该按钮在单击时将新值添加到我的Arraylist中(contries),但是它没有添加我的item(卡),而是使应用程序崩溃了。我也在使用片段。这是我的代码。
那是我的FragmentPage类
package com.example.barryblack;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class FragmentPage extends Fragment {
private RecyclerView.Adapter mAdapter;
private ArrayList countries;
Button btn;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
Bundle savedInstanceState) {
CreateExampleList();
View view = inflater.inflate(R.layout.page_fragment, container, false);
RecyclerView recyclerView = view.findViewById(R.id.listRecyclerView);
btn = view.findViewById(R.id.button_insert);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InsertItem();
}
});
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
RecyclerView.Adapter adapter = new MyAdapter(countries);
recyclerView.setAdapter(adapter);
return view;
}
public void InsertItem() {
countries.add("Smile");
mAdapter.notifyDataSetChanged();
}
private void CreateExampleList() {
countries = new ArrayList<String>();
countries.add("Eat");
countries.add("Kill");
}
}
这是我的CustomAdapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ArrayList<String> cards;
public MyAdapter(ArrayList<String> countries) {
this.cards = countries;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.example_item, viewGroup,
false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, int i) {
viewHolder.tv_country.setText(cards.get(i));
}
@Override
public int getItemCount() {
return cards.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tv_country;
private Button tv_button;
public ViewHolder(View view) {
super(view);
tv_country = (TextView)view.findViewById(R.id.tv_country);
}
}
}public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ArrayList<String> cards;
public MyAdapter(ArrayList<String> countries) {
this.cards = countries;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.example_item, viewGroup,
false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, int i) {
viewHolder.tv_country.setText(cards.get(i));
}
@Override
public int getItemCount() {
return cards.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tv_country;
private Button tv_button;
public ViewHolder(View view) {
super(view);
tv_country = (TextView)view.findViewById(R.id.tv_country);
}
}
}
还有我的LogcatText
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void androidx.recyclerview.widget.RecyclerView $ Adapter.notifyDataSetChanged()”
在com.example.barryblack.FragmentPage.InsertItem(FragmentPage.java:60)
在com.example.barryblack.FragmentPage $ 1.onClick(FragmentPage.java:43)
最佳答案
此代码private RecyclerView.Adapter mAdapter;
必须为private MyAdapter mAdapter;
和这个RecyclerView.Adapter adapter = new MyAdapter(countries);
到这个MyAdapter mAdapter = new MyAdapter(countries);
关于java - NotifyDatasetchanged无法正常工作。 ButtonOnClick后AppCrash ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61046534/
10-12 02:31