Android Build a Simple Android App Basic Android Programming Adding the OnClick Method

habeebur rahman
habeebur rahman
713 Points

cant resolve symbol

both fact text view and showbutton are in red color, when hovered it says cant resolve symbol

this is the code :

package com.example.habeeb.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView mFactTextView;
    private Button mShowButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {



        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mFactTextView = (TextView) findViewById(R.id.FactTextView);
        mShowButton = (Button) findViewById(R.id.ShowButton);

        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String fact = "bloodseeker is noob";
                mFactTextView.setText(fact);

            }
        };
mShowButton.setOnClickListener(listener);
    }
}

when i run i get this error

C:\Users\habeeb\AndroidStudioProjects\MyApplication3\app\src\main\java\com\example\habeeb\myapplication\MainActivity.java
Error:(22, 53) error: cannot find symbol variable FactTextView
Error:(23, 49) error: cannot find symbol variable ShowButton
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 2.781 secs
Information:3 errors
Information:0 warnings
Information:See  complete output in console

I think it would be helpful if you posted the screenshot from your IDE instead of the code snippet itself. If the error/warning is included in the screenshot, it would be a bonus..

3 Answers

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 56,865 Points

Yes, the issue is in there. Let me try to explain.

These lines in the Java code:

        mFactTextView = (TextView) findViewById(R.id.FactTextView);
        mShowButton = (Button) findViewById(R.id.ShowButton);

make the Java code join the member variables of the class, the mFactTextView and mShowButton join to the GUI, or activity. That's the R.id.FactTextView bit. However, your textviews, and you have two, are called:

    <TextView
        android:id="@+id/textView"
    .
    .
    />

    <TextView
        android:id="@+id/textView2"

The name after the id attribute is what you use to hook the GUI controls up to your code. Android looks for that id after the R.id bit. You've tried to use a GUI element in your Java code that doesn't exist in your XML.

So, you need to change one of these to be called FactTextView. Strictly, you should use camel-case so it would be factTextView but let's just change one thing at once.

Same with your button. In your XML it is called:

    <Button
        android:id="@+id/button"

But in your Java, it is expecting to find the button named ShowButton.

I don't know which TextView should be called FactTextView but I will guess it is the second one. In that case, amend your code in the XML to be:

    <TextView
        android:id="@+id/FactTextView"

Just change that one line; leave the rest as it is inside there. And for your button, change this line:

    <Button
        android:id="@+id/ShowButton"

That should clear your errors. Let me know how you get on.

Steve.

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 56,865 Points

Can you post your XML for that activity. It is saying that your textview and button have different names to that which you have used in the code.

Steve.

habeebur rahman
habeebur rahman
713 Points

this is the xml

tools:context="com.example.habeeb.funfacts.FunfactActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/textView2"
        android:layout_marginTop="25dp"
        android:text="Did you know !"
        android:textColor="@color/colorAccent"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="186dp"
        android:text="ursa is a stronger early game"
        android:textSize="24sp"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="58dp"
        android:text="show me the next fact"/>

</RelativeLayout>
Ben Jakuben
Ben Jakuben
Treehouse Teacher

Thanks for this excellent reply and explanation, Steve Hunter!

habeebur rahman
habeebur rahman
713 Points

you`re a legend Man ...i got that right, thank you ;-)