简体   繁体   English

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

[英]Unboxing of hashmap.get() is producing a NullPointerException

I'm using a hashmap to get data from another class. 我正在使用哈希图从另一个类获取数据。 I check the logcat and the data for hashmap has been set and it contains the key. 我检查了logcat,并设置了hashmap的数据,其中包含密钥。 But when I try to get the hashmap from the other class I'm getting NullPointerException and when I try to check if the map contains the key it doesn't 但是,当我尝试从另一个类获取哈希图时,我得到了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) {

        }


    }
}

I'm expecting for the hashmap being able to access the data from the other class. 我期望哈希图能够访问其他类的数据。

Here is some of the code you posted: 这是您发布的一些代码:

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

What this is going to do is create a new instance of your CustomAdapter class, then create a new instance of your InputTextListener class, and finally retrieve the HashMap stored inside the text listener. 这将要做的是创建CustomAdapter类的新实例,然后创建InputTextListener类的新实例,最后检索存储在文本侦听器中的HashMap

Again, this is all happening with new instances of these classes. 同样,这些都是在这些类的实例中发生的。 Therefore, the HashMap is empty (since nothing has populated it). 因此, HashMap为空(因为没有填充它)。

You are probably assuming that i would be the "same" listener instance as you're using elsewhere in your app. 您可能会假设i将成为您在应用程序其他位置使用的“相同”侦听器实例。 This is not the case. 不是这种情况。 You will need to access that listener somehow rather than creating a new instance. 您将需要以某种方式访问​​该侦听器,而不是创建一个新实例。

Looking at your code, this doesn't really seem feasible. 查看您的代码,这似乎并不可行。 Each ViewHolder has its own instance of InputTextListener ... how will your options menu know which ViewHolder you're trying to interact with? 每个ViewHolder都有其自己的InputTextListener实例...您的选项菜单如何知道您要与哪个ViewHolder进行交互?

Chances are good that you're going to need to go back to the drawing board and come up with a different way to solve whatever problem you're attempting. 您很有可能需要回到绘图板上,并提出另一种方法来解决您尝试的任何问题。

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

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