在活动之间移动

时间:2014-06-11 16:51:05

标签: java android multithreading android-intent android-activity

我有几项活动。在mainActivity我有4个按钮,每个按钮都会导致另一个活动。 在三个活动中,我激活了一个绘制画布的线程,在每个活动中我都有一个按钮返回mainActivity。我尝试过使用:

                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                // i tried also (theClassName.this,MainActivity.Class)
                startActivity(i);

当我点击返回按钮时,我确实返回mainActivity但应用程序停止响应(它提醒我应用程序没有响应,我必须在等待或退出应用程序之间做出选择等待没有帮助。

当我使用

finish();

代替,

然后点击返回按钮,我返回设备的主屏幕(退出应用程序)但没有退出 - 我仍然可以使用任务管理器返回应用程序,当我做了它让我回到我输入的活动而不是mainActivity

正如我所说,mainActivity上有4个按钮,当我使用第四个按钮时,它转到另一个没有激活线程的活动,它只有listView,当我从它返回时到mainActivity它确实有效。

我该如何解决?谢谢你的帮助

*出现问题时,没有错误堆栈跟踪。

*如果您需要查看任何代码,我很乐意在此处发布

代码:

MainActivity:

   public class MainActivity extends Activity {

    Button classic;
    Button vsPlayer;
    Button vsComp;
//  Button settings;
    Button scores;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i=new Intent(MainActivity.this,ClassicActivity.class);
                startActivity(i);
                }
        });

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

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i=new Intent(MainActivity.this,VsPlayerActivity.class);
                startActivity(i);
                }
        });

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

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i=new Intent(MainActivity.this,VsCompActivity.class);
                startActivity(i);
                }
        });

       /* settings=(Button) findViewById(R.id.Settings);
        settings.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i=new Intent(MainActivity.this,SettingsActivity.class);
                startActivity(i);
                }
        });
*/
        scores=(Button) findViewById(R.id.Scores);
        scores.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i=new Intent(MainActivity.this,ScoresActivity.class);
                startActivity(i);
                }
        });

    }//onCreate

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.mains, menu);
        return true;
    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        MainActivity.this.finish();
    }

}

- = ------其他一些活动:

 public class ClassicActivity extends Activity  {
    Classic g;

    ImageView left;
    ImageView right;
    ImageView up;
    ImageView down;

    TextView pause;
    TextView back;

    TextView scoretv;

    LinearLayout surface;

    LinearLayout screen;

    LinearLayout stoped;
    LinearLayout gameover;

    ScoreDataSource sds;

    Button no;
    Button yes;
    EditText name;
    Button continueb;
    Button leave;

     //protected static TextView MyScore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /*requestWindowFeature(Window.FEATURE_NO_TITLE);
        v= new GameView1(this);
        setContentView(v);
        */

        setContentView(R.layout.activity_classic);
        sds=new ScoreDataSource(this);
        gameover=(LinearLayout) findViewById(R.id.gameover);
        screen = (LinearLayout)findViewById(R.id.screen);
        surface = (LinearLayout)findViewById(R.id.surface);
        g= new Classic(this, surface);
        surface.addView(g);

        //MyScore=(TextView) findViewById(R.id.MyScore);
        //MyScore.setText("My score:"+gv.getPoints());

        left=(ImageView) findViewById(R.id.left);
        right=(ImageView) findViewById(R.id.right);
        up=(ImageView) findViewById(R.id.up);
        down=(ImageView) findViewById(R.id.down);

        left.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(g.snake.getDir()!="LEFT"&&g.snake.getDir()!="RIGHT"){
                g.snake.Left();
                }
            }
        });
        right.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(g.snake.getDir()!="RIGHT"&&g.snake.getDir()!="LEFT"){
                g.snake.Right();    
                }
            }
        });
        up.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(g.snake.getDir()!="UP"&&g.snake.getDir()!="DOWN"){
                g.snake.Up();   
                }
            }
        });
        down.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(g.snake.getDir()!="DOWN"&&g.snake.getDir()!="UP"){
                g.snake.Down();
                }
            }
        });

        pause=(TextView) findViewById(R.id.Pause);
        back=(TextView) findViewById(R.id.Back);

        stoped=(LinearLayout) findViewById(R.id.stoped);

        pause.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(g.getRunning()){
                    pause.setText("continue");
                    g.setRunning(false);
                }
                else{
                    pause.setText("pause");
                    g.setRunning(true);
                    new Thread(g.snakethread).start();
                }

            }
        });

        back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                g.setRunning(false);
                stoped.setVisibility(View.VISIBLE);
            }
        });

        no=(Button) findViewById(R.id.no);
        yes=(Button) findViewById(R.id.yes);
        continueb=(Button) findViewById(R.id.Continue);
        leave=(Button) findViewById(R.id.leave);

        no.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });

        name=(EditText) findViewById(R.id.name);
        yes.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Calendar c = Calendar.getInstance();
                SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                String date = df.format(c.getTime());

                sds.open();
                Score s=new Score(0,name.getText().toString(),date, g.getPoints(),"Classic");
                s=sds.createScore(s);
                sds.close();
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(i);
            }
        });
        continueb.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                stoped.setVisibility(View.GONE);
                g.setRunning(true);
                new Thread(g.snakethread).start();
            }
        });
        leave.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(i);
            }
        });

        scoretv=(TextView) findViewById(R.id.scoretv);

        screen.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(g.getGameover()){
                    scoretv.setText("Your score is:"+g.getPoints());
                    gameover.setVisibility(View.VISIBLE);
                    g.setGameover(false);
                }
                }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

