gpt4 book ai didi

java - 为什么我得到 java.lang.ArithmeticException : divide by zero

转载 作者:行者123 更新时间:2023-12-05 00:16:01 27 4
gpt4 key购买 nike

E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.marsad.wallpaperapp, PID: 20425java.lang.ArithmeticException: divide by zero

嗨,当我点击 gif 图像应用为动态壁纸时,大多数情况下一切正常,但有时应用程序崩溃并出现错误

日志

E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.marsad.wallpaperapp, PID: 20425java.lang.ArithmeticException: divide by zeroat com.marsad.wallpaperapp.GIFWallpaperService$GIFWallpaperEngine.draw(GIFWallpaperService.java:139)at com.marsad.wallpaperapp.GIFWallpaperService$GIFWallpaperEngine.access$000(GIFWallpaperService.java:82)at com.marsad.wallpaperapp.GIFWallpaperService$GIFWallpaperEngine$1.run(GIFWallpaperService.java:111)at android.os.Handler.handleCallback(Handler.java:873)at android.os.Handler.dispatchMessage(Handler.java:99)at android.os.Looper.loop(Looper.java:224)at android.app.ActivityThread.main(ActivityThread.java:7139)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:536)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)

完整代码如下


public class GIFWallpaperService extends WallpaperService {
Context context;


@Override
public WallpaperService.Engine onCreateEngine() {

Singleton b = Singleton.getInstance();
String path = b.getPath();


try {
InputStream is = new FileInputStream(path);
Movie movie = Movie.decodeStream(is);

return new GIFWallpaperEngine(movie);

} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(context, "Error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
return null;
}


}


private class GIFWallpaperEngine extends WallpaperService.Engine {

private final int frameDuration = 20;
private SurfaceHolder holder;
private Movie movie;
private boolean visible;
private Handler handler;

int width;
int height;

public GIFWallpaperEngine(Movie movie) {

this.movie = movie;
handler = new Handler();

}

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

this.holder = surfaceHolder;

}

private Runnable drawGIF = new Runnable() {
@Override
public void run() {
draw();
}
};

private void draw() {

WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
float screenWidth = display.getWidth();
float screenHeight = display.getHeight();

width = (int) screenWidth;
height = (int) screenHeight;


if (visible) {
Canvas canvas = holder.lockCanvas();
canvas.save();


canvas.scale((float) display.getWidth() / (float) movie.width(),
(float) display.getHeight() / (float) movie.height());
movie.draw(canvas, 0, 0);


canvas.restore();

holder.unlockCanvasAndPost(canvas);
movie.setTime((int) (System.currentTimeMillis() % movie.duration()));

handler.removeCallbacks(drawGIF);
handler.postDelayed(drawGIF, frameDuration);

}

}

@Override
public void onVisibilityChanged(boolean visible) {
//super.onVisibilityChanged(visible);

this.visible = visible;

if (visible) {
handler.post(drawGIF);
} else {
handler.removeCallbacks(drawGIF);
}


}

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

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


@Override
public void onDestroy() {
super.onDestroy();
handler.removeCallbacks(drawGIF);

}


}

}

最佳答案

你可以像这样在抽签前检查

if(movie.width() > 0 && movie.height() > 0){
canvas.scale((float) display.getWidth() / (float) movie.width(),
(float) display.getHeight() / (float) movie.height());
movie.draw(canvas, 0, 0);
}else{
//do something default which suit your application
}

关于java - 为什么我得到 java.lang.ArithmeticException : divide by zero,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62691321/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com