现在我正在尝试使用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;
}
}
答案 0 :(得分:3)
在onPopupBtClick()中,声明自己的popupListener接口,如下所示:menu.setOnMenuItemClickListener(popupListener );
然后在活动的其他地方(但在onPopupBtClick()之外),实现接口:
private PopupMenu.OnMenuItemClickListener popupListener = new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// etc., etc.,
....
}
}