取消装箱hashmap.get()会产生NullPointerException

时间:2019-08-10 14:54:53

标签: java android nullpointerexception hashmap

我正在使用哈希图从另一个类获取数据。我检查了logcat,并设置了hashmap的数据,其中包含密钥。但是,当我尝试从另一个类中获取哈希图时,我得到了NullPointerException,并且当我尝试检查地图是否包含键时


 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.continueItem:
                CustomAdapter a = new CustomAdapter();
                CustomAdapter.InputTextListener i = a.new InputTextListener();
                HashMap<String, Integer> hashMap = i.getHashMap();
                inputTime = hashMap.get("EDITTEXT VALUE");
                Log.d(TAG, "onOptionsItemSelected: " + hashMap.get("EDITTEXT VALUE"));
                Log.d(TAG, "onOptionsItemSelected: " + hashMap.containsKey("EDITTEXT VALUE"));
                retrieveInputTime(inputTime);
                break;

        }
        return super.onOptionsItemSelected(item);
    }


public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {

    private static final String TAG = "CustomAdapter";
    private ArrayList<Integer> mWorkTW = new ArrayList<>();
    private ArrayList<Integer> mWorkET = new ArrayList<>();
    private ArrayList<Integer> mRestTW = new ArrayList<>();
    private ArrayList<Integer> mRestET = new ArrayList<>();
    private Context mContext;
    private int numberOfIntervals;

    public CustomAdapter() {

    }

    public CustomAdapter(Context context, ArrayList<Integer> mWorkTW, ArrayList<Integer> mWorkET, ArrayList<Integer> mRestTW, ArrayList<Integer> mRestET, int numberOfIntervals) {

        this.mWorkTW = mWorkTW;
        this.mWorkET = mWorkET;
        this.mRestTW = mRestTW;
        this.mRestET = mRestET;
        this.mContext = context;
        this.numberOfIntervals = numberOfIntervals;
        //this.inputTimeIntegerWET = inputTimeIntegerWET;

        Log.d(TAG, "CustomAdapter: " + numberOfIntervals);
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View customView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.time_row, viewGroup, false);
        ViewHolder holder = new ViewHolder(customView, new InputTextListener());
        return holder;

    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
        Log.d(TAG, "onBindViewHolder: called");
        viewHolder.workTextView.setText(R.string.work_text_view);
        viewHolder.restTextView.setText(R.string.rest_text_view);
        viewHolder.workEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus)
                    viewHolder.workEditText.setHint("");
                else
                    viewHolder.workEditText.setHint(mWorkET.get(viewHolder.getAdapterPosition()));
            }
        });

        viewHolder.restEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus)
                    viewHolder.restEditText.setHint("");
                else
                    viewHolder.restEditText.setHint(mRestET.get(viewHolder.getAdapterPosition()));
            }
        });
    }

    @Override
    public int getItemCount() {
        Log.d(TAG, "" + numberOfIntervals);
        return numberOfIntervals;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public InputTextListener inputTextListener;
        TextView workTextView;
        EditText workEditText;
        TextView restTextView;
        EditText restEditText;
        ConstraintLayout parentLayout;




        public ViewHolder(@NonNull View itemView, InputTextListener inputTextListener) {
            super(itemView);
            workTextView = itemView.findViewById(R.id.workTextView);
            workEditText = itemView.findViewById(R.id.workEditText);
            restTextView = itemView.findViewById(R.id.restTextView);
            restEditText = itemView.findViewById(R.id.restEditText);
            parentLayout = itemView.findViewById(R.id.parentLayout);
            this.inputTextListener = inputTextListener;


            workEditText.addTextChangedListener(inputTextListener);
        }
    }

        class InputTextListener implements TextWatcher {
        String inputTimeString;

        int inputTime;
        HashMap<String, Integer> hashMap = new HashMap<String, Integer>();

            public HashMap<String, Integer> getHashMap() {
                return hashMap;
            }

            public InputTextListener() {
            }

            public void setHashMap(HashMap<String, Integer> hashMap) {
                this.hashMap = hashMap;
            }

            /*public int getInputTime() {
            return inputTime;
        }*/

        public void setInputTime(int inputTime) {
            this.inputTime= inputTime;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
             try {
                 Log.d(TAG, "onTextChanged: I've made it to here!");
                 inputTimeString = s.toString().trim();
                 inputTime = Integer.parseInt(inputTimeString);
                 setInputTime(inputTime);
                 hashMap.put("EDITTEXT VALUE", inputTime);
                 Log.d(TAG, "onTextChanged: " + inputTime);

                 int bla = inputTime + 2;
                 Log.d(TAG, "onTextChanged: " + bla);
                 Log.d(TAG, "onTextChanged: " + hashMap.containsKey("EDITTEXT VALUE"));
                 Log.d(TAG, "onTextChanged: " + hashMap.get("EDITTEXT VALUE"));
                 setHashMap(hashMap);

             } catch (NumberFormatException NFE) {
                 mWorkET = null;
             }



        }

        @Override
        public void afterTextChanged(Editable s) {

        }


    }
}

我希望哈希图能够访问其他类中的数据。

1 个答案:

答案 0 :(得分:1)

以下是您发布的一些代码:

CustomAdapter a = new CustomAdapter();
CustomAdapter.InputTextListener i = a.new InputTextListener();
HashMap<String, Integer> hashMap = i.getHashMap();

这将要做的是创建CustomAdapter类的新实例,然后创建InputTextListener类的新实例,最后检索文本中存储的HashMap听众。

同样,这些都是在这些类的 new 实例中发生的。因此,HashMap为空(因为没有填充它)。

您可能假设i将是与您在应用程序中其他位置使用的“相同”侦听器实例。不是这种情况。您将需要以某种方式访问​​该侦听器,而不是创建一个新实例。

看看您的代码,这似乎并不可行。每个ViewHolder都有自己的InputTextListener实例...您的选项菜单如何知道您要与哪个ViewHolder进行交互?

有很好的机会,您需要回到绘图板上,并提出另一种方法来解决您尝试的任何问题。