0

I am having problem of setting wallpaper in android.I have used the following code but its not working .

Here is the code:

  final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
 public void onClick(DialogInterface dialog, int item) {
             // TODO Auto-generated method stub
             try {
                wallpaperManager.setBitmap(i.getDrawingCache());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

                }
        });

  public View getView(int position, View convertView, ViewGroup parent) {

         ImageView i = new ImageView(mContext);
         i.setImageResource(mImageIds[position]);
         i.setScaleType(ImageView.ScaleType.FIT_XY); 

         BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
         drawable.setAntiAlias(true);
         return i;

           }

Could anybody help me!! @Thanks!!

error:

I am getting Null pointer exception:

04-03 12:52:29.344: E/AndroidRuntime(600): FATAL EXCEPTION: main
04-03 12:52:29.344: E/AndroidRuntime(600): java.lang.NullPointerException
04-03 12:52:29.344: E/AndroidRuntime(600):  at com.example.androiddatingapp.gallery$1$1.onClick(gallery.java:74)
04-03 12:52:29.344: E/AndroidRuntime(600):  at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:873)
04-03 12:52:29.344: E/AndroidRuntime(600):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
04-03 12:52:29.344: E/AndroidRuntime(600):  at android.widget.ListView.performItemClick(ListView.java:3513)
04-03 12:52:29.344: E/AndroidRuntime(600):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
04-03 12:52:29.344: E/AndroidRuntime(600):  at android.os.Handler.handleCallback(Handler.java:587)
04-03 12:52:29.344: E/AndroidRuntime(600):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-03 12:52:29.344: E/AndroidRuntime(600):  at android.os.Looper.loop(Looper.java:123)
04-03 12:52:29.344: E/AndroidRuntime(600):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-03 12:52:29.344: E/AndroidRuntime(600):  at java.lang.reflect.Method.invokeNative(Native Method)
04-03 12:52:29.344: E/AndroidRuntime(600):  at java.lang.reflect.Method.invoke(Method.java:507)
04-03 12:52:29.344: E/AndroidRuntime(600):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-03 12:52:29.344: E/AndroidRuntime(600):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-03 12:52:29.344: E/AndroidRuntime(600):  at dalvik.system.NativeStart.main(Native Method)
  • Can you check which of `wallpaperManager` or `i` is null? One of the two are. – Tushar Apr 03 '13 at 07:29
  • @Tushar actually I have some images in array which I have passed using .. i.setImageResource(mImageIds[position]); and i is showing null –  Apr 03 '13 at 07:31
  • Can you post the code where you declare `i`? – Tushar Apr 03 '13 at 07:32
  • @Tushar i have edited .. –  Apr 03 '13 at 07:35
  • May your view dont have cache: http://stackoverflow.com/questions/8939664/getdrawingcache-always-returning-null – neworld Apr 03 '13 at 07:36
  • So I'm not seeing where `i` is set in the scope of the `try/catch`. Can you post the full code surrounding that `try/catch` block and the `onClick` method? – Tushar Apr 03 '13 at 07:36
  • @neworld hence i don't have cache it will not show null pointer exception right!! –  Apr 03 '13 at 07:39
  • @Tushar I have edited with try catch block –  Apr 03 '13 at 07:40
  • @priya2134412 the `i` in your `onClick`...where do you create that particular `i`. You do realize that you can't use the `i` in `getView` in `onClick`, right? – Tushar Apr 03 '13 at 07:41
  • @Tushar..right!! can you edit with all that.. –  Apr 03 '13 at 07:45

1 Answers1

0

Try this.

public void onClick(DialogInterface dialog, int item) {
         // TODO Auto-generated method stub
         try {
            wallpaperManager.setResource(mImageIds[item]);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

            }
    });
sandy
  • 3,311
  • 4
  • 36
  • 47