android - 无法添加窗口——token null 无效;您的 Activity 正在运行吗?

标签 android

我想在用户点击 float 图标时显示自定义弹出菜单

使用服务创建的 float 图标,我没有 Activity

这是我的 float 图标代码

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);             
    }
}

一切都很好,但是当我点击 float 按钮时,应用程序停止并且这个错误显示在 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?

但我没有 Activity ?

我想在用户点击 float 图标后弹出菜单显示;但弹出菜单只能显示文字;

如何显示带有图标的弹出菜单?

最佳答案

如果您在 Activity 中将 getApplicationContext() 作为 Context 用于这样的对话框

Dialog dialog = new Dialog(getApplicationContext());

然后使用 YourActivityName.this

Dialog dialog = new Dialog(YourActivityName.this);

关于android - 无法添加窗口——token null 无效;您的 Activity 正在运行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27087983/

相关文章:

android - 手机语言改变时不要改变设计

android - 使用 URL 启动应用程序

android - 如何使自定义 ListView 内的 TextView 可滚动?

android - 如何让用户在优步中开始和结束位置

java - Android Textview 文本在底部被截断

java - 在扩展类中强制使用某个函数,可能吗?

java - Activity com.example.mediastore.LoginActivity 已泄漏窗口 com.android.internal.policy.impl.PhoneWindow$DecorView

android - 如何从触摸android检测手指的类型,例如拇指,食指,中指,无名指,小指

android - 如何在Android库项目中按测试类型分隔源文件?

android - 在 listView 内的 listItem 内的 textView 上使用 setSelected