Android使用添加的视图在setCustomSelectionActionModeCallback上获取NULL异常

时间:2017-07-15 21:41:11

标签: android

以下代码在某些Android版本上运行正常,没有任何问题,但在某些版本上,例如4.36.0.1。我得到null exception或应用不具有在R.menu.actionmode上实施的自定义菜单

这是我的示例代码

@Override
public void onBindViewHolder(final MyViewHolder viewHolder, final int position) {
    final TextView myTv = new TextView(context);
    myTv.setText(myString.get(position));
    myTv.setTextIsSelectable(true);
    myTv.setFocusable(true);
    myTv.setFocusableInTouchMode(true);
    myTv.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            ((TextView) view).setCustomSelectionActionModeCallback(new ActionMode.Callback() {
                @Override
                public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                    MenuInflater inflater = mode.getMenuInflater();
                    inflater.inflate(R.menu.actionmode, menu);
                    return true;
                }
                @Override
                public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                    menu.removeItem(android.R.id.copy);
                    menu.removeItem(android.R.id.cut);
                    menu.removeItem(android.R.id.selectAll);
                    return true;
                }
                @Override
                public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                    return false;
                }
                @Override
                public void onDestroyActionMode(ActionMode mode) {
                }
            });
            return true;
        }
    });
    viewHolder.sampleText.addView(myTv);
}

1 个答案:

答案 0 :(得分:1)

我通过实施其他不使用setCustomSelectionActionModeCallback

的方法来解决此Android错误

这是完整的代码,知道如何解决这个问题:

public class MainActivity extends AppCompatActivity {

    private RecyclerView  recyclerView;
    private SampleAdapter SampleAdapter;
    private List<String>  sampleData;
    private Button        getSelected;
    private int selected = -1;
    private ViewGroup viewGroup;
    private int       selectedCafeWeb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        getSelected = (Button) findViewById(R.id.getSelected);

        sampleData = new ArrayList<>();
        sampleData.add("Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World");
        sampleData.add("Good Bye Good Bye Good Bye Good Bye Good Bye Good Bye Good Bye Good Bye Good Bye Good Bye Good Bye ");

        recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext()));
        SampleAdapter = new SampleAdapter(sampleData, getBaseContext(), new OnItemClickListener() {
            @Override
            public void onClick(ViewGroup mViewGroup, int mSelectedCafeWeb) {
                selectedCafeWeb = mSelectedCafeWeb;
                viewGroup = mViewGroup;
            }
        });
        recyclerView.setAdapter(SampleAdapter);
        SampleAdapter.notifyDataSetChanged();

        getSelected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                check(viewGroup, selectedCafeWeb);
            }
        });
    }

    private void check(ViewGroup viewGroup, int selectedCafeWeb) {
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            final View child = viewGroup.getChildAt(i);
            if (child instanceof ViewGroup) {
                check((ViewGroup) child, selectedCafeWeb);
            } else if (child instanceof TextView) {
                TextView mTextView = ((TextView) child);
                if (mTextView.isFocused()) {
                    final int selStart = mTextView.getSelectionStart();
                    final int selEnd   = mTextView.getSelectionEnd();

                    int min = Math.max(0, Math.min(selStart, selEnd));
                    int max = Math.max(0, Math.max(selStart, selEnd));

                    final CharSequence selectedText = mTextView.getText().subSequence(min, max);
                    Toast.makeText(getApplicationContext(), selectedText, Toast.LENGTH_SHORT).show();
                    selected = selectedCafeWeb;
                    Toast.makeText(getBaseContext(), selected + " " + selectedText.toString(), Toast.LENGTH_SHORT);
                } else {
                    Log.e("NO ", " isFocused ");
                }
            }
        }
    }

    private class SampleAdapter extends RecyclerView.Adapter<MyViewHolder> {
        private List<String>        myString;
        private Context             context;
        private OnItemClickListener onItemClickListener;

        public SampleAdapter(List<String> mMyString, Context mContext, OnItemClickListener mOnItemClickListener) {
            context = mContext;
            myString = mMyString;
            onItemClickListener = mOnItemClickListener;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.sample_list, viewGroup, false);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final MyViewHolder viewHolder, final int position) {
            final TextView myTv = new TextView(context);
            myTv.setText(myString.get(position));
            myTv.setTextIsSelectable(true);
            myTv.setFocusable(true);
            myTv.setFocusableInTouchMode(true);

            viewHolder.sampleText.addView(myTv);
            myTv.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onClick(viewHolder.sampleText, position);
                    }
                    return false;
                }
            });
        }

        @Override
        public int getItemCount() {
            return myString.size();
        }
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        LinearLayout sampleText;

        public MyViewHolder(View view) {
            super(view);
            sampleText = (LinearLayout) view.findViewById(R.id.sampleText);
        }
    }
}