英文:
NotifyDatasetchanged not working. AppCrashes after ButtonOnClick
问题
以下是翻译好的部分:
我一直在尝试创建一个按钮,当点击按钮时,可以将新值添加到我的ArrayList(countries)中,但实际上应用程序崩溃了,而不是添加我的项目(Card)。我还在使用Fragments。以下是我的代码。
这是我的Fragment页面类:
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<String> 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<>();
countries.add("Eat");
countries.add("Kill");
}
}
这是我的自定义适配器类:
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;
public ViewHolder(View view) {
super(view);
tv_country = (TextView) view.findViewById(R.id.tv_country);
}
}
}
这是您提供的Logcat日志:
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView$Adapter.notifyDataSetChanged()' on a null object reference
at com.example.barryblack.FragmentPage.InsertItem(FragmentPage.java:60)
at com.example.barryblack.FragmentPage$1.onClick(FragmentPage.java:43)
英文:
I've been trying to create a button that adds a new Value to my Arraylist (contries) when clicked, but it instead of adding my item(Card) the app simply crashes. I am also using Fragments. Here's my code.
That one is my Class for my 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");
}
}
This is my 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);
}
}
}
And my LogcatText
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView$Adapter.notifyDataSetChanged()' on a null object reference
at com.example.barryblack.FragmentPage.InsertItem(FragmentPage.java:60)
at com.example.barryblack.FragmentPage$1.onClick(FragmentPage.java:43)
答案1
得分: 0
这段代码:
private RecyclerView.Adapter mAdapter;
需要修改为:
private MyAdapter mAdapter;
以及将这段代码:
RecyclerView.Adapter adapter = new MyAdapter(countries);
修改为:
MyAdapter mAdapter = new MyAdapter(countries);
英文:
this code private RecyclerView.Adapter mAdapter;
must be private MyAdapter mAdapter;
and this RecyclerView.Adapter adapter = new MyAdapter(countries);
to this MyAdapter mAdapter = new MyAdapter(countries);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论