0

请看下面的代码......我正在创建一个上下文菜单并实现该方法onContextItemSelected,但问题是当我按下回复项时......删除案例上的对话框也会出现并且活动也开始两次。 ..表示所有案例都执行...删除回复和转发案例...发生了什么问题请帮忙

@Override
    public boolean onContextItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        switch(item.getItemId())
        {
    case R.id.reply:
    {
            Intent i = new Intent();
             String s2 = (String) ((Cursor) getListView().getItemAtPosition(info.position))
                    .getString(2);
             i.putExtra("number", s2);
        //   Log.v("number", s2);
             i.setClass(this, CreateMessage.class);
            // i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             startActivity(i);

    }   
        case R.id.delete:
        {

        final String s = (String) ((Cursor) getListView().getItemAtPosition(info.position))
                        .getString(1);

              dba = new DBAdapter(this);

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("Are you sure you want to delete?")
                       .setCancelable(false)
                       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                            //   Log.v("", "You Clicked " + s);             


                            dba.delete("messages", "id=?", new String[] { s });
                            populate();
                            dba.close();
                           }
                       })
                       .setNegativeButton("No", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                           }
                       });
                AlertDialog alert = builder.create();
                alert.show();
        }

        case R.id.forward:
        {
            Intent i = new Intent();
            String s3 = (String) ((Cursor) getListView().getItemAtPosition(info.position))
                    .getString(4);
        //  Log.v("message", s3);
            i.putExtra("message", s3);
            i.setClass(this, CreateMessage.class);
            startActivity(i);
        }
default:

        return super.onContextItemSelected(item);
        }
    }

这是logcat中显示的来自logcat的错误...

03-30 09:13:28.439: E/WindowManager(2273): Activity sms.app.Displayer has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@44eb95c8 that was originally added here

sms.app.Displayer 是我在其中实现上下文菜单的类。

这是上下文菜单文件的代码..

<menu xmlns:android="http://schemas.android.com/apk/res/android" >


    <item android:id="@+id/reply" android:title="Reply"></item><item
        android:id="@+id/forward"
        android:title="Forward">
    </item>
    <item android:id="@+id/delete" android:title="Delete Message">
    </item>

</menu>
4

2 回答 2

1

在你的案例陈述之后添加休息

编辑:由于您错过了休息时间,因此您陷入了以下案例块。要么为你的 case 语句添加中断(并稍后处理返回值),要么直接添加 return true 而不是中断。

于 2012-04-12T09:19:59.103 回答
0

检查此代码后,您的 onContextItemSelected 方法会发生细微变化::

@Override
    public boolean onContextItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        switch(item.getItemId()){
        case R.id.reply:
            Intent i = new Intent();
            String s2 = (String) ((Cursor) getListView().getItemAtPosition(info.position))
                    .getString(2);
            i.putExtra("number", s2);
            //   Log.v("number", s2);
            i.setClass(this, CreateMessage.class);
            // i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
            break;
        case R.id.delete:   

            final String s = (String) ((Cursor) getListView().getItemAtPosition(info.position))
                    .getString(1);

            dba = new DBAdapter(this);

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Are you sure you want to delete?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //   Log.v("", "You Clicked " + s);             


                    dba.delete("messages", "id=?", new String[] { s });
                    populate();
                    dba.close();
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        break;

        case R.id.forward:
            Intent i = new Intent();
            String s3 = (String) ((Cursor) getListView().getItemAtPosition(info.position))
                    .getString(4);
            //  Log.v("message", s3);
            i.putExtra("message", s3);
            i.setClass(this, CreateMessage.class);
            startActivity(i);
        break;
        }
        return true;
    }
于 2012-04-12T09:37:31.703 回答