对话框一直不显示,错误定在监听器处,但是我实在找不出问题在哪了,请大家帮忙

刘敏smile 发布于 2013/04/19 09:41
阅读 73
收藏 0

我的代码如下:

public class Business_ordered_Activity extends ExpandableListActivity { 

 private TextView mTitleView;
 DataSource_ordered ds;
 ExpandableListView listview;
 SimpleExpandableListAdapter sela;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.business_ordered);
  prepareView();
  mTitleView.setText(R.string.ordered);

  sela = new SimpleExpandableListAdapter
    (this,
      ds.getData_group(), R.layout.groups, new String[]{"group"}, new int[]{R.id.textGroup},
      ds.getData_child(), R.layout.childs, new String[]{"child"}, new int[]{R.id.textChild});
  setListAdapter(sela);
  //注册视图对象,即为ListView控件注册上下文菜单
  registerForContextMenu(listview);
 }
 /**
  * 创建上下文菜单
  */
 @Override
 public void onCreateContextMenu(ContextMenu menu,View view,
   ContextMenuInfo menuInfo){
  MenuInflater mInflater = getMenuInflater();
  mInflater.inflate(R.menu.menu_ordered, menu);
  menu.setHeaderTitle("相关操作");
  super.onCreateContextMenu(menu, view, menuInfo);

 }
 /**
  *当菜单某个选项被点击时调用该方法
  */
 class MyOnMenuItemClickListener implements android.widget.PopupMenu.OnMenuItemClickListener{

  @Override
  public boolean onMenuItemClick(MenuItem item) {
   // TODO Auto-generated method stub
   dialog();
   return false;
  }
  
 }
 @Override
 public boolean onContextItemSelected(MenuItem item){
  
  switch (item.getItemId()) {
  case R.id.ordered_cancel: 
   item.setOnMenuItemClickListener((OnMenuItemClickListener) new MyOnMenuItemClickListener());     
   return true;   
  case R.id.ordered_back:   
   return false;  
  }
  return super.onContextItemSelected(item);
 }
 public void dialog(){
  
  
  LayoutInflater inflater = LayoutInflater.from(Business_ordered_Activity.this);
  final View layView = inflater.inflate(R.layout.alert_ordered_cancel, null);
  
  AlertDialog.Builder builder = new Builder(Business_ordered_Activity.this).setView(layView);
  builder.setTitle(R.string.cancel_order_title);
  builder.setMessage(R.string.cancel_order_message);
  builder.setPositiveButton(R.string.ok, new OnClickListener() {
   
   @Override
   public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
    dialog.dismiss();
    Business_ordered_Activity.this.finish();
   }
  });
  builder.setNegativeButton(R.string.cancel, new OnClickListener() {   
   
   @Override
   public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
    dialog.dismiss();
   }
  } );
  builder.create().show();
 }
 /**
  * 当上下文菜单关闭时调用该方法 
  */
 @Override
 public void onContextMenuClosed(Menu menu){
  super.onContextMenuClosed(menu);
 }
 private void prepareView() {
  mTitleView = (TextView) findViewById(R.id.title_text);
  listview = (ExpandableListView)findViewById(android.R.id.list);
 }


}

 

我debug as的时候在item.setOnMenuItemClickListener((OnMenuItemClickListener) new MyOnMenuItemClickListener()); 

有问题,请大家看看

加载中
返回顶部
顶部