无法弄清楚为什么我的Android应用重置按钮无法正常工作

时间:2012-12-20 23:23:34

标签: android

我今天一直在研究这个应用程序,我似乎无法弄清楚代码有什么问题。

我可以在模拟器和我的摩托罗拉Bravo(2.2)上运行应用程序,但是当我完成一轮并按下“重置”按钮时,它突然给我一个错误,程序不再起作用。有什么方法可以解决这个问题吗?

这里是logcat错误(整个红色部分):

12-20 23:01:27.636: E/StrictMode(585): null
12-20 23:01:27.636: E/StrictMode(585): android.app.ServiceConnectionLeaked: Service     com.android.exchange.ExchangeService has leaked ServiceConnection     com.android.emailcommon.service.ServiceProxy$ProxyConnection@40ceb270 that was originally     bound here
12-20 23:01:27.636: E/StrictMode(585):  at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
12-20 23:01:27.636: E/StrictMode(585):  at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
12-20 23:01:27.636: E/StrictMode(585):  at android.app.ContextImpl.bindService(ContextImpl.java:1418)
12-20 23:01:27.636: E/StrictMode(585):  at android.app.ContextImpl.bindService(ContextImpl.java:1407)
12-20 23:01:27.636: E/StrictMode(585):  at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
12-20 23:01:27.636: E/StrictMode(585):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
12-20 23:01:27.636: E/StrictMode(585):  at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
12-20 23:01:27.636: E/StrictMode(585):  at com.android.emailcommon.service.AccountServiceProxy.getDeviceId  (AccountServiceProxy.java:116)
12-20 23:01:27.636: E/StrictMode(585):  at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
12-20 23:01:27.636: E/StrictMode(585):  at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
12-20 23:01:27.636: E/StrictMode(585):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
12-20 23:01:27.636: E/StrictMode(585):  at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
12-20 23:01:27.636: E/StrictMode(585):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-20 23:01:27.636: E/StrictMode(585):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-20 23:01:27.636: E/StrictMode(585):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-20 23:01:27.636: E/StrictMode(585):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-20 23:01:27.636: E/StrictMode(585):  at java.lang.Thread.run(Thread.java:856)

这是我的源文件:

package com.example.rockpaperscissorlizardspock;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.view.View.OnClickListener;

public class rockpaperscissorlizardspock extends Activity {
    /** Called when the activity is first created. */

    private TextView resultText;
    private Button play, reset;
    private ImageButton scissors, paper, rock, lizard, spock;
    private int paperimg, paper_pressedimg, scissorsimg, scissors_pressedimg,     rockimg, rock_pressedimg, lizardimg, lizard_pressedimg, spockimg, spock_pressedimg;
        private ImageView compSciss, compPaper, compRock, compLiz, compSpock;
    Random rand = new Random();
    int compChoice = 0;
    String pChoice ="";
    String cChoice = "";

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

