为android弹出窗口中的项目设置onClickListner

ten*_*ten 1 android onclick android-popupwindow

我有一个包含 2 个文本视图的弹出窗口。

如果用户单击一个文本视图 toast 必须出现。我为此功能编码但显示Nullpointer exception在线far.setOnClickListener(new OnClickListener() {请帮助我。

我的代码:

btn_a.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            LayoutInflater lInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View popup_view = lInflater.inflate(R.layout.popup_a, null);
            final PopupWindow popup = new PopupWindow(popup_view,200,75,true);
            popup.setFocusable(true);
            popup.setBackgroundDrawable(new ColorDrawable());   
            popup.showAsDropDown(btn_a,  0,0);

            TextView far = (TextView) rootView.findViewById(R.id.fartext);
            far.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getActivity(), "Clicked Far", Toast.LENGTH_SHORT).show();

                }
            });
        }
    });
Run Code Online (Sandbox Code Playgroud)

Abh*_*tel 5

改变这一行

TextView far = (TextView) popup_view.findViewById(R.id.fartext);
Run Code Online (Sandbox Code Playgroud)

而不是这个

TextView far = (TextView) rootView.findViewById(R.id.fartext);
Run Code Online (Sandbox Code Playgroud)