0

这是包含4张图片的画廊的项目代码。我设置了一个选项菜单,它将图片设置为墙纸。我尝试运行它,但是当我选择“设置为墙纸”(menuSet)时,它被迫关闭。

private ImageView foto;
private Gallery gallery;

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



    gallery = (Gallery) findViewById(R.id.gallery);
    foto = (ImageView) findViewById(R.id.imageView1);



    gallery.setAdapter(new ImageAdapter(this));

    gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id) {

            switch(position) {

            case 0: foto.setImageResource(R.drawable.ornek1); break;

            case 1: foto.setImageResource(R.drawable.ornek2); break;

            case 2: foto.setImageResource(R.drawable.ornek3); break;

            case 3: foto.setImageResource(R.drawable.ornek4); break;


            }
        }
    });

}
public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;
    private Integer[] mImageIds = {
            R.drawable.ornek1,
            R.drawable.ornek2,
            R.drawable.ornek3,
            R.drawable.ornek4,

         };
    public ImageAdapter(Context c) {
        mContext = c;
        TypedArray attr = mContext.obtainStyledAttributes(R.styleable.GaleriOlusturma);
        mGalleryItemBackground = attr.getResourceId(
                R.styleable.GaleriOlusturma_android_galleryItemBackground, 0);
        attr.recycle();
    }
    public int getCount() {
        return mImageIds.length;
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mImageIds[position]);
        imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setBackgroundResource(mGalleryItemBackground);

        return imageView;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);

    return true;
}

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

    switch (item.getItemId()) {
    case R.id.menuSet:

        Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ornek1);
        WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
        try {
            myWallpaperManager.setBitmap(mBitmap);
            Toast.makeText(NarutoHdWallpapers.this, "Wallpaper set", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            Toast.makeText(NarutoHdWallpapers.this, "Error setting wallpaper", Toast.LENGTH_SHORT).show();
        }

        return true;

    default:
        return super.onOptionsItemSelected(item);
    }

}

}

如果我将这些块更改为简单的 toast 消息,它可以正常工作。

Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ornek1);
        WallpaperManager myWallpaperManager =        WallpaperManager.getInstance(getApplicationContext());
        try {
            myWallpaperManager.setBitmap(mBitmap);
            Toast.makeText(NarutoHdWallpapers.this, "Wallpaper set", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            Toast.makeText(NarutoHdWallpapers.this, "Error setting wallpaper",  Toast.LENGTH_SHORT).show();
        }

**注意:我将权限放入清单

4

0 回答 0