我试图在Android中制作动态壁纸,但是当方向发生变化时,它会崩溃应用程序。看起来在试图将表壳固定在表面支架上时它会崩溃,但我不知道我能做些什么来阻止它。
这是班级:
public class LiveWallpaperService extends WallpaperService
{
public void onCreate() {
super.onCreate();
}
public void onDestroy() {
super.onDestroy();
}
public Engine onCreateEngine() {
return new MyWallpaperEngine();
}
class MyWallpaperEngine extends Engine
{
private final Handler handler = new Handler();
private final Runnable drawRunner = new Runnable() {
@Override
public void run() {
draw();
}
};
private boolean visible = true;
Paint paint;
MyWallpaperEngine() {
paint = new Paint();
}
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);
}
@Override
public void onVisibilityChanged(boolean visible) {
this.visible = visible;
if (visible) {
handler.post(drawRunner);
}
else {
handler.removeCallbacks(drawRunner);
}
}
@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
this.visible = false;
handler.removeCallbacks(drawRunner);
}
public void onOffsetsChanged(float xOffset, float yOffset, float xStep, float yStep, int xPixels, int yPixels) {
draw();
}
void draw() {
final SurfaceHolder holder = getSurfaceHolder();
Canvas c = null;
try {
c = holder.lockCanvas();
if (c != null) {
// Paint stuff here.
}
}
finally {
if (c != null) {
holder.unlockCanvasAndPost(c);
}
}
handler.removeCallbacks(drawRunner);
if (visible) {
handler.postDelayed(drawRunner, 10);
}
}
}
}
这是方向改变时发生的异常:
E/StudioProfiler: JVMTI error: 15(JVMTI_ERROR_THREAD_NOT_ALIVE)
E/Surface: dequeueBuffer failed (No such device)
E/BaseSurfaceHolder: Exception locking surface
java.lang.IllegalArgumentException
at android.view.Surface.nativeLockCanvas(Native Method)
at android.view.Surface.lockCanvas(Surface.java:318)
at com.android.internal.view.BaseSurfaceHolder.internalLockCanvas(BaseSurfaceHolder.java:194)
at com.android.internal.view.BaseSurfaceHolder.lockCanvas(BaseSurfaceHolder.java:158)
at android.service.wallpaper.WallpaperService$Engine$1.lockCanvas(WallpaperService.java:262)
at greencell.bitpatternswallpaper.LiveWallpaperService$MyWallpaperEngine.draw(LiveWallpaperService.java:206)
at greencell.bitpatternswallpaper.LiveWallpaperService$MyWallpaperEngine$1.run(LiveWallpaperService.java:51)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
更新
我已经检查了许多似乎有相同问题的其他线程,但到目前为止我唯一能做的就是将unlockCanvasAndPost
和lockCanvas
包装在try catch中以忽略{ {1}}。
答案 0 :(得分:2)
在draw()
中,我会尝试在handler.removeCallbacks(drawRunner);
阻止之前移动try
。可能是onOffsetsChanged()
被调用方向更改,并且处理程序上的前一个线程可能尚未调用unlockCanvasAndPost(c)
,这解释了为什么您在lockCanvas()
时收到错误在那时候。但是,如果您在此处发布的代码与您在本地运行的代码完全匹配,则不应该是这种情况,因为您没有覆盖onOffsetsChanged()
。
您可以尝试的另一件事是覆盖onSurfaceChanged()
并清除处理程序队列,如下所示:
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
handler.removeCallbacks(drawRunner);
super.onSurfaceChanged(holder, format, width, height);
}
最终,我在线阅读的所有关于WallpaperService
的示例都有一个带有锁定/解锁画布逻辑的try
- finally
块,所以我不会担心
答案 1 :(得分:2)
尝试android:configChanges="orientation"
清单中的<service android:name=".LiveWallpaperService" />
例如:
<service android:name=".LiveWallpaperService"
android:configChanges="orientation" />