`NotifyDatasetchanged`不起作用。在点击按钮后应用程序崩溃。

huangapple 未分类评论50阅读模式
英文:

NotifyDatasetchanged not working. AppCrashes after ButtonOnClick

问题

以下是翻译好的部分:

我一直在尝试创建一个按钮,当点击按钮时,可以将新值添加到我的ArrayList(countries)中,但实际上应用程序崩溃了,而不是添加我的项目(Card)。我还在使用Fragments。以下是我的代码。

这是我的Fragment页面类:

  1. package com.example.barryblack;
  2. import android.os.Bundle;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.Button;
  7. import androidx.annotation.NonNull;
  8. import androidx.annotation.Nullable;
  9. import androidx.fragment.app.Fragment;
  10. import androidx.recyclerview.widget.LinearLayoutManager;
  11. import androidx.recyclerview.widget.RecyclerView;
  12. import java.util.ArrayList;
  13. public class FragmentPage extends Fragment {
  14. private RecyclerView.Adapter mAdapter;
  15. private ArrayList<String> countries;
  16. Button btn;
  17. @Nullable
  18. @Override
  19. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  20. CreateExampleList();
  21. View view = inflater.inflate(R.layout.page_fragment, container, false);
  22. RecyclerView recyclerView = view.findViewById(R.id.listRecyclerView);
  23. btn = view.findViewById(R.id.button_insert);
  24. btn.setOnClickListener(new View.OnClickListener() {
  25. @Override
  26. public void onClick(View v) {
  27. InsertItem();
  28. }
  29. });
  30. recyclerView.setHasFixedSize(true);
  31. RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
  32. recyclerView.setLayoutManager(layoutManager);
  33. RecyclerView.Adapter adapter = new MyAdapter(countries);
  34. recyclerView.setAdapter(adapter);
  35. return view;
  36. }
  37. public void InsertItem() {
  38. countries.add("Smile");
  39. mAdapter.notifyDataSetChanged();
  40. }
  41. private void CreateExampleList() {
  42. countries = new ArrayList<>();
  43. countries.add("Eat");
  44. countries.add("Kill");
  45. }
  46. }

这是我的自定义适配器类:

  1. public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
  2. private ArrayList<String> cards;
  3. public MyAdapter(ArrayList<String> countries) {
  4. this.cards = countries;
  5. }
  6. @Override
  7. public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
  8. View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.example_item, viewGroup, false);
  9. return new ViewHolder(view);
  10. }
  11. @Override
  12. public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, int i) {
  13. viewHolder.tv_country.setText(cards.get(i));
  14. }
  15. @Override
  16. public int getItemCount() {
  17. return cards.size();
  18. }
  19. public class ViewHolder extends RecyclerView.ViewHolder {
  20. private TextView tv_country;
  21. public ViewHolder(View view) {
  22. super(view);
  23. tv_country = (TextView) view.findViewById(R.id.tv_country);
  24. }
  25. }
  26. }

这是您提供的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

  1. package com.example.barryblack;
  2. import android.os.Bundle;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.Button;
  7. import androidx.annotation.NonNull;
  8. import androidx.annotation.Nullable;
  9. import androidx.fragment.app.Fragment;
  10. import androidx.recyclerview.widget.LinearLayoutManager;
  11. import androidx.recyclerview.widget.RecyclerView;
  12. import java.util.ArrayList;
  13. public class FragmentPage extends Fragment {
  14. private RecyclerView.Adapter mAdapter;
  15. private ArrayList countries;
  16. Button btn;
  17. @Nullable
  18. @Override
  19. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
  20. Bundle savedInstanceState) {
  21. CreateExampleList();
  22. View view = inflater.inflate(R.layout.page_fragment, container, false);
  23. RecyclerView recyclerView = view.findViewById(R.id.listRecyclerView);
  24. btn = view.findViewById(R.id.button_insert);
  25. btn.setOnClickListener(new View.OnClickListener() {
  26. @Override
  27. public void onClick(View v) {
  28. InsertItem();
  29. }
  30. });
  31. recyclerView.setHasFixedSize(true);
  32. RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
  33. recyclerView.setLayoutManager(layoutManager);
  34. RecyclerView.Adapter adapter = new MyAdapter(countries);
  35. recyclerView.setAdapter(adapter);
  36. return view;
  37. }
  38. public void InsertItem() {
  39. countries.add(&quot;Smile&quot;);
  40. mAdapter.notifyDataSetChanged();
  41. }
  42. private void CreateExampleList() {
  43. countries = new ArrayList&lt;String&gt;();
  44. countries.add(&quot;Eat&quot;);
  45. countries.add(&quot;Kill&quot;);
  46. }
  47. }

This is my CustomAdapter

  1. public class MyAdapter extends RecyclerView.Adapter&lt;MyAdapter.ViewHolder&gt; {
  2. private ArrayList&lt;String&gt; cards;
  3. public MyAdapter(ArrayList&lt;String&gt; countries) {
  4. this.cards = countries;
  5. }
  6. @Override
  7. public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
  8. View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.example_item, viewGroup,
  9. false);
  10. return new ViewHolder(view);
  11. }
  12. @Override
  13. public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, int i) {
  14. viewHolder.tv_country.setText(cards.get(i));
  15. }
  16. @Override
  17. public int getItemCount() {
  18. return cards.size();
  19. }
  20. public class ViewHolder extends RecyclerView.ViewHolder{
  21. private TextView tv_country;
  22. private Button tv_button;
  23. public ViewHolder(View view) {
  24. super(view);
  25. tv_country = (TextView)view.findViewById(R.id.tv_country);
  26. }
  27. }
  28. }public class MyAdapter extends RecyclerView.Adapter&lt;MyAdapter.ViewHolder&gt; {
  29. private ArrayList&lt;String&gt; cards;
  30. public MyAdapter(ArrayList&lt;String&gt; countries) {
  31. this.cards = countries;
  32. }
  33. @Override
  34. public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
  35. View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.example_item, viewGroup,
  36. false);
  37. return new ViewHolder(view);
  38. }
  39. @Override
  40. public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, int i) {
  41. viewHolder.tv_country.setText(cards.get(i));
  42. }
  43. @Override
  44. public int getItemCount() {
  45. return cards.size();
  46. }
  47. public class ViewHolder extends RecyclerView.ViewHolder{
  48. private TextView tv_country;
  49. private Button tv_button;
  50. public ViewHolder(View view) {
  51. super(view);
  52. tv_country = (TextView)view.findViewById(R.id.tv_country);
  53. }
  54. }
  55. }

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

这段代码:

  1. private RecyclerView.Adapter mAdapter;

需要修改为:

  1. private MyAdapter mAdapter;

以及将这段代码:

  1. RecyclerView.Adapter adapter = new MyAdapter(countries);

修改为:

  1. 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);

huangapple
  • 本文由 发表于 2020年4月6日 01:22:01
  • 转载请务必保留本文链接:https://java.coder-hub.com/61046534.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定