无法添加窗口-令牌null无效;您的活动正在进行吗? [英] Unable to add window -- token null is not valid; is your activity running?
问题描述
我想在用户单击浮动图标时显示自定义弹出菜单
i want to show a custom popup menu when user click on a floating icon
使用服务创建的浮动图标,但我没有任何活动
the float icon create with an service and i have no activity
这是我的浮动图标代码
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上显示此错误时:(
every thing fine but when i click on float button app stop and this error is shown on 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?
但是我没有活动?!
我想在用户单击浮动图标后弹出菜单显示;但是弹出菜单只能显示文本;
i want to popup menu show after user click on float icon; but popup menu only can show text;
我如何显示带有图标的弹出菜单?
how to i can show a popup menu with icons?
推荐答案
我遇到了与您相同的问题,看来您使用的是 http://www.piwai.info/chatheads-basics 就像我一样.问题在于您无法可靠地将当前活动传递到弹出窗口,因为您无法控制当前活动.似乎有一种不可靠的方式来获取当前活动,但我不建议这样做.
I had the same problem as you, it looks like you used the tutorial from http://www.piwai.info/chatheads-basics like I did. The problem is that you cannot reliably pass the current activity to the popup window, because you have no control over the current activity. It looks like there might be a unreliable way to get the current activity, but I don't recommend that.
我为应用程序修复该问题的方法是不使用弹出窗口,而是通过窗口管理器自行创建.
The way I fixed it for my app was to not use the popup window, but instead make my own through the window manager.
private void showCustomPopupMenu()
{
windowManager2 = (WindowManager)getSystemService(WINDOW_SERVICE);
LayoutInflater layoutInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=layoutInflater.inflate(R.layout.xxact_copy_popupmenu, null);
params=new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity=Gravity.CENTER|Gravity.CENTER;
params.x=0;
params.y=0;
windowManager2.addView(view, params);
}
如果您希望它看起来像一个弹出窗口,只需添加一个透明的灰色视图作为背景,并向其添加一个onClickListener即可从windowManager对象中删除该视图.
If you want this to look like a popup window, just add a transparent gray view as the background and add a onClickListener to it to remove the view from the windowManager object.
我知道这不如弹出窗口方便,但根据我的经验,这是最可靠的方法.
I know this isn't as convenient as a popup, but from my experience, it is the most reliable way.
别忘了在清单文件中添加权限
and don't forget to add permission in your manifest file
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
这篇关于无法添加窗口-令牌null无效;您的活动正在进行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!