Nullpointer to SurfaceHolder Живые Обои
Эй, ребята, у меня эта ошибка с моими живыми обоями. По какой-то причине, когда я пытаюсь установить тип поверхности, я получаю ошибку NullPointerException. Я не уверен, что там происходит. Заранее спасибо.
Кажется, ошибка в методе SeType().
Вот мой код:
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.hardware.Camera;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.service.wallpaper.WallpaperService.Engine;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import java.lang.reflect.Method;
public class MyWallpaperService extends WallpaperService {
private Handler handler = null;
public Handler getHandler() {
return this.handler;
}
public WallpaperService.Engine onCreateEngine() {
return new MovieLiveWallpeperEngine();
}
public void setHandler(Handler paramHandler) {
this.handler = paramHandler;
}
public class MovieLiveWallpeperEngine extends WallpaperService.Engine {
private static final String TAG = "MovieLiveWallpeperEngine";
public SurfaceHolder hold;
private Camera mCamera = null;
public MovieLiveWallpeperEngine() {
super();
}
@SuppressWarnings("deprecation")
public void onCreate(SurfaceHolder paramSurfaceHolder) {
super.onCreate(paramSurfaceHolder);
this.hold = new MyWallpaperService.VideoSurfaceHolder(getSurfaceHolder());
this.hold.setType(3);
}
public void onDesiredSizeChanged(int paramInt1, int paramInt2) {
}
public void onDestroy() {
if (this.mCamera != null) {
this.mCamera.stopPreview();
this.mCamera.setPreviewCallback(null);
this.mCamera.release();
}
super.onDestroy();
}
public void onOrientationChanged(int paramInt) {
}
public void onVisibilityChanged(boolean paramBoolean) {
if (paramBoolean)
startVideo();
while (true) {
//return;
stopVideo();
}
}
public void setDisplayOrientation(Camera paramCamera, int paramInt) {
try {
Class localClass = paramCamera.getClass();
Class[] arrayOfClass = new Class[1];
arrayOfClass[0] = Integer.TYPE;
Method localMethod = localClass.getMethod(
"setDisplayOrientation", arrayOfClass);
if (localMethod != null) {
Object[] arrayOfObject = new Object[1];
arrayOfObject[0] = Integer.valueOf(paramInt);
localMethod.invoke(paramCamera, arrayOfObject);
}
label58: return;
} catch (Exception localException) {
//break;
}
}
public void startVideo() {
int i = MyWallpaperService.this.getResources().getConfiguration().orientation;
try {
this.mCamera = Camera.open();
if (this.mCamera != null) {
if (i == 1)
setDisplayOrientation(this.mCamera, 90);
if (i == 2)
setDisplayOrientation(this.mCamera, 0);
this.mCamera.setPreviewDisplay(this.hold);
this.mCamera.startPreview();
}
return;
} catch (Exception localException) {
while (true)
Log.e("MovieLiveWallpeperEngine", "Error : "
+ localException.getMessage());
}
}
public void stopVideo() {
if (this.mCamera != null) {
this.mCamera.stopPreview();
this.mCamera.setPreviewCallback(null);
this.mCamera.release();
this.mCamera = null;
}
}
}
public class VideoSurfaceHolder implements SurfaceHolder {
private SurfaceHolder surfaceHolder;
public VideoSurfaceHolder(SurfaceHolder arg2) {
Object localObject = null;
this.surfaceHolder = (SurfaceHolder) localObject;
}
public void addCallback(SurfaceHolder.Callback paramCallback) {
this.surfaceHolder.addCallback(paramCallback);
}
public Surface getSurface() {
return this.surfaceHolder.getSurface();
}
public Rect getSurfaceFrame() {
return this.surfaceHolder.getSurfaceFrame();
}
public boolean isCreating() {
return this.surfaceHolder.isCreating();
}
public Canvas lockCanvas() {
return this.surfaceHolder.lockCanvas();
}
public Canvas lockCanvas(Rect paramRect) {
return this.surfaceHolder.lockCanvas(paramRect);
}
public void removeCallback(SurfaceHolder.Callback paramCallback) {
this.surfaceHolder.removeCallback(paramCallback);
}
public void setFixedSize(int paramInt1, int paramInt2) {
this.surfaceHolder.setFixedSize(paramInt1, paramInt2);
}
public void setFormat(int paramInt) {
this.surfaceHolder.setFormat(paramInt);
}
public void setKeepScreenOn(boolean paramBoolean) {
}
public void setSizeFromLayout() {
this.surfaceHolder.setSizeFromLayout();
}
@SuppressWarnings("deprecation")
public void setType(int paramInt) {
this.surfaceHolder.setType(paramInt);
}
public void unlockCanvasAndPost(Canvas paramCanvas) {
this.surfaceHolder.unlockCanvasAndPost(paramCanvas);
}
}
}
Вот моя ошибка Logcat:
03-08 00:38:37.793: D/AndroidRuntime(20508): Shutting down VM
03-08 00:38:37.793: W/dalvikvm(20508): threadid=1: thread exiting with uncaught exception (group=0x4203e8b0)
03-08 00:38:37.803: E/AndroidRuntime(20508): FATAL EXCEPTION: main
03-08 00:38:37.803: E/AndroidRuntime(20508): java.lang.NullPointerException
03-08 00:38:37.803: E/AndroidRuntime(20508): at com.gordondev.copytransparent.MyWallpaperService$VideoSurfaceHolder.setType(MyWallpaperService.java:173)
03-08 00:38:37.803: E/AndroidRuntime(20508): at com.gordondev.copytransparent.MyWallpaperService$MovieLiveWallpeperEngine.onCreate(MyWallpaperService.java:45)
03-08 00:38:37.803: E/AndroidRuntime(20508): at android.service.wallpaper.WallpaperService$Engine.attach(WallpaperService.java:777)
03-08 00:38:37.803: E/AndroidRuntime(20508): at android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:1031)
03-08 00:38:37.803: E/AndroidRuntime(20508): at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:40)
03-08 00:38:37.803: E/AndroidRuntime(20508): at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 00:38:37.803: E/AndroidRuntime(20508): at android.os.Looper.loop(Looper.java:213)
03-08 00:38:37.803: E/AndroidRuntime(20508): at android.app.ActivityThread.main(ActivityThread.java:5225)
03-08 00:38:37.803: E/AndroidRuntime(20508): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 00:38:37.803: E/AndroidRuntime(20508): at java.lang.reflect.Method.invoke(Method.java:525)
03-08 00:38:37.803: E/AndroidRuntime(20508): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-08 00:38:37.803: E/AndroidRuntime(20508): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
03-08 00:38:37.803: E/AndroidRuntime(20508): at dalvik.system.NativeStart.main(Native Method)
03-08 00:38:53.103: I/Process(20508): Sending signal. PID: 20508 SIG: 9
1 ответ
// проблема в том, что вы делаете localObject равным нулю и назначаете null
к surfaceHolder
public VideoSurfaceHolder(SurfaceHolder arg2) {
Object localObject = null;
this.surfaceHolder = (SurfaceHolder) localObject;
}
// попробуйте назначить getSurfaceHolder
метод к surfaceHolder
this.surfaceHolder = arg2;