Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android I';我试图在第二个线程中为自己添加一个int,但在我的主线程中没有任何变化_Android_Multithreading - Fatal编程技术网

Android I';我试图在第二个线程中为自己添加一个int,但在我的主线程中没有任何变化

Android I';我试图在第二个线程中为自己添加一个int,但在我的主线程中没有任何变化,android,multithreading,Android,Multithreading,我正在制作一张活生生的壁纸。我在尝试多线程处理我的精灵帧更改时遇到了问题。我有一个更新方法和一个绘图方法 public void update() { try { Thread.sleep(500); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace();

我正在制作一张活生生的壁纸。我在尝试多线程处理我的精灵帧更改时遇到了问题。我有一个更新方法和一个绘图方法

public void update() {

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        currentFrame = ++currentFrame % 2;

    }

    public void Draw(Canvas c, int sx, int sy){
        update();
        x = sx;
        y = sy;
        int srcX =  currentFrame * width;
        int srcY = 0;
        Rect src = new Rect(srcX,srcY,srcX+width,srcY+height);
        Rect dst = new Rect(x,y,x+width,y+height);
        c.drawBitmap(b, src, dst, null);
我试着去上课

public class SecondThread extends Thread{
            public void run(){
                seaweed.update();

            }
            }
它在自己的线程中运行更新,因此它们可以在不同的时间长度上睡眠。一个睡50个,另一个睡500个。如果将SecondThread类放在spirte类中,它只会在第一帧上设置,如果我将wallpaper引擎类放入并尝试启动它,它会像一个步骤一样执行,然后崩溃。我在这里做错了什么?

在更新int后尝试调用视图。类似于:

public void update() {

    try {
        Thread.sleep(500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    currentFrame = ++currentFrame % 2;
    YourViewClass.this.postInvalidate();
}

public void Draw(Canvas c, int sx, int sy){
    update();
    x = sx;
    y = sy;
    int srcX =  currentFrame * width;
    int srcY = 0;
    Rect src = new Rect(srcX,srcY,srcX+width,srcY+height);
    Rect dst = new Rect(x,y,x+width,y+height);
    c.drawBitmap(b, src, dst, null);

这是说我的壁纸引擎无法解析为这种类型。