Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Error: cannot find symbol variable showFactButton, and greyed out imports1

Soo trying to run the Fun Fact app. The shoFactButton is highlighted red and says "cannot resolve symbol 'showFactButton'". Above in the imports "import android.content.DialogInterface;" is greyed out and says "unused import statement" Any Ideas on how to get 'showFactButton' working?

Can you post your code please, it may be helpful to have the java and the XML. Would be happy to help

Sure here it is

package Project.funfacts;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;


public class FunFactActivity extends Activity {

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

        //Declare our View Variable
        final TextView factLabel = (TextView) findViewById(R.id.factTextView);
        Button showFactButton = (Button) findViewById(R.id.showFactButton);
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String[] facts = {
                        "1",
                        "2",
                        "3",
                        "4",
                        "5",


                };
                //the button was clicked, so update the fact label with a new fact
                String fact = "";
                //Randomly select a fact
                Random randomGenerator = new Random(); //Construct a new Random number Generator
                int randomNumber = randomGenerator.nextInt(facts.length);

                fact=facts[randomNumber];



                //Update the label with our dynamic fact
                factLabel.setText(fact);
            }
        };

        showFactButton.setOnClickListener(listener);
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Annnd XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".FunFactActivity"
    android:background="#ff5cff68">

    <TextView
        android:text="Did you know?"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView2"
        android:textSize="24sp"
        android:textColor="#80ffffff" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text='"Ants stretch when they wake up in the morning."'
        android:id="@+id/factTextView"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="24sp"
        android:textColor="#ffffffff" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show Another Fun Fact"
        android:id="@+id/ShowFactButton"
        android:layout_alignParentBottom="true"
        android:background="#fffff5fa" />

</RelativeLayout>

1 Answer

Okay it's just a typo I think!

In the line Button showFactButton = (Button) findViewById(R.id.showFactButton);

in your on create method you have used a lowercase s however the XML file has the id as android:id="@+id/ShowFactButton" with a capital S.

Simply change the capital S to a lowercase s in the XML file, save the changes then hopefully the error will disappear.

To get rid of unused imports you can use the keyboard shortcut Ctrl+Shift+O in Eclipse, not sure with Android studio as i've not used it all that much myself yet but did a quick search on google and found the following question in stack overflow which seems to suggest that the keyboard shortcut Ctrl+Shift+O is available also in android studio..

http://stackoverflow.com/questions/22272524/how-to-auto-import-the-necessary-classes-in-android-studio-with-shortcut

Hope this helps Daniel

That kicked it right, and its working now thanks:)