在片段android

时间:2015-10-16 19:55:56

标签: android fragment fatal-error popupmenu

我正在尝试在带有动态叠加层的片段中创建一个弹出菜单。我在片段中播放视频,当您触摸屏幕时,会使用按钮控件创建叠加层。其中一个按钮控件将在按下时创建一个弹出菜单。

@Override
// when we touch the screen
public boolean onTouch(View v, MotionEvent event) {

    Log.v(TAG, "onTouch() ");
    if (mMediaControl == null) {
        createMediaController();
        mMediaControl.show();
        return true;
    }
    mMediaControl.show();
    return false;
}


private void createMediaController() {
    Log.i(TAG, "createMediaController()");

    destroyMediaController();

    // create media controller
    mMediaControl = new MediaController(getActivity(), false);
    // set media player as this.
    mMediaControl.setMediaPlayer(this);
    mMediaControl.setAnchorView(mSurfaceView);


    mMediaControl.removeAllViewsInLayout();
    View v = makeControllerView(mMediaControl);

    FrameLayout.LayoutParams frameParams = new FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
    );

    mMediaControl.addView(v, frameParams);
    mMediaControl.show();
}

private View makeControllerView(View v) {
    LayoutInflater inflate = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v= inflate.inflate(R.layout.custom_media_controller, null);
    final ImageButton imageButton = (ImageButton)   v.findViewById(R.id.imageId);

    imageButton .setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
             v.post(new Runnable() {
                 @Override
                 public void run() {
                     showPopupMenu(v);
                 }
             });
        }
    });



    // BEGIN_INCLUDE(show_popup)
private void showPopupMenu(View view) {

    // Create a PopupMenu, giving it the clicked view for an anchor
    PopupMenu popup = new PopupMenu(getActivity(), view);

    // Inflate our menu resource into the PopupMenu's Menu
    popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu());

    // Set a listener so we are notified if a menu item is clicked
    popup.setOnMenuItemClickListener(new     PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
          // whatever  
          return true;
        }
    });

    popup.show();
}

这几乎直接来自弹出菜单的android doc示例。

每当我按下按钮创建菜单时,我都会遇到这个致命的例外。

  

10-16 12:17:55.554 2974-2974 /? E / AndroidRuntime:致命异常:   主要       流程:ia.android.com,PID:2974       android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌android.view.ViewRootImpl$W@41efd798无效;你的活动在运行吗?               在android.view.ViewRootImpl.setView(ViewRootImpl.java:582)               在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)               在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:76)               at amazon.view.AmazonWindowManagerImpl.addView(AmazonWindowManagerImpl.java:83)               在android.widget.PopupWindow.invokePopup(PopupWindow.java:1063)               在android.widget.PopupWindow.showAsDropDown(PopupWindow.java:969)               在android.widget.ListPopupWindow.show(ListPopupWindow.java:654)               在com.android.internal.view.menu.MenuPopupHelper.tryShow(MenuPopupHelper.java:167)               在com.android.internal.view.menu.MenuPopupHelper.show(MenuPopupHelper.java:132)               在android.widget.PopupMenu.show(PopupMenu.java:170)               at ia.android.com.PlayerActivity $ PlayerFragment.showPopupMenu(PlayerActivity.java:347)               at ia.android.com.PlayerActivity $ PlayerFragment.access $ 700(PlayerActivity.java:143)               at ia.android.com.PlayerActivity $ PlayerFragment $ 11 $ 1.run(PlayerActivity.java:473)               在android.os.Handler.handleCallback(Handler.java:755)               在android.os.Handler.dispatchMessage(Handler.java:95)               在android.os.Looper.loop(Looper.java:145)               在android.app.ActivityThread.main(ActivityThread.java:5266)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:515)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:826)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:642)               在dalvik.system.NativeStart.main(本地方法)

0 个答案:

没有答案