0

So I'm still working on my first little app here, new to Android and Java, so I'm stuck on a basic little problem here. Answers to my first questions were really helpful, so after researching and not coming up with anything, I thought I'd ask for some more help!

The idea is that on another screen the user makes a choice A, B, C, or D, and that choices is passed as a string through the intent. OnResume checks if the choice is not null and sets an integer that corresponds to that string. Later when the user pushes another button, some if else logic checks that int and performs and action based on which was chosen. The problem is that the App crashed at onResume.

I learned that I have to use equals(string) to compare string reference, but maybe the problem is that I am trying to compare a string in reference to a literal string? Any help would be greatly appreciated. Thanks!

        protected void onResume() {
    super.onResume();

    // Get the message from the intent
    Intent intent = getIntent();
    String choice = intent
            .getStringExtra(ExtensionSetupSlector.TORQUE_SETUP);

    // Create the text view
    TextView displayChoice = (TextView) findViewById(R.id.displayChoice);
    if (!choice.equals("")){
    displayChoice.setText(choice);

    if (choice.equals("A")) {
        myChoice = 1;
    }
    if (choice.equals("B")) {
        myChoice = 2;
    }
    if (choice.equals("C")) {
        myChoice = 3;
    }
    if (choice.equals("D")) {
        myChoice = 4;
    }

}
}

myChoice is declare right after ...extends Activity{ Also I'm not quite sure If this should really be in onResume, but it was working before I started try to set myChoice in the onResume (when I was just displaying the choice). Thanks again!

4

1 回答 1

0

Change if (!choice.equals("")) to check for null instead. Otherwise your app attempts to access an empty reference and crashes.

于 2012-12-08T17:57:40.223 回答