使用PopupWindow出现的错误

yitiaochongzi 2011-07-07 02:42:00
MainActivity.java
package com.menu;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.PopupWindow;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity {
PopupWindow popupMenu;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View view = LayoutInflater.from(this).inflate(R.layout.menu_home, null);

OnClickListener clickListener = new OnClickListener() {

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.menu_home4:

break;

default:
break;
}
popupMenu.dismiss();
}
};
view.findViewById(R.id.menu_home4).setOnClickListener(clickListener);

view.setFocusableInTouchMode(true);
popupMenu = new PopupWindow(view, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true);
popupMenu.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popupMenu.setFocusable(true);
popupMenu .setOutsideTouchable(true);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
popupMenu.showAtLocation(MainActivity.this.findViewById(R.id.main),
Gravity.BOTTOM, 0, 0);
return true;
}
return super.onKeyUp(keyCode, event);
}

}


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>


menu_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:gravity="bottom|center_horizontal" >

<Button android:id="@+id/menu_home4" android:layout_weight="1"
android:layout_width="110px" android:layout_height="80px" android:textColor="#000000"
android:text="帮助" android:paddingTop="10px" android:layout_gravity="center" android:layout_margin="10px" />

</LinearLayout>


几乎同时 按下 menu键和back键 (menu在前back在后) 可能爆出下面异常

 WARN/dalvikvm(1682): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.PopupWindow$PopupViewContainer.dispatchKeyEvent(PopupWindow.java:1340)
at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2493)
at android.view.ViewRoot.deliverKeyEvent(ViewRoot.java:2453)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1758)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:144)
at android.app.ActivityThread.main(ActivityThread.java:4937)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
WARN/ActivityManager(97): Force finishing activity com.fone.player/.Launcher
WARN/ActivityManager(97): Activity pause timeout for HistoryRecord{46d6a0a0 com.fone.player/.Launcher}


请问是什么 原因造成的,怎样能避免出现上述问题
...全文
1187 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
obmeflbj 2013-01-11
  • 打赏
  • 举报
回复
引用 5 楼 yitiaochongzi 的回复:
比较完美 解决方案 创建PopupWindow 的时候 不要调用 setFocusable(true); 在showAtLocation后在调用setFocusable(true); 并且在 dismiss的时候 调用setFocusable(false);
我也遇到同样的问题,用5楼的方法解决了,谢了
yitiaochongzi 2012-01-05
  • 打赏
  • 举报
回复
比较完美 解决方案
创建PopupWindow 的时候 不要调用 setFocusable(true);
在showAtLocation后在调用setFocusable(true);

并且在 dismiss的时候 调用setFocusable(false);
coding大叔 2011-12-08
  • 打赏
  • 举报
回复
Popupwindow弹出来,menu的点击事件无效是怎么回事啊 ?怎么解决的楼主?
xiaowangzaixian 2011-10-31
  • 打赏
  • 举报
回复
popup是什么东西。。。
yitiaochongzi 2011-07-10
  • 打赏
  • 举报
回复
算是 已经解决了吧
我将 PopupWindow 的 showAtLocation方法 放到了 Handler 中

还有一点 当 有View 进行动画Animation 时 最好 屏蔽 PopupWindow 的 调用显示

上述 修改暂时 没出现 什么问题
知道分子 2011-07-08
  • 打赏
  • 举报
回复
不明白

/**
* Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
* for this view's window. Returns null if the view is not currently attached
* to the window. Normally you will not need to use this directly, but
* just use the standard high-level event callbacks like {@link #onKeyDown}.
*/
public KeyEvent.DispatcherState getKeyDispatcherState() {
return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
}

应该是这个返回空了。
PopupWindow 里的
private class PopupViewContainer extends FrameLayout
还没attached to the window吧

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