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 trialhabeebur rahman
713 Pointscant 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
3 Answers
Steve Hunter
57,712 PointsYes, 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
57,712 PointsCan 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
713 Pointsthis 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
Treehouse TeacherThanks for this excellent reply and explanation, Steve Hunter!
Steve Hunter
57,712 Pointshabeebur rahman
713 Pointsyou`re a legend Man ...i got that right, thank you ;-)
Steve Hunter
57,712 PointsNo problem!
Steve.
Unsubscribed User
12,721 PointsUnsubscribed User
12,721 PointsI 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..