What is the error in the program? [closed]

-2

Give me an error where 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]);
                        }
                    }
        }
    }
    
asked by anonymous 22.06.2017 / 18:37

1 answer

3

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

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

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

Solution 3 Make sure that your app's Buid Target is configured according to your AndroidManifest.xml file through the android: minSdkVersion property. The android: minSdkVersion property must match the API Level of Project Build Target. When you change the Target of your application the plugin does not modify the manifest file.

--- Loop problem resolution ---

Replace this part

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

For this

     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

Verify that the solution meets.

    
22.06.2017 / 18:59