我为另一个视图的自定义对话框顶部编写了onClick
方法的代码。它给了我nullpointer exception
。我也尝试过使用Layout inflater。它给了我ok.setOnclickListener
的错误。我的代码有什么问题?
ImageButton search =(ImageButton) findViewById(R.id.search);
search.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Dialog searchDialog = new Dialog(Page.this);
/*LayoutInflater inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.search, null);
searchDialog.addContentView(layout, new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
searchDialog.setContentView(layout);*/
searchDialog.setContentView(R.layout.search);
searchDialog.setTitle("Search Dialog");
searchDialog.setCancelable(true);
Button ok = (Button)findViewById(R.id.OkButton);
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String searchString = null;
EditText searchText = (EditText)findViewById(R.id.searchText);
if(searchText.getText()!=null){
searchString = searchText.getText().toString();
}
Log.i("TAG","Search word :"+searchString);
}
});
searchDialog.show();
}
});
答案 0 :(得分:7)
您正在查找创建对话框的活动中的按钮,而不是对话框本身。 findViewById行应为:
Button ok = (Button)searchDialog.findViewById(R.id.OkButton);