Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么不根据位置设置背景[android]_Android - Fatal编程技术网

为什么不根据位置设置背景[android]

为什么不根据位置设置背景[android],android,Android,在下面的代码中,当点击按钮时,不会根据Imgid的位置设置背景图像,只会将图像的第一个(p1.png)设置为背景 为什么不根据Imgid的位置设置背景图像 public class Main extends Activity{ private Gallery gallery; private ImageView imgView; private int[] Imgid = { R.drawable.p1, R.drawable.p2, R.draw

在下面的代码中,当点击按钮时,不会根据Imgid的位置设置背景图像,只会将图像的第一个(p1.png)设置为背景

为什么不根据Imgid的位置设置背景图像

public class Main extends Activity{
    private Gallery gallery;
    private ImageView imgView;
    private int[] Imgid = {
            R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4
    };
    int position;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        imgView = (ImageView)findViewById(R.id.ImageView01);    
        imgView.setImageResource(Imgid[0]);

         gallery = (Gallery) findViewById(R.id.examplegallery);
         gallery.setAdapter(new AddImgAdp(this));

         gallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                imgView.setImageResource(Imgid[position]); 

            }
        });
         Button b =(Button) findViewById(R.id.button1);
         b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                setback(position);

            }
        });
    }

    public void setback (int position){
    WallpaperManager wallpaperManager = WallpaperManager.getInstance(Main.this); 
        Drawable drawable = getResources().getDrawable(Imgid[position]);
                        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                        try {
                        wallpaperManager.setBitmap(bitmap);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
    }

    public class AddImgAdp extends BaseAdapter {
        int GalItemBg;
        private Context cont;

        public AddImgAdp(Context c) {
            cont = c;
            TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
            GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
            typArray.recycle();
        }

        public int getCount() {
            return Imgid.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 imgView = new ImageView(cont);

            imgView.setImageResource(Imgid[position]);
            imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
            imgView.setScaleType(ImageView.ScaleType.FIT_XY);
            imgView.setBackgroundResource(GalItemBg);

            return imgView;
        }
    }
}

你同意了吗

 <uses-permission android:name="android.permission.SET_WALLPAPER"/>


在清单中创建一个
mPosition
全局变量并初始化
mPosition
变量

 gallery.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id) {
        imgView.setImageResource(Imgid[position]); 
         mPosition=position;
    }
});
然后使用
mPosition
like

 b.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        setback(mPosition);
    }
});

这是因为您的
位置
值未初始化。您需要将位置值初始化为
setOnItemClickListener(…)
。请按照我的答案进行尝试。