0

好的,我在三星 Galaxy Y 和三星 Galaxy Ace Duos 上测试了我的动态壁纸,但是当我在 Xperia E 和 Xperia J 上测试它时,它显示“不幸的是动态壁纸意外停止”。我在具有 API 级别 17 的模拟器上对其进行了测试。它运行良好。

        public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate(); 
   }

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}

/*Method called when the Live Wallpaper loads on device.*/  
@Override
public Engine onCreateEngine() {
    context = this;
    return new MyWallpaperEngine();

}

/*Creating a context to refer to the class.*/
Context context;

/ WallpaperEngine 类的定义/

        private class MyWallpaperEngine extends Engine implements SharedPreferences.OnSharedPreferenceChangeListener{

        /*Instance Variables of the above class.*/  
        private final Handler handler = new Handler(); 
        private SharedPreferences   mPreferences;
        private String              mMode = "random";
        private final Runnable drawRunner = new Runnable() {
        public void run() {
            draw();
        }
        };
        public Bitmap[] image = new Bitmap[26];
        private boolean visible;
        private int i ;


        public MyWallpaperEngine() {


        image[0] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image1);
        image[1] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image2);
        image[2] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image3);
        image[3] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image4);
        image[4] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image5);
        image[5] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image6);
        image[6] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image7);
        image[7] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image8);
        image[8] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image9);
        image[9] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image10);
        image[10] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image11);
        image[11] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image12);
        image[12] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image13);
        image[13] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image14);
        image[14] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image15);
        image[15] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image16);
        image[16] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image17);
        image[17] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image18);
        image[18] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image19);
        image[19] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image20);
        image[20] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image21);
        image[21] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image22);
        image[22] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image23);
        image[23] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image24);
        image[24] = BitmapFactory.decodeResource(getResources(),
                R.drawable.image25);

        mPreferences = LiveWallpaper.this.getSharedPreferences(SHARED_PREFS_NAME, 0);
        mPreferences.registerOnSharedPreferenceChangeListener(this);
        onSharedPreferenceChanged(mPreferences,null); 
        handler.post(drawRunner);
    }

             public void onSharedPreferenceChanged(
            SharedPreferences prefs, String key) {
        mMode = prefs.getString("livewallpaper_mode", "random");

    }

    @Override
    public void onCreate(SurfaceHolder surfaceHolder)
    {
        super.onCreate(surfaceHolder);
        setTouchEventsEnabled(true);

    }


    @Override
    public void onDestroy()
    {
        super.onDestroy();
        handler.removeCallbacks(drawRunner);
        System.gc();
    }

    @Override
    public void onVisibilityChanged(boolean visible) {
        this.visible = visible;
        if (visible) {
            handler.post(drawRunner);
        } else {
            handler.removeCallbacks(drawRunner);
        }
    }

    @Override
    public void onSurfaceCreated(SurfaceHolder holder)
    {
        super.onSurfaceCreated(holder);
    }

    @Override
    public void onSurfaceDestroyed(SurfaceHolder holder) {
        super.onSurfaceDestroyed(holder);
        this.visible = false;
        handler.removeCallbacks(drawRunner);
    }

    @Override
    public void onSurfaceChanged(SurfaceHolder holder, int format,
            int width, int height) {
        super.onSurfaceChanged(holder, format, width, height);
    }

    private void draw() {
        Random random = new Random();// Creates a new random generator.
        SurfaceHolder holder = getSurfaceHolder();//Creates a surface controller
        Canvas canvas = null;//Sets the canvas to null
        try {
            canvas = holder.lockCanvas();
            if (canvas != null) {
                DisplayMetrics metrics =   context.getResources().getDisplayMetrics();
                int width = metrics.widthPixels;//Gets the metrics width
                int height = metrics.heightPixels;//Gets the metrics height
                    if(mMode.compareToIgnoreCase("aerial") == 0){

                         i = AERIAL_MIN + random.nextInt(AERIAL_MAX);
                         canvas.drawBitmap(Bitmap.createScaledBitmap(image[i],
                                 width, height, false), 0, 0, null);
                    }else if(mMode.compareToIgnoreCase("pitchside")==0){
                        i = AERIAL_MAX + random.nextInt(PITCH_MAX-AERIAL_MAX);
                         canvas.drawBitmap(Bitmap.createScaledBitmap(image[i],
                                 width, height, false), 0, 0, null);
                    }else if(mMode.compareToIgnoreCase("stadium")==0){
                        i = PITCH_MAX + random.nextInt(STAD_MAX-PITCH_MAX);
                         canvas.drawBitmap(Bitmap.createScaledBitmap(image[i],
                                 width, height, false), 0, 0, null);
                    }else {
                        i = random.nextInt(STAD_MAX);
                         canvas.drawBitmap(Bitmap.createScaledBitmap(image[i],
                                 width, height, false), 0, 0, null);
                    }       
            }
        } finally {
            if (canvas != null)
                holder.unlockCanvasAndPost(canvas);
        }
        handler.removeCallbacks(drawRunner);
        if (visible) {
            handler.postDelayed(drawRunner, 10000);//Called to delay(pause the thread)     the image by 10000 milliseconds
        }
       }
     }
   }

