读书人

经常遇到的Exception以及解决方法

发布时间: 2013-12-10 15:05:55 作者: rapoo

经常遇到的Exception以及解决办法

  1. LayoutInflater mLayoutInflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);??
  2. View view = mLayoutInflater.inflate(R.layout.music_popwindow, null);??
  3. PopupWindow mPopupWindow = new PopupWindow(view, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);??
  4. mPopupWindow.showAtLocation(findViewById(R.id.main), Gravity.RIGHT|Gravity.CENTER, 0, 0);
复制代码

上面的代码是一个PopupWindow的创建过程,但是我们在button的点击事件中运行这段代码却会报空指针,原因在最后一行,showAtLocation(parent, gravity, x, y)方法出了问题,而报NullPointerException的地方正是parent即findViewById(R.id.main)为空,因为这里根本没有获得PopupWindow的parent,第二行代码的View获取了xml布局文件,parent应该在view中实例化,正确的代码应该是mPopupWindow.showAtLocation(view.findViewById(R.id.main), Gravity.RIGHT|Gravity.CENTER, 0, 0);

读书人网 >其他相关

热点推荐