Pregunta

I have button in activity. When i click a button it displays a PopupMenu. Inside PopupMenu button click i need to show gallery in bottom page. But im getting error " The constructor MainActivity.ImageAdapter(new PopupMenu.OnMenuItemClickListener(){}) is undefined "

code:

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>

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

}
¿Fue útil?

Solución

gallery.setAdapter(new ImageAdapter(this)); In the above line of your code "this" signifies pop up menu item click listener, whereas the Constructor in the Image adapter Class requires Application context.Try giving the Mainactivity context like the below line gallery.setAdapter(new ImageAdapter(MainActivity.this));

Otros consejos

You are passing the return type of the OnMenuItemClickListener(){} to the constructor. The constructor for ImageAdapter takes a Context. Use MainActivity.this to reference the current instance of the Activity which extends from Context.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top