gpt4 book ai didi

java - 此类上获取线程已启动错误?

转载 作者:行者123 更新时间:2023-12-02 00:34:45 27 4
gpt4 key购买 nike

这是我的类,我在其中实现在 Canvas 上绘制的线程。

    package com.example.drawing;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.example.drawing.DrawingActivity;

public class DrawingSurface extends SurfaceView implements SurfaceHolder.Callback {
public static Boolean _run;
public static DrawThread thread;
public Canvas canvas = null;
private CommandManager commandManager;
//private Bitmap myBitmap;
private Bitmap mBitmap;

public DrawingSurface(Context context, AttributeSet attrs) {
super(context, attrs);

getHolder().addCallback(this);

commandManager = new CommandManager();
thread = new DrawThread(getHolder());
}

class DrawThread extends Thread{
private SurfaceHolder mSurfaceHolder;

public DrawThread(SurfaceHolder surfaceHolder){
mSurfaceHolder = surfaceHolder;

}

public void setRunning(boolean run) {
_run = run;
}

@Override
public void run() {
//Canvas canvas = null;
while (_run){

try{

canvas = mSurfaceHolder.lockCanvas(null);
if(mBitmap == null){
mBitmap = Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
}

final Canvas c = new Canvas (mBitmap);
//canvas.drawColor(0, PorterDuff.Mode.CLEAR);
c.drawColor(0, PorterDuff.Mode.CLEAR);
canvas.drawColor(Color.WHITE);

// Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
// canvas.drawBitmap (kangoo, 0, 200,null);

// works for logo
// Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
// c.drawBitmap (kangoo, 0, 200,null);

if(!(DrawingActivity.imagePath==null)){
c.drawBitmap(DrawingActivity.mBitmap, 0, 0, null);
}
commandManager.executeAll(c);
canvas.drawBitmap (mBitmap, 0, 0,null);

} finally {
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}

public void addDrawingPath (DrawingPath drawingPath){
commandManager.addCommand(drawingPath);
}

public boolean hasMoreRedo(){
return commandManager.hasMoreRedo();
}

public void redo(){
commandManager.redo();
}

public void undo(){
commandManager.undo();
}

public boolean hasMoreUndo(){
return commandManager.hasMoreRedo();
}

public Bitmap getBitmap(){
Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.resize_drawing_logo);
Bitmap myBitmap = addLogo(mBitmap, kangoo);
return myBitmap;
}

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
mBitmap = Bitmap.createBitmap (width, height, Bitmap.Config.ARGB_8888);;
}

public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
//thread.setRunning(true); // Edited By Shreyash
_run=true; //
thread.start();
// error at this line
// if(!thread.isAlive())
// thread.start();
}

public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
boolean retry = true;
//thread.setRunning(false); //Edited By Shreyash
_run = false;
while (retry) {
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
public static Bitmap addLogo(Bitmap mainImage, Bitmap logoImage) { // can add a 3rd parameter 'String loc' if you want to save the new image - left some code to do that at the bottom

Bitmap finalImage = null;
int width, height = 0;
width = mainImage.getWidth();
height = mainImage.getHeight();
finalImage = Bitmap.createBitmap(width, height, mainImage.getConfig());
Canvas canvas = new Canvas(finalImage);
canvas.drawBitmap(mainImage, 0,0,null);
canvas.drawBitmap(logoImage, canvas.getWidth()-logoImage.getWidth() ,canvas.getHeight()-logoImage.getHeight() ,null);

return finalImage;
}


}

我正在另一个 Activity 中创建此类的对象并进行绘图。现在,当我转到另一个 Activity 然后返回此 Activity 时,就会发生错误。

错误日志:

11-15 11:26:40.738: ERROR/AndroidRuntime(668): FATAL EXCEPTION: main
11-15 11:26:40.738: ERROR/AndroidRuntime(668): java.lang.IllegalThreadStateException: Thread already started.
11-15 11:26:40.738: ERROR/AndroidRuntime(668): at java.lang.Thread.start(Thread.java:1322)
11-15 11:26:40.738: ERROR/AndroidRuntime(668): at com.example.drawing.DrawingSurface.surfaceCreated(DrawingSurface.java:119)
11-15 11:26:40.738: ERROR/AndroidRuntime(668): at android.view.SurfaceView.updateWindow(SurfaceView.java:532)
11-15 11:26:40.738: ERROR/AndroidRuntime(668): at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:206)
11-15 11:26:40.738: ERROR/AndroidRuntime(668): at android.view.View.dispatchWindowVisibilityChanged(View.java:3891)
11-15 11:26:40.738: ERROR/AndroidRuntime(668): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-15 11:26:40.738: ERROR/AndroidRuntime(668): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-15 11:26:40.738: ERROR/AndroidRuntime(668): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-15 11:26:40.738: ERROR/AndroidRuntime(668): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-15 11:26:40.738: ERROR/AndroidRuntime(668): at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:719)
11-15 11:26:40.738: ERROR/AndroidRuntime(668): at android.view.ViewRoot.performTraversals(ViewRoot.java:744)
11-15 11:26:40.738: ERROR/AndroidRuntime(668): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
11-15 11:26:40.738: ERROR/AndroidRuntime(668): at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 11:26:40.738: ERROR/AndroidRuntime(668): at android.os.Looper.loop(Looper.java:123)
11-15 11:26:40.738: ERROR/AndroidRuntime(668): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-15 11:26:40.738: ERROR/AndroidRuntime(668): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 11:26:40.738: ERROR/AndroidRuntime(668): at java.lang.reflect.Method.invoke(Method.java:521)
11-15 11:26:40.738: ERROR/AndroidRuntime(668): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-15 11:26:40.738: ERROR/AndroidRuntime(668): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-15 11:26:40.738: ERROR/AndroidRuntime(668): at dalvik.system.NativeStart.main(Native Method)
11-15 11:26:40.768: WARN/ActivityManager(60): Force finishing activity com.example.drawing/.DrawingActivity
11-15 11:26:41.338: WARN/ActivityManager(60): Activity pause timeout for HistoryRecord{450073d0 com.example.drawing/.DrawingActivity}

我不知道为什么会收到此错误。所以请帮我处理这个线程。谢谢。

最佳答案

嗯,正如它所说:您正在一个已经启动的线程上调用 start() 。在一段代码中创建一个线程然后在另一段代码中启动它是非常不寻常的...如果您可以将两者保持在一起,这个问题就不会发生。

(顺便说一句,看起来你的线程基本上是紧密循环的,这对我来说似乎不是一个好主意,但那是另一回事。)

关于java - 此类上获取线程已启动错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8132207/

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