为什么我的bottomsheetdialog接口为空

时间:2018-04-30 07:45:45

标签: java android mobile

  

E / UncaughtException:java.lang.NullPointerException:尝试在空对象引用上调用接口方法'void mm.com..fragment.BottomSheetFragment $ BottomSheetListener.onButtonClicked(java.lang.String)'                            at mm.com.blueplanet.videoclip.fragment.BottomSheetFragment $ 1.onClick(BottomSheetFragment.java:40)                            在android.view.View.performClick(View.java:5647)

这是RecycervieAdapter类

((ItemViewHolder) holder).cmtText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction ft = ((DetailActivity) mContext).getSupportFragmentManager()
                        .beginTransaction();
                BottomSheetFragment bottomsheet= new BottomSheetFragment();
                bottomsheet.show( ft, "BottomSheet");
            }
        });

这是底片

public class BottomSheetFragment extends BottomSheetDialogFragment {
  Context mContext;
  public BottomSheetListener mListener;

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

    mContext = getActivity();
    ImageButton btn = (ImageButton) v.findViewById(R.id.cmt_btn);
    final EditText edt = (EditText) v.findViewById(R.id.edt_cmt);


    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mListener.onButtonClicked("" + edt.getText().toString());

            dismiss();
        }
    });


     return v;
  }

  public interface BottomSheetListener {
    void onButtonClicked(String text);
  }


 @Override
 public void onAttach(Context context) {
    super.onAttach(context);
    try {
        mListener = (BottomSheetListener) mContext;
    } catch (ClassCastException e) {
        throw new ClassCastException(context.toString());
    }
  }
}

1 个答案:

答案 0 :(得分:0)

((ItemViewHolder) holder).cmtText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentTransaction ft = ((DetailActivity) mContext).getSupportFragmentManager()
                        .beginTransaction();
                BottomSheetFragment bottomsheet= new BottomSheetFragment();
                bottomsheet.show( ft, "BottomSheet");
                bottomsheet.onButtonClicked()

            }
        });

创建新对象时必须实施 onButtonClicked

你可以像这样编辑你的听众

private onItemsClicked mListener = null;

并创建两个界面

public void setOnClickListener(onItemsSelected onClickListener)
    {
        mListener = onClickListener;
    }
    public interface onItemsSelected
    {
        void onclick(float x, float y, String value);
    }

现在你可以像这样使用它

view.setOnClickListener(new LineChart.onItemsSelected() {
            @Override
            public void onclick(float x, float y , String value) {}