0

Well, I developed a wallpaper app that is working on most of the recent devices. But one of my activity in that app is getting crashed on a few devices that include galaxy J7 prime, Vivo y53. So far I saw these devices crashed my app's activity when I am trying to set wallpaper. Below is the method to set wallpaper.

@RequiresApi(api = Build.VERSION_CODES.N)
private void setWallpaper(String url, int which) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    WallpaperManager wm ;
    InputStream ins ;
    try {
        wm = WallpaperManager.getInstance(getApplicationContext());
        ins = new URL(url).openStream();
        if (which == 0) {
            wm.setStream(ins, null, false, WallpaperManager.FLAG_SYSTEM); // Exception 
        }
        if (which == 1) {
            wm.setStream(ins, null, false, WallpaperManager.FLAG_LOCK); // Exception
        }
        Toast.makeText(SetWallpaperActivity.this, "Wallpaper Set Successfully!", Toast.LENGTH_SHORT).show();
        if(ad.isLoaded()){
            ad.show();
        }
    } catch (IOException e) {
        Toast.makeText(this, "Check your Connection!", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }
}

Actually, it is generating an error when setStream method is invoked. Below is the error details

 02-19 15:18:33.514 5384-5384/com.manas.superwallpz E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.manas.supe, PID: 5384
java.lang.NoSuchMethodError: No virtual method setStream(Ljava/io/InputStream;Landroid/graphics/Rect;ZI)I in class Landroid/app/WallpaperManager; or its super classes (declaration of 'android.app.WallpaperManager' appears in /system/framework/framework.jar)
    at com.manas.superwallpz.activities.SetWallpaperActivity.setWallpaper(SetWallpaperActivity.java:278)
    at com.manas.superwallpz.activities.SetWallpaperActivity.access$600(SetWallpaperActivity.java:49)
    at com.manas.superwallpz.activities.SetWallpaperActivity$3.onClick(SetWallpaperActivity.java:257)
    at android.support.v7.app.AlertController$AlertParams$3.onItemClick(AlertController.java:1068)
    at android.widget.AdapterView.performItemClick(AdapterView.java:305)
    at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
    at android.widget.AbsListView$3.run(AbsListView.java:3860)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5254)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

It is generating an Error called NoSuchMethodError. I am guessing it is something to do with requires Api annotation.

@RequiresApi(api = Build.VERSION_CODES.N)

I am new to android development so I am unable to figure out what's causing this? Your help will make my day. Please, If you are providing a solution then do explain the reason because I love to explore these problems.

Ryan M
  • 18,333
  • 31
  • 67
  • 74

1 Answers1

0

The version of WallpaperManager.setStream that you are calling doesn't exist before API level 24 (Android N). You'd need to check the current API level and on older Android versions, use a method that exists in that API level, such as WallpaperManager.setStream(InputStream)

Ryan M
  • 18,333
  • 31
  • 67
  • 74