Android 弹出菜单按钮不工作

Android 弹出菜单按钮不工作,android,android-arrayadapter,android-menu,popupmenu,Android,Android Arrayadapter,Android Menu,Popupmenu,我有一个活动按钮。当我点击一个按钮时,它会显示一个弹出菜单。在弹出菜单按钮内,单击我需要在底部页面显示图库。但我收到错误“构造函数MainActivity.ImageAdapter(新的弹出菜单.OnMenuItemClickListener(){})未定义” 代码: main.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://sch

我有一个活动按钮。当我点击一个按钮时,它会显示一个弹出菜单。在弹出菜单按钮内,单击我需要在底部页面显示图库。但我收到错误“
构造函数MainActivity.ImageAdapter(新的弹出菜单.OnMenuItemClickListener(){})未定义

代码:

main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button 
          android:id="@+id/popup_but_id" 
          android:layout_height="wrap_content"           
          android:layout_width="wrap_content" 
          android:text="Popup_button" />

    <Gallery
       android:id="@+id/gallery1"
       android:layout_marginTop="300dp"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content" />

</RelativeLayout>

您正在将OnMenuItemClickListener()的返回类型{}传递给构造函数。ImageAdapter的构造函数采用上下文。使用MainActivity.this引用从上下文扩展的活动的当前实例。

gallery.setAdapter(new ImageAdapter(this)); 在代码的上面一行中,“this”表示弹出菜单项click listener,而Image adapter类中的构造函数需要应用程序上下文。请尝试像下面一行那样提供Mainactivity上下文
setAdapter(新的ImageAdapter(MainActivity.this))

就像斯瓦特说的。您正在匿名内部类(本例中为接口)new OnMenuItemClickListener()中使用关键字this。这样想吧。关键字“this”改变了它的作用域,因为您正在另一个类中使用它。您想引用MainActivity的当前对象,因此,您应该通过让它知道它不是您所指的当前范围来“帮助”该关键字;这是MainActivity的。如果查看PopupMenu popup=new PopupMenu(MainActivity.this,popup_but);这正是您所做的。请尝试使用日志检查ImageAdapter类的getView()函数中setImageResource()处的ImageID[Position]值。还可以尝试在ImageAdapter类Integer[]ImageID={R.drawable.tab,R.drawable.tab1,R.drawable.tab2,R.drawable.tab3,R.drawable.tab4,R.drawable.tab5,R.drawable.tab6,R.drawable.tab7}中声明下面的数组;在任何点击事件上,“this”都表示点击的视图,因此使用Activity.this作为上下文的引用。祝你有美好的一天!
public class MainActivity extends Activity {

    Button popup_but;

    public class ImageAdapter extends BaseAdapter
    {
    Context context;
    int itemBackground;


    public ImageAdapter(Context c)
      {
        context = c;
        //---setting the style---
       // TypedArray a = obtainStyledAttributes(R.styleable.Gallery1); 
       // itemBackground = a.getResourceId(
                           //   R.styleable.Gallery1_android_galleryItemBackground, 0); 

      //  a.recycle();
      }


    //---returns the number of images---
    public int getCount() {
      return imageIDs.length;
     }

    //---returns the item---
    public Object getItem(int position) {
      return position;
     }
    //--returns the ID of an item---
    public long getItemId(int position) {
      return position;
    } 

    //---returns an ImageView view---

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ImageView imageView;
          if (convertView == null) {
            imageView = new ImageView(context);
             //set the ImageView to display image in array
             // at user selected position
            imageView.setImageResource(imageIDs[position]);  
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setLayoutParams(new Gallery.LayoutParams(150, 120)); 

           // imageView.setLayoutParams(new Gallery.LayoutParams(300, 300));

          } else {
            imageView = (ImageView) convertView;
          } 

          imageView.setBackgroundResource(itemBackground);
          return imageView;
    }
}

    Integer[] imageIDs = {
            R.drawable.tab,
            R.drawable.tab1,
            R.drawable.tab2,
            R.drawable.tab3,
            R.drawable.tab4,
            R.drawable.tab5,
            R.drawable.tab6,
            R.drawable.tab7
        };


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

popup_but = (Button) findViewById(R.id.popup_but_id);

        popup_but.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                PopupMenu popup = new PopupMenu(MainActivity.this, popup_but);
                popup.getMenuInflater().inflate(R.menu.main, popup.getMenu());

                popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        // TODO Auto-generated method stub

                        switch(item.getItemId()){

                        case R.id.add:  

                             Gallery gallery=(Gallery)findViewById(R.id.gallery1);

                                gallery.setAdapter(new ImageAdapter(this)); 


                            break;

                        case R.id.sub:
                            break;
                        case R.id.mul:
                            break;
                        case R.id.div:
                            break;


                        }

                        return true;
                    }



                });
                popup.show();

            }


        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}