如果我的 Button 已初始化,我不知道为什么它会得到 NullPointerException。这是我的代码。希望大家能帮忙。谢谢
public boolean onOptionsItemSelected(MenuItem item)
{ switch (item.getItemId())
{
case SEARCH:
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
AlertDialog.Builder search = new AlertDialog.Builder(this);
search.setTitle(R.string.search);
searchDialog = inflater.inflate(R.layout.search,null);
search.setView(searchDialog);
//The problem should be here
searchButton = (Button) findViewById(R.id.searchButton);
//R.id.searchButton is inside the layout, search.xml
searchButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{ //For testing
Toast.makeText(getApplicationContext(),
"Search was clicked!", Toast.LENGTH_SHORT).show();
}
});
AlertDialog searchDialog = search.create();
searchDialog.show();
return true;
.
.
.