简体   繁体   English

无法从 onClickListener 添加到 arraylist

[英]Can't add to arraylist from onClickListener

I am trying to add an ArrayList inside of another ArrayList but i always get this error:我正在尝试在另一个 ArrayList 中添加一个 ArrayList 但我总是收到此错误:

java.lang.NullPointerException:Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference

The problem seems to be the OnClickListener or the Dialog because this error does not happen in the OnCreateView().问题似乎出在 OnClickListener 或 Dialog 上,因为此错误不会发生在 OnCreateView() 中。 Here is my code:这是我的代码:

public class CoordsFragment extends Fragment {
    public static ArrayList<ArrayList<String>> AllCoords = new ArrayList<ArrayList<String>>();
    public FloatingActionButton fab;
    public Dialog dialog;
    public Button cancel;
    public Button create;


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_coords, container, false);

        mainLayout = view.findViewById(R.id.fragment_coords_main_layout);

        ArrayList<String> arr;
        arr = new ArrayList<String>();

        /*the 3 arr.add doesn't work because the views "name", "x" and "y" are null here but if i replace 
        them with normal strings it works. this is just for the exemple*/
        arr.add(name.getText().toString());
        arr.add(x.getText().toString());
        arr.add(y.getText().toString());
        //AllCords.add(arr); causes no error here
        AllCoords.add(arr);

        fab = view.findViewById(R.id.plus_fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fabClick();
            }
        });

        return view;
    }

    public void fabClick() {



        dialog = new Dialog(getActivity());

        LayoutInflater inflater = this.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.dialog_add_coords, null);
        dialog.setContentView(dialogView);

        create = dialogView.findViewById(R.id.coords_dialog_create);
        create.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogCreate();
            }
        });

        cancel = dialogView.findViewById(R.id.coords_dialog_cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogCancel();
            }
        });

        dialog.show();
    }



    public void dialogCreate() {
        dialog.dismiss();
        ArrayList<String> arr;
        arr = new ArrayList<String>();
        arr.add(name.getText().toString());
        arr.add(x.getText().toString());
        arr.add(y.getText().toString());
        //AllCoords.add(arr); causes the error here
        AllCoords.add(arr);
    }

    public void dialogCancel() {
        dialog.dismiss();
    }



}

I didn't manage to find a working solution for me.我没能找到适合我的工作解决方案。 Maybe i shouldn't use an ArrayList for this code, so if you have a solution that replace the ArrayList i'm fine with it.也许我不应该为这段代码使用 ArrayList,所以如果你有一个替代 ArrayList 的解决方案,我会接受它。 Thank you in advance for answers and sorry in advance for my potential bad english.提前感谢您的回答,并提前为我潜在的糟糕英语道歉。

You should initialize name , x and y before use them.您应该在使用前初始化namexy I have not found the initializations of variables in the code.我没有在代码中找到变量的初始化。

Found myself where the problem was, thanks all for your answers but the problem isn't in the code i sent.发现问题所在,感谢大家的回答,但问题不在我发送的代码中。

So the problem was that i was getting an ArrayList from sharedPreferences(using gson) and saving it in AllCoords.所以问题是我从 sharedPreferences(使用 gson)得到一个 ArrayList 并将它保存在 AllCoords 中。 but if no data was saved in sharedPreferences, the ArrayList would always be null.但如果 sharedPreferences 中没有保存数据,则 ArrayList 将始终为 null。

I'm sorry for wasting your time, it was impossible to find the solution with the code i sent.很抱歉浪费您的时间,我发送的代码无法找到解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM