これは、私がキャンバスに描画するスレッドを実装しているMy Classです。スレッドを既に取得このクラスでエラーを開始しますか?
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;
}
}
私は、このクラスのオブジェクトを別のアクティビティに作成し、描画しています。 今、別のアクティビティに行ってから、このアクティビティに戻ってくると、エラーが発生します。
エラーログ:
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}
私はこのエラーを取得していますなぜ私が知っているdonr。 このスレッドを処理するのを手伝ってください。おかげさまで
ありがとう: –
@iDroidExplorer:私はAndroidの開発について十分に詳しくは分かりませんが、アクティビティのライフサイクルのドキュメントを慎重に見てみることをお勧めします。大幅に再設計したいと思うかもしれません。 –
サンプルコードは本質的にスレッド(surfaceCreated)を開始し、完了(surfaceDestroyed)を待ってから最後に再び開始します(2回目のアクティビティが表示されるときにはsurfaceCreated)。実際には、完了したスレッドでstart()を呼び出すことはできません。直感的には当然ですが。 – wheleph