在Android中以编程方式设置壁纸

时间:2013-04-03 07:25:34

标签: android imageview wallpaper

我在android中设置壁纸的问题。我使用了以下代码,但它无法正常工作。

以下是代码:

  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;

           }

有人能帮帮我!! @Thanks !!

错误:

我得到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)

1 个答案:

答案 0 :(得分:0)

试试这个。

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();
        }

            }
    });