我在网上跟随教程,遇到一个问题,我正在努力找出Null对象在哪里,就像我在Android手机中设置动态壁纸时一样。只是说“不幸的是,晨动态壁纸已经停止”
我试图找出解决方案,但无法获得解决方案。即使我尝试调试代码,但应用程序也未得到调试。
import android.graphics.Canvas;
import android.graphics.Movie;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.SurfaceHolder;
import java.io.IOException;
import android.os.Handler;
public class GIFWallpaperService extends WallpaperService {
@Override
public WallpaperService.Engine onCreateEngine() {
try{
Movie movie = Movie.decodeStream(getResources().getAssets().open("image.gif"));
System.out.println("This is 1111111111");
return new GIFWallpaperEngine(movie);
} catch (IOException e){
Log.d("GIF", "Could not load Assets");
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;
public GIFWallpaperEngine(Movie movie){
this.movie = movie;
handler = new Handler();
}
public void onCreate(SurfaceHolder surfaceHolder){
super.onCreate(surfaceHolder);
this.holder = surfaceHolder;
System.out.println("This is 2222");
}
private Runnable drawGIF = new Runnable() {
@Override
public void run() {
draw();
}
};
private void draw(){
if(visible){
Canvas canvas = holder.lockCanvas();
canvas.save();
canvas.scale(4f,4f);
movie.draw(canvas,-100,0);
canvas.restore();
holder.unlockCanvasAndPost(canvas);
movie.setTime((int)(System.currentTimeMillis() % movie.duration()));
System.out.println("This is 333333");
handler.removeCallbacks(drawGIF);
handler.postDelayed (drawGIF,frameDuration);
}
}
public void onVisibilityChanged(boolean visible){
this.visible = visible;
if(visible){
handler.post(drawGIF);
}else{
handler.removeCallbacks(drawGIF);
}
}
@Override
public void onDestroy() {
super.onDestroy();
handler.removeCallbacks(drawGIF);
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="livewallpaper.digital.com.morninglivewallpaper">
<uses-feature
android:name="android.software.live_wallpaper"
android:required="true">
</uses-feature>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<service android:name=".GIFWallpaperService"
android:enabled="true"
android:label="GIF Wallpaper"
android:permission="android.permission.BIND_WALLPAPER">
<intent-filter>
<action android:name="android.service.wallpaper.WallpaperService" />
</intent-filter>
<meta-data
android:name="android.service.wallpaper"
android:resource="@xml/wallpaper">
</meta-data>
</service>
</application>
</manifest>
这是Logcat
01-29 02:56:39.845 1308-1308/? D/dalvikvm: Late-enabling CheckJNI
01-29 02:56:39.857 1308-1308/? D/GIF: Could not load Assets
01-29 02:56:39.857 1308-1308/? D/AndroidRuntime: Shutting down VM
01-29 02:56:39.861 1308-1308/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa6147908)
01-29 02:56:39.861 1308-1314/? E/jdwp: Failed sending reply to debugger: Broken pipe
01-29 02:56:39.861 1308-1314/? D/dalvikvm: Debugger has detached; object registry had 1 entries
01-29 02:56:39.861 1308-1308/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:1037)
at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:40)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
try{
Movie movie = Movie.decodeStream(getResources().getAssets().open("image.gif"));
System.out.println("This is 1111111111");
return new GIFWallpaperEngine(movie);
} catch (IOException e){
Log.d("GIF", "Could not load Assets");
return null;
}
对该代码段进行一些调试,检查是否未发生异常,否则引擎实例将为null,也可能是您造成NPE的原因