        Initialize();
    }

    /** initialize all of the components*/
     private void Initialize()
    {
        play = (Button) findViewById(R.id.button1);
        reset = (Button) findViewById(R.id.button2);
        resultText = (TextView) findViewById(R.id.resultText);
        lizard = (ImageButton) findViewById(R.id.imageButton4);
        spock = (ImageButton) findViewById(R.id.imageButton5);
        scissors = (ImageButton) findViewById(R.id.imageButton3);
        paper = (ImageButton) findViewById(R.id.imageButton2);
        rock = (ImageButton) findViewById(R.id.imageButton1);
        compSciss = (ImageView) findViewById(R.id.computerScissors);
        compPaper = (ImageView) findViewById(R.id.computerPaper);
        compRock = (ImageView) findViewById(R.id.computerRock);
        spockimg = R.raw.spock;
        spock_pressedimg = R.raw.spock_pressed;
        lizardimg = R.raw.lizard;
        lizard_pressedimg = R.raw.lizard_pressed;
        paperimg = R.raw.paper;
        paper_pressedimg = R.raw.paper_pressed;
        scissorsimg = R.raw.scissors;
        scissors_pressedimg = R.raw.scissors_pressed;
        rockimg = R.raw.rock1;
        rock_pressedimg = R.raw.rock1_pressed;

        /**set the button Listeners*/
        play.setOnClickListener(new OnClickListener(){
            public void onClick(View arg0){
                play();
            }
        });

        reset.setOnClickListener(new OnClickListener(){
             public void onClick(View arg0){
                paper.setImageResource(paperimg);

                 rock.setImageResource(rockimg);
                scissors.setImageResource(scissorsimg);
                lizard.setImageResource(lizardimg);
                spock.setImageResource(spockimg);
                compPaper.setImageResource(paperimg);
                compRock.setImageResource(rockimg);
                compSciss.setImageResource(scissorsimg);
                compLiz.setImageResource(lizardimg);
                compSpock.setImageResource    (spockimg);               
                pChoice = "";
                cChoice = "";
                play.setEnabled(false);
                reset.setEnabled(true);
                scissors.setEnabled(true);
                paper.setEnabled(true);
                rock.setEnabled(true);
                lizard.setEnabled(true);
                spock.setEnabled(true);
                resultText.setText("");
            }
        });

        /*
         * Declaring what happens when specific choices are made.
         */

        scissors.setOnClickListener(new OnClickListener(){
             public void onClick(View arg0){    
                     scissors.setImageResource(scissors_pressedimg);
                    paper.setImageResource(paperimg);
                    rock.setImageResource(rockimg);
                    spock.setImageResource(spockimg);
                    lizard.setImageResource(lizardimg);
                    pChoice = "S";
                    play.setEnabled(true);
             }
         });

         paper.setOnClickListener(new OnClickListener(){
            public void onClick(View arg0){
                    paper.setImageResource(paper_pressedimg);
                    scissors.setImageResource(scissorsimg);
                    spock.setImageResource(spockimg);
                    rock.setImageResource(rockimg);
                    lizard.setImageResource(lizardimg);
                    pChoice = "P";
                    play.setEnabled(true);
            }
         });

        rock.setOnClickListener(new OnClickListener(){
            public void onClick(View arg0){
                rock.setImageResource(rock_pressedimg);
                scissors.setImageResource(scissorsimg);
                paper.setImageResource(paperimg);
                spock.setImageResource(spockimg);
                lizard.setImageResource(lizardimg);
                pChoice = "R";
                play.setEnabled(true);
            }
        });

        lizard.setOnClickListener(new OnClickListener(){
             public void onClick(View arg0){
                 lizard.setImageResource(lizard_pressedimg);
                 spock.setImageResource(spockimg);
                 rock.setImageResource(rockimg);
                 scissors.setImageResource(scissorsimg);
                 paper.setImageResource(paperimg);
                 pChoice = "L";
                 play.setEnabled(true);
            }
         });

        spock.setOnClickListener(new OnClickListener(){
            public void onClick(View arg0){
                spock.setImageResource(spock_pressedimg);
                lizard.setImageResource(lizardimg);
                rock.setImageResource(rockimg);
                scissors.setImageResource(scissorsimg);
                paper.setImageResource(paperimg);
                pChoice = "SP";
                play.setEnabled(true);
            }
        });

    }

    /**The method that chooses the computer's move and compares
     *  to the player's move.
     */
    public void play()
    {
        compChoice = rand.nextInt(5);

        if(compChoice == 0)
        {
            cChoice = "R";
            compRock.setImageResource(rock_pressedimg);
            }
        else if(compChoice == 1){
            cChoice = "P";
            compPaper.setImageResource(paper_pressedimg);
        }       
         else if(compChoice == 2)
        {
            cChoice = "S";
            compSciss.setImageResource(scissors_pressedimg);
            }
        else if(compChoice == 3)
           {
            cChoice = "L";
             compLiz.setImageResource(lizard_pressedimg);
            }
        else if(compChoice == 4)
       { 
       cChoice = "SP";
       compSpock.setImageResource(spock_pressedimg);
       }

    check();
}

/**
 * Compares the Computer's and the Player's moves.
 */
public void check()
{
    if(pChoice == "R") //Rock
    {
        if(cChoice == "R")
            resultText.setText("Draw");

        else if(cChoice == "P")
            resultText.setText("You Lose");

        else if(cChoice == "S")
            resultText.setText("You Win");

        else if(cChoice == "L")
            resultText.setText("You Win");

        else if(cChoice == "SP")
            resultText.setText("You Lose");
    }

    else if(pChoice == "P") //Paper
    {
        if(cChoice == "R")
            resultText.setText("You Win");

        else if(cChoice == "P")
            resultText.setText("Draw");

        else if(cChoice == "S")
            resultText.setText("You Lose");

        else if(cChoice == "SP")
            resultText.setText("You Win");

        else if(cChoice == "L")
            resultText.setText("You Lose");


    }

    else if(pChoice == "S") //Scissors
    {
        if(cChoice == "R")
            resultText.setText("You Lose");

        else if(cChoice == "P")
            resultText.setText("You Win");

        else if(cChoice == "S")
            resultText.setText("Draw");

        else if(cChoice == "L")
            resultText.setText("You Win");

        else if(cChoice == "SP")
            resultText.setText("You Lose");


    }

    else if(pChoice == "L") //Lizard
    {
      if(cChoice == "R")
        resultText.setText("You Lose");

      else if(cChoice == "L")
        resultText.setText("Draw");

      else if(cChoice == "SP")
        resultText.setText("You Win");

      else if(cChoice == "S")
        resultText.setText("You Lose");

      else if(cChoice == "P")
        resultText.setText("You Win");
    }

    else if(pChoice == "SP") //Spock
    {
        if(cChoice == "SP")
            resultText.setText("Draw");

        else if(cChoice == "L")
            resultText.setText("You Lose");

        else if(cChoice == "S")
            resultText.setText("You Win");

        else if(cChoice == "P")
            resultText.setText("You Lose");

        else if(cChoice == "R")
            resultText.setText("You Win");

    }

    gameOver();
}


/**
 * Runs at the end of each round. Disables all of the buttons
 * except for the reset button.
 */
public void gameOver()
{
    play.setEnabled(false);
    scissors.setEnabled(false);
    rock.setEnabled(false);
    paper.setEnabled(false);
    spock.setEnabled(false);
    lizard.setEnabled(false);
    reset.setEnabled(true);
}

1 个答案:

答案 0 :(得分:0)

你在com.android.exchange.ExchangeService has leaked...中遇到了一个棘手的错误
有时会发生这种错误,与你的代码无关 我建议你这样做,但不能保证它是否会起作用。

  1. 对于模拟器,请尝试创建新的AVD并在那里进行测试。
  2. 对于您的设备,请卸载该应用程序,然后重试。如果它再次发生,请告诉我它是否在第一次尝试时发生。