---- ClassicActivity使用此活动:

    public class Classic extends SurfaceView implements Runnable {

    private boolean isRunning=false;
    private final long FPS=8;//MAX is 16
    final float scale = Resources.getSystem().getDisplayMetrics().density;
    float PART_SIZE= 10*scale;

    private SurfaceHolder holder;
    Thread snakethread;
    static SnakePlayer snake;
    Arena a;
    LinearLayout view;
    Bitmap arena;
    Food food1;

    Boolean gameover;

    int points;
    Paint writePB=new Paint();
    Paint writePR=new Paint();



    public Classic(Context context,LinearLayout view) {
        super(context);
        this.gameover=false;
        this.view=view;
        this.a=new Arena(view,PART_SIZE,33,36,1,0);
        this.food1=new Food(PART_SIZE,"GRAY");
        points=0;
        writePB.setColor(Color.BLACK);
        writePB.setTextSize(PART_SIZE);
        writePR.setColor(Color.RED);
        writePR.setTextSize(PART_SIZE);
        snakethread=new Thread(this);
        holder= getHolder();

        holder.addCallback(new SurfaceHolder.Callback(){

            @Override
            public void surfaceCreated(SurfaceHolder arg0) {
                setRunning(true);
                snakethread.start();
            }

            @Override
            public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,int arg3) {



            }

            @Override
            public void surfaceDestroyed(SurfaceHolder arg0) {

                setRunning(false);

                while(true){
                    try {
                        snakethread.join();
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
    });
        snake=new SnakePlayer(0+3*PART_SIZE, 0+3*PART_SIZE, "RIGHT",PART_SIZE,2,"RED","BLACK");
}

    @SuppressLint("WrongCall")
    @Override
    public void onDraw(Canvas c) {
        a.onDraw(c);
        c.drawText("Your Score:"+points, 5*scale, 10*scale, writePB);
        food1.drawFood(c);
        snake.onDraw(c);
    }
    @SuppressLint("WrongCall")
    @Override

    public void run() {
        long stepPerSecond=1000/FPS;
        long startTime;
        long sleepTime;
        Canvas c = null;
        while(isRunning){
            startTime=System.currentTimeMillis();
            c = null;
            try{
                c=this.getHolder().lockCanvas();
                onDraw(c);

            }
            catch(Exception e){
            }
            finally{
                if(c!=null){
                    this.getHolder().unlockCanvasAndPost(c);
                }
            }
            AllChecks(c);
            sleepTime= stepPerSecond-(System.currentTimeMillis()-startTime);
            if(sleepTime>0)
                try {
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
        if(gameover){
            try{
                c=this.getHolder().lockCanvas();
                c.drawText("GAMEOVER", 125*scale, 150*scale, writePR);
                c.drawText("Touch Anywhere To Continue ", 87*scale, 162*scale, writePR);
                }
            catch(Exception e){                 
            }
            finally{
                if(c!=null){
                    this.getHolder().unlockCanvasAndPost(c);
                    }
                }
            }
        }
    boolean firstTime=true;
    public void AllChecks(Canvas c){
        /*
         * Call all the right function, to check if the snake has bumped in a wall, or eaten a food.
         */
        if(Arena.WallCollision(snake.getxLoc(), snake.getyLoc())){
            setRunning(false);
            gameover=true;
        }
        if(snake.snakeCollisionNoHead(snake.getxLoc(), snake.getyLoc())){
            setRunning(false);
            gameover=true;
        }
        if(firstTime){
            firstTime=false;
            changeFoodLoc(food1);
        }
        if(food1.hasEaten(snake.getxLoc(), snake.getyLoc())){
            changeFoodLoc(food1);
            snake.Add();
            points+=10;
        }
    }
    public void changeFoodLoc(Food food){
        /*
         * if a snake has eaten a food it changes the location of the food randomly
         */
        Random rnd = new Random();
        food.x=(rnd.nextInt(32) + 3)*food.SIZE;
        food.y=(rnd.nextInt(35) + 1)*food.SIZE;
        if(snake.snakeCollision(food.getX(), food.getY()))
            changeFoodLoc(food);    
    }
    public void setRunning(boolean b){
        isRunning=b;
    }
    public Boolean getRunning(){
        return isRunning;
    }

    public int getPoints() {
        return points;
    }

    public void setPoints(int points) {
        this.points = points;
    }

    public Boolean getGameover() {
        return gameover;
    }

    public void setGameover(Boolean gameover) {
        this.gameover = gameover;
    }   




}

1 个答案:

答案 0 :(得分:1)

首先,我会尝试删除onPause的MainActivity上的覆盖 - 您不需要调用finish。在第二个活动中,如果您想回到第二个活动的MainActivity-呼叫结束,它应该返回到MainActivity。