What’s the bug in the program?

Asked

Viewed 54 times

-2

Give me one mistake R appears in red.

public class MainActivity extends AppCompatActivity {

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

        //reference to xml widgets
        final TextView FishText = (TextView) findViewById(R.id.FishText);
        Button ChangeNameButton = (Button) findViewById(R.id.ChangeNameButton);

        final String animais[] = new String[5];
        animais[0]="Dog";
        animais[1]="Cat";
        animais[2]="Rat";
        animais[3]="Horse";
        animais[4]="Mouse";

        //change the name of the animal 
        ChangeNameButton.setOnClickListener(
                new Button.OnClickListener(){
                    public void onClick(View view) {
                        for(int contador = 0;contador<5;contador++) {
                            FishText.setText(animais[contador]);
                        }
                    }
        }
    }
  • None. You need to rebuild.

1 answer

3

The error occurs when the project’s R Class was not generated, or is not in the folder /res/ of the project. To solve the problem, do the following:

Solution 1 Navigate to Project -> Clean -> select your project or Select the project and press F5 (Refresh)

Solution 2 Select the project, right-click and navigate to Android Tools -> Fix Project Properties

Solution 3 Check that the Project Buid Target of your app is configured according to your Androidmanifest.xml file via the android:minSdkVersion property. The android:minSdkVersion property has to be equal to the Level API of Project Build Target. When you change the Target of your application the plugin does not modify the manifest file.

--- Solving the loop problem ---

Change that part

for(int contador = 0;contador<5;contador++) {
                            FishText.setText(animais[contador]);
                        }

For that reason

     String [] animais = {"Dog","Cat","Rat","Horse","Mouse"};
      ArrayList<String> animaisList= new ArrayList<String> //Cria o Array com a String 
    (Arrays.asList(animaisList));
         Collections.shuffle(animaisList);   //Embaralho (Random) as posições          
 FishText.setText(String.valueOf(animaisList.get(1))); //Capituro o primeiro valor

Checks if the solution meets.

  • Thank you! Could you just help me with something ? I did what I said and gave me only mouse, could tell me how to make it loop by all and go changing and not loop and only change at last ?

  • I’ll update the answer.

  • it is necessary to import something ? give me some mistakes

  • Yes, the java.lang.Object class, plus the IDE should already request the inclusion of that class, take the updated code there and test it. A simpler solution would also be to add a Jcombobox, with the animal options, the user then selects the animal in the combobox would look even better than the current form, to pick a text from a combobox you use the following resource -> docombobox.getSelectedItem();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.