1

现在我正在尝试使用 Android 3.0 (API 11) ECLIPSE 做另一个弹出项目。但我有错误。无法实例化类型 PopupMenu.OnMenuItemClickListener MainActivity.java

&

PopupMenu 类型中的 getMenuInflater() 方法不适用于参数 (int, Object) MainActivity.java)
任何帮助将不胜感激。谢谢。

package com.example.indicationsss;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;

public class MainActivity extends Activity {

     @Override
     public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
     }

     public void onPopupBtClick (View view) {
          PopupMenu menu = new PopupMenu(this, view);
      menu.getMenuInflater().inflate(R.menu.activity_main, menu.getMenu());
      menu.setOnMenuItemClickListener(**new PopupMenu.OnMenuItemClickListener**());
     }

     public boolean onMenuItemClick(MenuItem item) {
          Toast toast = Toast.makeText (MainActivity.this,
            item.getTitle() + "was pressed",
            Toast.LENGTH_SHORT);
          toast.show();
          return true;
     }
}
4

1 回答 1

3

In onPopupBtClick (), declare your own popupListener interface like so: menu.setOnMenuItemClickListener(popupListener );

and then elsewhere in the activity (but outside of onPopupBtClick() ), implement the interface:

private PopupMenu.OnMenuItemClickListener popupListener = new PopupMenu.OnMenuItemClickListener() {
                  @Override
                  public boolean onMenuItemClick(MenuItem item) {
                     // etc., etc., 
                     ....
                  }
    }
于 2012-10-08T18:00:09.020 回答