在 android 中为弹出窗口 window 中的项目设置 onClickListener
Setting onClickListner for items within popup window in android
我有一个包含 2 个文本视图的弹出窗口 window。
如果用户单击一个文本视图 toast,则必须 appear.I 为该功能编码但在行 far.setOnClickListener(new OnClickListener() {
中显示 Nullpointer exception
请帮助我。
我的代码:
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();
}
});
}
});
更改此行
TextView far = (TextView) popup_view.findViewById(R.id.fartext);
而不是这个
TextView far = (TextView) rootView.findViewById(R.id.fartext);
您得到 NullPointerException
是因为您试图在实际上属于 popup_view 而不是 rootView 的视图上设置点击侦听器。像这样修复它:
TextView far = (TextView) popup_view.findViewById(R.id.fartext);
far.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Clicked Far", Toast.LENGTH_SHORT).show();
}
});
问题是您正在查找 rootView
,而 fareText
不存在。你应该看看膨胀的布局。检查下面的代码。
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) popup_view.findViewById(R.id.fartext);
far.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Clicked Far", Toast.LENGTH_SHORT).show();
}
});
}
});
我有一个包含 2 个文本视图的弹出窗口 window。
如果用户单击一个文本视图 toast,则必须 appear.I 为该功能编码但在行 far.setOnClickListener(new OnClickListener() {
中显示 Nullpointer exception
请帮助我。
我的代码:
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();
}
});
}
});
更改此行
TextView far = (TextView) popup_view.findViewById(R.id.fartext);
而不是这个
TextView far = (TextView) rootView.findViewById(R.id.fartext);
您得到 NullPointerException
是因为您试图在实际上属于 popup_view 而不是 rootView 的视图上设置点击侦听器。像这样修复它:
TextView far = (TextView) popup_view.findViewById(R.id.fartext);
far.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Clicked Far", Toast.LENGTH_SHORT).show();
}
});
问题是您正在查找 rootView
,而 fareText
不存在。你应该看看膨胀的布局。检查下面的代码。
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) popup_view.findViewById(R.id.fartext);
far.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Clicked Far", Toast.LENGTH_SHORT).show();
}
});
}
});