我想在用户单击浮动图标时显示自定义弹出菜单
浮动图标是通过服务创建的,但我没有任何活动
这是我的浮动图标代码
public class copy_actions_service extends Service
{
ImageView copy_ImageView;
WindowManager windowManager;
WindowManager.LayoutParams layoutParams;
@Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate()
{
windowManager=(WindowManager)getSystemService(WINDOW_SERVICE);
copy_ImageView=new ImageView(this);
copy_ImageView.setImageResource(R.drawable.ic_launcher);
copy_ImageView.setAlpha(245);
copy_ImageView.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
showCustomPopupMenu();
}
});
layoutParams=new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
layoutParams.gravity=Gravity.TOP|Gravity.CENTER;
layoutParams.x=0;
layoutParams.y=100;
windowManager.addView(copy_ImageView, layoutParams);
}
private void showCustomPopupMenu()
{
LayoutInflater layoutInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=layoutInflater.inflate(R.layout.xxact_copy_popupmenu, null);
PopupWindow popupWindow=new PopupWindow();
popupWindow.setContentView(view);
popupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, 0, 0);
}
}
一切都很好,但是当我单击浮动按钮应用程序停止时,此错误显示在 logcat 上:(
11-23 02:18:58.217: E/AndroidRuntime(3231): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
但我没有活动?
我想在用户单击浮动图标后显示弹出菜单;但弹出菜单只能显示文字;
如何显示带有图标的弹出菜单?
如果您正在使用getApplicationContext()
as Context
in Activity对于这样的对话框
Dialog dialog = new Dialog(getApplicationContext());
然后使用 YourActivityName.this
Dialog dialog = new Dialog(YourActivityName.this);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)