将位图设置为墙纸时,应用有时崩溃

时间:2018-08-09 02:46:07

标签: android

我想将位图设置为墙纸时遇到问题... 下面的这段代码有时会起作用,但有时会崩溃....真的很奇怪

fabSetWallpaper.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String url = list.get(position).getImageURL();
                    ImageView img=new ImageView(SecondActivity.this);
                    Picasso.get().load(url).into(img);
                    Bitmap bitmapImg = ((BitmapDrawable) img.getDrawable()).getBitmap();

                    WallpaperManager myWallpaperManager = WallpaperManager
                            .getInstance(getApplicationContext());
                    try {
                        myWallpaperManager
                                .setBitmap(bitmapImg);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    Toast.makeText(SecondActivity.this, "Wallpaper Successfully Set On Screen ", Toast.LENGTH_LONG).show();
                }
            });

当应用崩溃时我得到了这个异常

  

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()”

请帮助我完全被困在那里

1 个答案:

答案 0 :(得分:0)

使用依赖项

  

实现'com.squareup.picasso:picasso:2.71828'

,然后在您的活动onCreate()

   String url = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTe5cLRyavEKkYIBR5DnjHX4eRIb61XiIHbgMh7DU48S6Im-ZIt";


  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final ImageView iv = findViewById(R.id.image_view); // if you have this in your xml

    Picasso.get()
            .load(url)
            .resize(50, 50)
            .centerCrop()
            .into(new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                   // iv.setImageBitmap(bitmap);


                    WallpaperManager myWallpaperManager = WallpaperManager
                            .getInstance(getApplicationContext());
                    try {
                        myWallpaperManager
                                .setBitmap(bitmap);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                @Override
                public void onBitmapFailed(Exception e, Drawable errorDrawable) {
                    Log.d("SIMPLE_CODER","fail");

                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {
                    Log.d("SIMPLE_CODER","onPrepareLoad");
                }
            });
}

在您的AndroidManifest.xml中,不要忘记添加以下权限

<uses-permission android:name="android.permission.INTERNET" /> // only when you are interacting over the internet
<uses-permission android:name="android.permission.SET_WALLPAPER"/>