我的项目中有一个Imageview和一个按钮,我希望按钮将图像设置为主屏幕背景,但我收到错误“imageView”无法在行中解析“Bitmap bitmap =((BitmapDrawable)imageView.getDrawable ())getBitmap();”。
package com.example.androidhive;
import java.io.IOException;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class FullImageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
}
public void setaswall(View view) { // SET AS WALLPAPER BUTTON
// TODO Auto-generated method stub
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
Bitmap bitmap=((BitmapDrawable)imageView.getDrawable()).getBitmap();
if(bitmap!=null)
myWallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
public void setaswall(View view) { // SET AS WALLPAPER BUTTON
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
};
}
答案 0 :(得分:0)
您必须将imageView声明为全局,因此当您在setaswall(View视图)上使用它时,您可以将其设置在范围内,而不是仅在onCreate()中创建它
像这样:
public class FullImageActivity extends Activity {
private ImageView _imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
然后,在setaswall(查看视图)上:
Bitmap bitmap=((BitmapDrawable)_imageView.getDrawable()).getBitmap();
if(bitmap!=null)
myWallpaperManager.setBitmap(bitmap);