我刚开始学习动态壁纸,我做了这个小东西。
问题是我的应用程序编译没有任何问题。
当我在手机中打开它时,它会在预览中显示壁纸,但是当我单击“设置为壁纸”时,它会以某种方式崩溃,并返回前屏幕,我的壁纸变黑,但它不运行它。
我的猜测是问题出在 onDestory() 的某个地方。
public class Strips extends WallpaperService {
private StripsEngine engine;
@Override
public void onCreate() {
super.onCreate();
engine = new StripsEngine();
engine.resume();
}
@Override
public void onDestroy() {
super.onDestroy();
engine.pause();
}
@Override
public Engine onCreateEngine() {
return engine;
}
class StripsEngine extends Engine implements Runnable {
private Thread t = null;
private SurfaceHolder holder;
private boolean clearToRun = false;
private Paint paint = new Paint();
private Paint paint2 = new Paint();
private int frame;
private float endX,endY;
private int height, width;
private Canvas c;
StripsEngine() {
holder = getSurfaceHolder();
paint.setColor(Color.WHITE);
paint.setStrokeWidth(30);
paint.setAntiAlias(true);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStyle(Paint.Style.STROKE);
paint2 = paint;
paint2.setColor(Color.BLACK);
paint2.setStrokeWidth(40);
frame = 0;
}
void drawStrips() {
}
public void run() {
// TODO Auto-generated method stub
while (clearToRun) {
if (!holder.getSurface().isValid()) {
continue;
}
height = 800;
width = 480;
endX = (frame) % (width + 10);
endY = (frame / 3) % (height + 10);
if (endX == 0)
paint.setARGB(255,(int)(Math.random()*255), (int)(Math.random()*255),(int)(Math.random()*255));
if (endY ==0)
paint.setARGB(255,(int)(Math.random()*255), (int)(Math.random()*255),(int)(Math.random()*255));
c = holder.lockCanvas();
c.drawLine(endX-30, endY-10, endX , endY,paint);
c.drawLine(endX-30, endY-10, endX , endY,paint);
holder.unlockCanvasAndPost(c);
frame = frame +2;
}
}
public void pause() {
clearToRun = false;
while (true) {
try {
t.join();
}
catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
t = null;
}
public void resume() {
clearToRun = true;
t = new Thread(this);
t.start();
}
}
}
07-06 17:14:43.869:E/Surface(478):出队缓冲区出错(未知错误:-19) 07-06 17:14:43.869:E/Surface(478):dequeueBuffer 失败(没有这样的设备) 07-06 17:14:43.869: E/BaseSurfaceHolder(478): 异常锁定表面 07-06 17:14:43.869: E/BaseSurfaceHolder(478): java.lang.IllegalArgumentException 07-06 17:14:43.869: E /BaseSurfaceHolder(478): 在 android.view.Surface.lockCanvasNative(Native Method) 07-06 17:14:43.869: E/BaseSurfaceHolder(478): 在 android.view.Surface.lockCanvas(Surface.java:288) 07 -06 17:14:43.869: E/BaseSurfaceHolder(478): 在 com.android.internal.view.BaseSurfaceHolder.internalLockCanvas(BaseSurfaceHolder.java:132) 07-06 17:14:43.869: E/BaseSurfaceHolder(478):在 com.android.internal.view.BaseSurfaceHolder.lockCanvas(BaseSurfaceHolder.java:112) 07-06 17:14:43.869: E/BaseSurfaceHolder(478):在 com.mendel.strips.Strips$StripsEngine.run(Strips.java:81) 07-06 17:14:43.869: E/BaseSurfaceHolder(478): 在 java.lang.Thread.run(Thread.java:1096) 07-06 17:14:43.972: W/dalvikvm(478): threadid=15: 线程以未捕获的异常退出 (group=0x4001b188) 07-06 17:14:43.979: E/AndroidRuntime(478): 未捕获的处理程序:线程线程 8 由于未捕获的异常而退出 07-06 17:14:43.990: E/AndroidRuntime(478): java.lang.NullPointerException 07-06 17:14:43.990: E/AndroidRuntime(478): at com.mendel。 strips.Strips$StripsEngine.run(Strips.java:82) 07-06 17:14:43.990: E/AndroidRuntime(478): at java.lang.Thread.run(Thread.java:1096) 07-06 17: 14:43.999: I/dalvikvm(478): threadid=7: 对信号 3 做出反应 07-06 17:14:43.999: E/dalvikvm(478): 无法打开堆栈跟踪文件'/data/anr/traces.txt ': 没有权限Strips$StripsEngine.run(Strips.java:81) 07-06 17:14:43.869: E/BaseSurfaceHolder(478): 在 java.lang.Thread.run(Thread.java:1096) 07-06 17:14: 43.972: W/dalvikvm(478): threadid=15: 线程退出未捕获异常 (group=0x4001b188) 07-06 17:14:43.979: E/AndroidRuntime(478): 未捕获处理程序:线程 Thread-8 由于未捕获而退出异常 07-06 17:14:43.990: E/AndroidRuntime(478): java.lang.NullPointerException 07-06 17:14:43.990: E/AndroidRuntime(478): at com.mendel.strips.Strips$StripsEngine.run (Strips.java:82) 07-06 17:14:43.990: E/AndroidRuntime(478): 在 java.lang.Thread.run(Thread.java:1096) 07-06 17:14:43.999: I/dalvikvm (478): threadid=7: 响应信号 3 07-06 17:14:43.999: E/dalvikvm(478): 无法打开堆栈跟踪文件'/data/anr/traces.txt': 权限被拒绝Strips$StripsEngine.run(Strips.java:81) 07-06 17:14:43.869: E/BaseSurfaceHolder(478): 在 java.lang.Thread.run(Thread.java:1096) 07-06 17:14: 43.972: W/dalvikvm(478): threadid=15: 线程退出未捕获异常 (group=0x4001b188) 07-06 17:14:43.979: E/AndroidRuntime(478): 未捕获处理程序:线程 Thread-8 由于未捕获而退出异常 07-06 17:14:43.990: E/AndroidRuntime(478): java.lang.NullPointerException 07-06 17:14:43.990: E/AndroidRuntime(478): at com.mendel.strips.Strips$StripsEngine.run (Strips.java:82) 07-06 17:14:43.990: E/AndroidRuntime(478): 在 java.lang.Thread.run(Thread.java:1096) 07-06 17:14:43.999: I/dalvikvm (478): threadid=7: 响应信号 3 07-06 17:14:43.999: E/dalvikvm(478): 无法打开堆栈跟踪文件'/data/anr/traces.txt': 权限被拒绝81) 07-06 17:14:43.869: E/BaseSurfaceHolder(478): 在 java.lang.Thread.run(Thread.java:1096) 07-06 17:14:43.972: W/dalvikvm(478): threadid =15:线程以未捕获的异常退出(组=0x4001b188)07-06 17:14:43.979:E/AndroidRuntime(478):未捕获的处理程序:线程 Thread-8 由于未捕获的异常而退出 07-06 17:14:43.990: E/AndroidRuntime(478): java.lang.NullPointerException 07-06 17:14:43.990: E/AndroidRuntime(478): 在 com.mendel.strips.Strips$StripsEngine.run(Strips.java:82) 07-06 17:14:43.990: E/AndroidRuntime(478): 在 java.lang.Thread.run(Thread.java:1096) 07-06 17:14:43.999: I/dalvikvm(478): threadid=7: 反应信号 3 07-06 17:14:43.999:E/dalvikvm(478):无法打开堆栈跟踪文件“/data/anr/traces.txt”:权限被拒绝81) 07-06 17:14:43.869: E/BaseSurfaceHolder(478): 在 java.lang.Thread.run(Thread.java:1096) 07-06 17:14:43.972: W/dalvikvm(478): threadid =15:线程以未捕获的异常退出(组=0x4001b188)07-06 17:14:43.979:E/AndroidRuntime(478):未捕获的处理程序:线程 Thread-8 由于未捕获的异常而退出 07-06 17:14:43.990: E/AndroidRuntime(478): java.lang.NullPointerException 07-06 17:14:43.990: E/AndroidRuntime(478): 在 com.mendel.strips.Strips$StripsEngine.run(Strips.java:82) 07-06 17:14:43.990: E/AndroidRuntime(478): 在 java.lang.Thread.run(Thread.java:1096) 07-06 17:14:43.999: I/dalvikvm(478): threadid=7: 反应信号 3 07-06 17:14:43.999:E/dalvikvm(478):无法打开堆栈跟踪文件“/data/anr/traces.txt”:权限被拒绝java:1096) 07-06 17:14:43.972: W/dalvikvm(478): threadid=15: 线程以未捕获的异常退出 (group=0x4001b188) 07-06 17:14:43.979: E/AndroidRuntime(478):未捕获的处理程序:线程 Thread-8 由于未捕获的异常而退出 07-06 17:14:43.990: E/AndroidRuntime(478): java.lang.NullPointerException 07-06 17:14:43.990: E/AndroidRuntime(478): at com.mendel.strips.Strips$StripsEngine.run(Strips.java:82) 07-06 17:14:43.990: E/AndroidRuntime(478): at java.lang.Thread.run(Thread.java:1096) 07 -06 17:14:43.999: I/dalvikvm(478): threadid=7: 响应信号 3 07-06 17:14:43.999: E/dalvikvm(478): 无法打开堆栈跟踪文件'/data/anr /traces.txt':权限被拒绝java:1096) 07-06 17:14:43.972: W/dalvikvm(478): threadid=15: 线程以未捕获的异常退出 (group=0x4001b188) 07-06 17:14:43.979: E/AndroidRuntime(478):未捕获的处理程序:线程 Thread-8 由于未捕获的异常而退出 07-06 17:14:43.990: E/AndroidRuntime(478): java.lang.NullPointerException 07-06 17:14:43.990: E/AndroidRuntime(478): at com.mendel.strips.Strips$StripsEngine.run(Strips.java:82) 07-06 17:14:43.990: E/AndroidRuntime(478): at java.lang.Thread.run(Thread.java:1096) 07 -06 17:14:43.999: I/dalvikvm(478): threadid=7: 响应信号 3 07-06 17:14:43.999: E/dalvikvm(478): 无法打开堆栈跟踪文件'/data/anr /traces.txt':权限被拒绝未捕获的处理程序:线程 Thread-8 由于未捕获的异常而退出 07-06 17:14:43.990: E/AndroidRuntime(478): java.lang.NullPointerException 07-06 17:14:43.990: E/AndroidRuntime(478): at com.mendel.strips.Strips$StripsEngine.run(Strips.java:82) 07-06 17:14:43.990: E/AndroidRuntime(478): at java.lang.Thread.run(Thread.java:1096) 07 -06 17:14:43.999: I/dalvikvm(478): threadid=7: 响应信号 3 07-06 17:14:43.999: E/dalvikvm(478): 无法打开堆栈跟踪文件'/data/anr /traces.txt':权限被拒绝未捕获的处理程序:线程 Thread-8 由于未捕获的异常而退出 07-06 17:14:43.990: E/AndroidRuntime(478): java.lang.NullPointerException 07-06 17:14:43.990: E/AndroidRuntime(478): at com.mendel.strips.Strips$StripsEngine.run(Strips.java:82) 07-06 17:14:43.990: E/AndroidRuntime(478): at java.lang.Thread.run(Thread.java:1096) 07 -06 17:14:43.999: I/dalvikvm(478): threadid=7: 响应信号 3 07-06 17:14:43.999: E/dalvikvm(478): 无法打开堆栈跟踪文件'/data/anr /traces.txt':权限被拒绝运行(Thread.java:1096)07-06 17:14:43.999:I/dalvikvm(478):threadid=7:对信号 3 做出反应 07-06 17:14:43.999:E/dalvikvm(478):无法打开堆栈跟踪文件'/data/anr/traces.txt':权限被拒绝运行(Thread.java:1096)07-06 17:14:43.999:I/dalvikvm(478):threadid=7:对信号 3 做出反应 07-06 17:14:43.999:E/dalvikvm(478):无法打开堆栈跟踪文件'/data/anr/traces.txt':权限被拒绝