日志猫:

     07-06 21:50:28.807: W/dalvikvm(14567): threadid=1: thread exiting with uncaught exception (group=0x40cfa498)
     07-06 21:50:28.907: W/BugSenseHandler(14567): Transmitting crash Exception Unable to resolve host "bugsense.appspot.com": No address associated with hostname
     07-06 21:50:31.857: E/test(14567): Exception
     07-06 21:50:31.857: E/test(14567): OutOfMemoryError
     07-06 21:50:32.487: E/dalvikvm(14567): can't open /mnt/sdcard/20130706-162031.hprof: Permission denied
     07-06 21:50:32.497: E/test(14567): writeHprofDataToFile: caught java.lang.RuntimeException: Failure during heap dump; check log output for details
     07-06 21:50:32.507: E/AndroidRuntime(14567): FATAL EXCEPTION: main
     07-06 21:50:32.507: E/AndroidRuntime(14567): java.lang.OutOfMemoryError
     07-06 21:50:32.507: E/AndroidRuntime(14567):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
     07-06 21:50:32.507: E/AndroidRuntime(14567):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500)
     07-06 21:50:32.507: E/AndroidRuntime(14567):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:353)
     07-06 21:50:32.507: E/AndroidRuntime(14567):   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:376)
     07-06 21:50:32.507: E/AndroidRuntime(14567):   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:406)
     07-06 21:50:32.507: E/AndroidRuntime(14567):   at app.sunny.soccerstadiumlivewallpaper.LiveWallpaper$MyWallpaperEngine.<init> (LiveWallpaper.java:118)
     07-06 21:50:32.507: E/AndroidRuntime(14567):   at app.sunny.soccerstadiumlivewallpaper.LiveWallpaper.onCreateEngine(LiveWallpaper.java:59)
     07-06 21:50:32.507: E/AndroidRuntime(14567):   at android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:1012)
     07-06 21:50:32.507: E/AndroidRuntime(14567):   at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:61)
     07-06 21:50:32.507: E/AndroidRuntime(14567):   at android.os.Handler.dispatchMessage(Handler.java:99)
     07-06 21:50:32.507: E/AndroidRuntime(14567):   at android.os.Looper.loop(Looper.java:137)
     07-06 21:50:32.507: E/AndroidRuntime(14567):   at android.app.ActivityThread.main(ActivityThread.java:4792)
     07-06 21:50:32.507: E/AndroidRuntime(14567):   at java.lang.reflect.Method.invokeNative(Native Method)
     07-06 21:50:32.507: E/AndroidRuntime(14567):   at java.lang.reflect.Method.invoke(Method.java:511)
     07-06 21:50:32.507: E/AndroidRuntime(14567):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
     07-06 21:50:32.507: E/AndroidRuntime(14567):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
     07-06 21:50:32.507: E/AndroidRuntime(14567):   at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

您正在将 25 张图像加载到内存中: image[24] = BitmapFactory.decodeResource(getResources(),R.drawable.image25); 那你为什么要这么做?

我认为你永远不会同时显示所有这些(你不能,显示太小了)。因此,请尝试仅按需阅读图像。如果您愿意展示一些拇指,然后创建一些并将这些小图片放入您的 drawable/res 文件夹中。

你的记忆中永远不应该有超过一个图像。

也看看http://developer.android.com/training/best-performance.html

于 2013-07-06T16:57:23.730 回答