Android线程,切换活动

时间:2014-06-01 10:44:20

标签: java android multithreading android-activity

我在Android上使用Threads时遇到此问题。 我怎样才能杀死一个线程并开始另一个活动?它工作但是当它切换时,给出一个力关闭错误。 这是运行的活动:

public class Game extends Activity implements OnTouchListener{

OurView v;
FileManager fis;
Clock clk;
public float xPos,yPos;
TargetFactory targ = new TargetFactory();
Target target = targ.makeTarget(1);
static int score = 0;
int width = 100;
int height = 200;
static User us  = User.getInstance();



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    v = new OurView(this);
    v.setOnTouchListener(this);
    fis= new FileManager(this);
    setContentView(v);
    clk = new Clock();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    v.resume();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    startActivity(new Intent(Game.this,Result.class));
    v.pause();
}

@Override
public synchronized boolean onTouch(View v, MotionEvent me) {

    xPos = me.getX();
    yPos = me.getY();
    target = targ.makeTarget((int) (Math.random()*20));
    switch(me.getAction()){
    case MotionEvent.ACTION_DOWN:
        if(target.targetMove(xPos, yPos, width, height)){
            score ++;
            us.setScore(score);
        }
        else
        {
            score --;
            us.setScore(score);
        }
        break;
    }

    return false;
}


public static int getScore(){
    return score;
}

public class OurView extends SurfaceView implements Runnable{
        Thread t = new Thread(this);
        SurfaceHolder holder;
        User us = User.getInstance();

        boolean ok = false;
        public OurView(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            holder = getHolder();
        }

        @Override
        public void run() {
            while(ok == true){
                if(Clock.getSeconds() < 5){             
                if(!holder.getSurface().isValid()){
                    continue;
                }
                Canvas c = holder.lockCanvas();
                Paint p = paint(c);
                holder.unlockCanvasAndPost(c);
                }else{
                    pause();
                    continue;

                }
            }       
        }

        public Paint paint(Canvas c) {
            width = c.getWidth();
            height = c.getHeight();
            c.drawARGB(255, 0, 0, 0);
            Paint p = new Paint();
            p.setColor(Color.WHITE);
            target.drawTarget(c);
            c.drawText(""+score, c.getWidth()-20,20, p);
            return p;
        }

        public void pause(){
            ok = false;
            fis.writeToFile(us);
            t.interrupt();

        } 
        public void checkForPaused() {
            while (ok == false) {
                try {
                    Thread.sleep(100);
                } catch (Exception e) {}
            }
    }

        public void resume(){
            target.reset(width, height);
            ok = true;
            t.start();

        }
    }}

这是我试图开始的那个:

public class Result extends Activity {

TextView result;
Button restart;
Button toMenu;
User us = User.getInstance();
FileManager fis;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    fis = new FileManager(this);

    fis.readFromFile();
    setContentView(R.layout.result);


    result = (TextView) findViewById(R.id.score);
    String s = us.getName()+" Result: "+us.getScore();
    result.setText(s);



    restart = (Button) findViewById(R.id.restart);
    restart.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startActivity(new Intent(Result.this,Game.class));

        }
    });


    toMenu = (Button) findViewById(R.id.retmenu);
    toMenu.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startActivity(new Intent(Result.this,Menu.class));

        }
    });
}}

对不起我的不好意思,对不起,如果我写了很多代码,但我不知道问题出在哪里。

LogCat:
06-01 10:51:00.464: E/AndroidRuntime(3302): FATAL EXCEPTION: Thread-10

06-01 10:51:00.464: E/AndroidRuntime(3302): java.lang.IllegalMonitorStateException: object not locked by thread before wait()

06-01 10:51:00.464: E/AndroidRuntime(3302):     at java.lang.Object.wait(Native Method)

06-01 10:51:00.464: E/AndroidRuntime(3302):     at java.lang.Object.wait(Object.java:358)

06-01 10:51:00.464: E/AndroidRuntime(3302):     at com.example.firstone.Game$OurView.pause(Game.java:140)

06-01 10:51:00.464: E/AndroidRuntime(3302):     at com.example.firstone.Game$OurView.run(Game.java:118)

06-01 10:51:00.464: E/AndroidRuntime(3302):     at java.lang.Thread.run(Thread.java:1019)

所以我设法通过调用finish()方法来解决问题。这会停止活动,因此会停止线程。

2 个答案:

答案 0 :(得分:0)

public void checkForPaused() {
            while (ok == false) {
                try {
                    Thread.sleep(100);
                } catch (Exception e) {}
            }

如果他暂停中断,你怎么能睡觉线程?:

  public void pause(){
            ok = false;
            fis.writeToFile(us);
            t.interrupt();

        } 

它的一个。二:当设置标志为false时,你停止线程。中断不需要。

答案 1 :(得分:0)

            try {
                Thread.sleep(100);
            } catch (Exception e) {
                //empty catch blocks are a terrible habit! Please at least use Log.w!
            }

除此之外,您正在滥用Object的Thread状态更改方法。当您在对象上调用interrupt时,它会中断该特定对象实例的Monitor中的线程。当您使用链接到特定对象的“同步”块时,线程进入对象的监视器,例如synchronized(this)。在这种情况下,您假设Thread本身被中断,而不是在跟踪其中的Thread的内容上调用interrupt。因此,当您使用中断时,为什么会收到IllegalMonitorStateException:Thread不在任何Object的Monitor中,因此它是非法状态。

中断线程通常是一种处理事情的坏方法。如果要结束线程,请使用一个标志来告诉线程离开它的循环。