android popupWindow问题,代码错误
publicclassTestLayoutextendsActivity{privateButtonbtn;privatePopupWindowpopupWindow;p...
public class TestLayout extends Activity {
private Button btn;
private PopupWindow popupWindow;
private Button mBtnConfirm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test_layout, menu);
return true;
}
public void init(){
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
View contentView = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.popup, null);
// 设置popupWindow的背景颜色
contentView.setBackgroundColor(Color.GRAY);
// 声明一个弹出框
popupWindow = new PopupWindow(findViewById(R.id.mainlayout), 200, 200);
// 为弹出框设定自定义的布局
popupWindow.setContentView(contentView);
popupWindow.setFocusable(true); // 很重要,获取焦点
popupWindow.showAsDropDown(btn);
mBtnConfirm=(Button)findViewById(R.id.btnConfirm);
mBtnConfirm.setOnClickListener(new OnClickListener(){ //!!!!!!!!!!!!!!!!!!这里报错啊
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}
});
}
});
}
}
这是我测试popupwindow的代码,layout文件没问题,每次运行,上面标记的地方就出错(求指导啊),onClickListener() 展开
private Button btn;
private PopupWindow popupWindow;
private Button mBtnConfirm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test_layout, menu);
return true;
}
public void init(){
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
View contentView = LayoutInflater.from(getApplicationContext())
.inflate(R.layout.popup, null);
// 设置popupWindow的背景颜色
contentView.setBackgroundColor(Color.GRAY);
// 声明一个弹出框
popupWindow = new PopupWindow(findViewById(R.id.mainlayout), 200, 200);
// 为弹出框设定自定义的布局
popupWindow.setContentView(contentView);
popupWindow.setFocusable(true); // 很重要,获取焦点
popupWindow.showAsDropDown(btn);
mBtnConfirm=(Button)findViewById(R.id.btnConfirm);
mBtnConfirm.setOnClickListener(new OnClickListener(){ //!!!!!!!!!!!!!!!!!!这里报错啊
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}
});
}
});
}
}
这是我测试popupwindow的代码,layout文件没问题,每次运行,上面标记的地方就出错(求指导啊),onClickListener() 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询