1

我有活动按钮。当我单击一个按钮时,它会显示一个 PopupMenu。在 PopupMenu 按钮内单击我需要在底部页面中显示图库。但我收到错误“ The constructor MainActivity.ImageAdapter(new PopupMenu.OnMenuItemClickListener(){}) is undefined

代码:

主.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>

MainActivity.java:

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;
    }

}
4

2 回答 2

0

您将 OnMenuItemClickListener(){} 的返回类型传递给构造函数。ImageAdapter 的构造函数采用 Context。使用 MainActivity.this 引用从 Context 扩展的 Activity 的当前实例。

于 2013-06-21T15:28:16.550 回答
0

画廊.setAdapter(新的ImageAdapter(这个));在上面的代码行中,“this”表示弹出菜单项单击侦听器,而图像适配器类中的构造函数需要应用程序上下文。尝试像下面的行一样给出 Mainactivity 上下文 gallery.setAdapter(new ImageAdapter(MainActivity.this) );

于 2013-06-21T15:32:32.143 回答