使用自定义事件侦听器时自定义对话框错误

时间:2017-10-02 11:28:36

标签: java android dialog

我通过扩展DialogFragment类来创建自定义对话框。我的对话框显示基于参数List<String> datas的列表菜单。所以,我为此创建了一个自定义适配器。

ListMenuAdapter.java

/**
 * Created by fanjavaid on 10/2/17.
 */
public class ListMenuAdapter extends ArrayAdapter<String> implements View.OnClickListener {

    private Context mContext;
    private List<String> mMenus;
    private OnClickItemMenuListener onClickItemMenuListener;

    public ListMenuAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<String> objects) {
        super(context, resource, objects);
        this.mMenus = objects;
        this.mContext = context;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        String menu = mMenus.get(position);

        LayoutInflater inflater = LayoutInflater.from(mContext);
        final ViewListItemBinding binding = DataBindingUtil.inflate(inflater, R.layout.view_list_item, null, false);

        binding.textListMenu.setTag(R.string.id_menu_item, position);
        binding.textListMenu.setText(menu);
        binding.textListMenu.setOnClickListener(this);

        return binding.getRoot();
    }

    @Override
    public void onClick(View v) {
        onClickItemMenuListener.onClickItem((int) v.getTag(R.string.id_menu_item));
    }

    public OnClickItemMenuListener getOnClickItemMenuListener() {
        return onClickItemMenuListener;
    }

    public void setOnClickItemMenuListener(OnClickItemMenuListener onClickItemMenuListener) {
        this.onClickItemMenuListener = onClickItemMenuListener;
    }

    public interface OnClickItemMenuListener {
        void onClickItem(int index);
    }
}

在上面的代码中,我创建了Listener来处理每个列表菜单中的click事件。

以下是我的自定义对话框实现。

CustomDialogMenu.java

public class CustomDialogMenu extends DialogFragment implements
        ListMenuAdapter.OnClickItemMenuListener {

    private DialogTemplateBinding mDataBinding;
    private FragmentManager dialogFragmentManager;
    private List<String> mMenus;
    private IDialogView.ListMenuSelectedListener onMenuClickListener;

    private ListMenuAdapter mMenuAdapter;

    public CustomDialogMenu() {
    }

    public static CustomDialogMenu newInstance() {
        CustomDialogMenu fragment = new CustomDialogMenu();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

        // Create menu list and add click listener
        mMenuAdapter = new ListMenuAdapter(getActivity(), 0, mMenus);
        mMenuAdapter.setOnClickItemMenuListener(this);
        mDataBinding.dialogListMenu.setAdapter(mMenuAdapter);

        return mDataBinding.getRoot();
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        return dialog;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        getDialog().getWindow().setWindowAnimations(R.style.DialogAnimation);
    }

    @Override
    public void onStart() {
        super.onStart();

        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    public void show() {
        if (null == dialogFragmentManager)
            throw new NullPointerException("dialogFragmentManager is null. " +
                    "Please set it from support FragmentManager when instantiate this Fragment.");

        show(dialogFragmentManager, "CustomDialog");
    }

    public void setDialogFragmentManager(FragmentManager dialogFragmentManager) {
        this.dialogFragmentManager = dialogFragmentManager;
    }

    public void setMenus(List<String> mMenus) {
        this.mMenus = mMenus;
    }

    public IDialogView.ListMenuSelectedListener getOnMenuClickListener() {
        return onMenuClickListener;
    }

    public void setOnMenuClickListener(IDialogView.ListMenuSelectedListener onMenuClickListener) {
        this.onMenuClickListener = onMenuClickListener;
    }

    @Override
    public void onClickItem(int index) {
        onMenuClickListener.onItemSelected(index);
//        Toast.makeText(getActivity(), "Is NULL : " + (getOnMenuClickListener() == null), Toast.LENGTH_SHORT).show();
    }
}

最后,我可以使用以下代码创建对话框菜单:

// Show popup menu
List<String> menus = new ArrayList<>();
menus.add(getString(R.string.occupant_detail_menu_edit));
menus.add(getString(R.string.occupant_detail_menu_delete));

CustomDialogMenu dialog = new CustomDialogMenu();
dialog.setMenus(menus);
dialog.setDialogFragmentManager(getSupportFragmentManager());
dialog.setOnMenuClickListener(new IDialogView.ListMenuSelectedListener() {
    @Override
    public void onItemSelected(int index) {
        Toast.makeText(OccupantDetailActivity.this, "Index : " + index, Toast.LENGTH_SHORT).show();
    }
});
dialog.show();

触发点击列表项时出现以下错误:

FATAL EXCEPTION: main
Process: android.bookost.com.bookost, PID: 29807
java.lang.NullPointerException: Attempt to invoke interface method 
'void app.bookost.com.bookost.view.IDialogView$ListMenuSelectedListener.onItemSelected(int)' on a null object reference
 at app.bookost.com.bookost.view.component.CustomDialogMenu.onClickItem(CustomDialogMenu.java:119)
 at app.bookost.com.bookost.adapter.ListMenuAdapter.onClick(ListMenuAdapter.java:52)
 at android.view.View.performClick(View.java:5637)
 at android.view.View$PerformClick.run(View.java:22429)
 at android.os.Handler.handleCallback(Handler.java:751)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:154)
 at android.app.ActivityThread.main(ActivityThread.java:6119)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

它在IDialogView$ListMenuSelectedListener上显示为null,但我已在onCreateView()方法上初始化它。

有没有其他方法来初始化它?

谢谢。

0 个答案:

没有答案