我有活动按钮。当我单击一个按钮时,它会显示一个 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;
}
}