Cannot resolve method setText(java.lang.String) - java

I've looked at other questions similar to the error I'm getting, but I can't seem to figure out the issue here. All I'm trying to do is click a button and change a TextView's text.
My code is as follows:
public class FindBeerActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_beer);
}
//call when the user clicks the button
public void onClickFindBeer(View view) {
//Get a reference to the TextView
View brands = (TextView) findViewById(R.id.brands);
//Get a reference to the Spinner
Spinner color = (Spinner) findViewById(R.id.color);
//Get the selected item in the Spinner
String beerType = String.valueOf(color.getSelectedItem());
//Display the beers. This is where the error is
brands.setText(beerType);
}
}
and the XML
<TextView
android:id="#+id/brands"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/find_beer"
android:layout_alignStart="#id/find_beer"
android:layout_below="#id/find_beer"
android:layout_marginTop="18dp"
android:text="#string/brands" />
When I build I get: "error: cannot find symbol method setText(String)
I'm following a tutorial to the letter and I can't see where I've made a mistake, so I'd appreciate it if you guys could help me out! Thanks!

You can either change
View brands = (TextView) findViewById(R.id.brands);
to
TextView brands = (TextView) findViewById(R.id.brands);
OR change
brands.setText(beerType);
to
((TextView)brands).setText(beerType);
In either ways, you need to call the setText method on a TextView reference.

You are trying setText() method on a view object. You need to call it on a TextView object.so either change the declaration of brands to TextView or just wrap ur brands object to TextView before calling setText() over it.

Change
View brands = (TextView) findViewById(R.id.brands);
To
TextView brands = (TextView) findViewById(R.id.brands);

Related

Attempt to invoke virtual method on a null object reference - How do I change properly the value of a TextView

I know where my problem exactly is, I just don't know how to fix it. I'm trying to change the value of a TextView and with my code, the app just crashes and I get the error in the title.
My code:
#Override
protected void onStart() {
super.onStart();
if(authenticate() == true) {
displayUserDetails();
} else {
startActivity(new Intent(MainActivity.this, Login.class));
}
}
private boolean authenticate() {
return userLocalStore.getUserStatus();
}
private void displayUserDetails() {
User user = userLocalStore.getLoggedInUser();
final TextView userName = (TextView) findViewById(R.id.userName);
userName.setText(user.name);
final TextView userEmail = (TextView) findViewById(R.id.userEmail);
userEmail.setText(user.email);
}
There you have a snippet of my code, if you need the whole class I'd be happy to provide. Thank you in advance.
UPDATE:
The issue was the content view as it was set to activity_main and since i couldn't switch to another I just did this:
private void displayUserDetails() {
User user = userLocalStore.getLoggedInUser();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View header=navigationView.getHeaderView(0);
final TextView userName = (TextView) header.findViewById(R.id.userName);
userName.setText(user.name);
final TextView userEmail = (TextView) header.findViewById(R.id.userEmail);
userEmail.setText(user.email);
}
It may be the problem that when the function is invoked initially user.email value remains empty.
Your code looks correct, the problem is that findViewById() returns null.
There could be a lot of causes of the problem.
Check that your .xml file have views with id userName and userName.
Check that this .xml is accessible by all devices, i.e. it is present in all layout folders, for all screen sizes, densities, languages and so on.
Try to comment out one of the .setText()'s and see if other one is working. If it is just make sure that not working one is exactly the same.
If you are using an activity make sure that you call .setContentView() with correct layout file.
If you are using a fragment make sure that you return a view constructed put of a correct layout file from onCreateView.

textview giving null pointer

I hate errors like this. I'm getting a null pointer. I know what it means. I just don't understand how i'm getting it since i know that the item it should be pointing to does exist. I'm creating a textview that will be updated by the opengl renderer. The textview provides a score. I've created the textview in XML and given it a 'id', then i reference it in my program and update it through the renderer. Icreate TextView variables globally then I initialize them in the onCreate() method. Then I created a method to set the text. Which is called inside of my renderer class.
Here is my java code
View r1;
TextView score3, score4;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set app to full screen and keep screen on
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(Main.layout);//R.layout.gl_triallayout;
r1 = findViewById(Main.id);////R.id.gl_triallayout;
score3 = (TextView) findViewById(R.id.threeScore);
score4 = (TextView) findViewById(R.id.fourScore);
......
}
public int get3(int i){
score3.setText("Score" + String.valueOf(i));
((RelativeLayout) r1).bringChildToFront(findViewById(R.id.threeScore));
Log.i("i", String.valueOf(i));
return i;
}
XML code
<TextView
android:id="#+id/threeScore"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:textSize="30dp"
android:textColor="#ffff00"/>
Update
Main class
public void Trial(View v) {
layout = R.layout.gl_triallayout;
id = R.id.glTrialLayout;
gameType = 0;
Intent intent = new Intent(Main.this, OpenGLActivity.class);
startActivity(intent);
}
public void PlayGame(View v) {
layout = R.layout.gl_layout;
id = R.id.glLayout;
gameType = 1;
Intent intent = new Intent(Main.this, OpenGLActivity.class);
startActivity(intent);
}
I hate the idea of clutter stackoverflow with the same question so if the answer is immensely simple could you give it to me in the comments. then i could delete this.
Your
setContentView(Main.layout)
should be
setContentView(R.layout.filename)
In the above code, you should change your layout file name based on your need like R.layout.activity_main.
Same applies to,
r1 = findViewById(Main.id);////R.id.gl_triallayout;
What is r1?
If it's a button, it should be
r1 = (Button) findViewById(R.id.gl_triallayout);////R.id.gl_triallayout;
If it's a Editext,
r1 = (EditText) findViewById(R.id.gl_triallayout);////R.id.gl_triallayout
If it's a TextView,
r1 = (TextView) findViewById(R.id.gl_triallayout);////R.id.gl_triallayout
Also, In your XML layout, where's the view for score4 and r1?
I think you should start learning the basics of Android first.

Cannot Resolve Symbol in Android Studio

In the code below, the first TextView symbol cannot be resolved, and the findById method cannot be resolved. Can someone explain to me what the problem is and how I can fix it?
final TextView factLabel = (TextView) findViewById(R.id.factTextView);
Button showFactButton = (Button) findById(R.id.showFactButton);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
String fact = "";
// Randomly select a fact.
// Update the label with our dynamic fact
factLabel.setText(fact);
}
};
You need to give more background. Are you in a fragment, or an activity? How was the layout set? I'm guessing the answer is that you need to be calling findViewById() on the View of your layout - e.g. view.findViewById() but it depends on how your layout was set.
If you're in an activity, calling findViewById() on the Activity object will only work if the current Activity layout is set by setContentView. If your layout was set a different way, then you need to get the View object of the layout and call findViewById() on it. If you're in a fragment, and you're in onCreateView() then the view has been passed in for you and you just need to call view.findViewById()
I am asuming that you want some text to set on text view on click of button try this
final TextView factLabel = (TextView)findViewById(R.id.factTextView);
Button showFactButton = (Button)findViewById(R.id.showFactButton);
showFactButton.setOnClickListener(new View.onClickListener()
{
#Override
public void onClick(View v) {
String fact = "Your Text Here";
// Randomly select a fact.
// Update the label with our dynamic fact
factLabel.setText(fact);
});
Make sure you are importing all the packages you need. Also make sure all the dependancies are working. Lastly make sure that there is a text view in the XML for the activity. Hooe some of this is helpful.

Using the XML Layout file instead of Java code for creating a TextView to display a message received through intent

I am learning how to develop apps for android.
I followed the tutorial by Google Building Your First App.
The idea is basically to write a message in a fragment, submit it with a button, the message is passed through the intent and displayed in the second fragment.
My question is about the part where we display the message. This is how it was done in the tutorial:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText("You submitted: "+message);
// Set the text view as the activity layout
setContentView(textView);
}
I learned that the UI components can be implemented using XML, or Java, in this case obviously the tutorial uses Java to make a new TextView with the data received through the intent. I was trying to redo that part, but this time without relying that much on Java when it comes to the view. So I replaced the part under // Create the text view by:
TextView textView = (TextView) findViewById(R.id.display_message);
textView.setText(message);
And in the fragment_display_message.xml I had the following element:
<TextView
android:id="#+id/display_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
It throws a NullPointerException at textView.setText(message);
I wonder why this happens, why can't it be done this way?
i think you should call
setContentView(R.layout.fragment_display_message);
before you call
TextView textView = (TextView) findViewById(R.id.display_message);
textView.setText(message);
but it's a fragment?

Can't set text to the TextView

I am learning to develop apps for android from a book and after following the instructions I end up with the following. When I run the app the text from the setStartUpScreenText() object is not displayed.
MainActivity.java:
protected void setStartupScreenText(){
TextView planetNameValue = (TextView)findViewById(R.id.DataView1);
planetNameValue.setText(earth.planetName);
}
MainActivity.xml
<TextView
android:id="#+id/DataView1"
android:layout_toRightOf="#+id/TextView1"
android:layout_marginLeft="36dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Make sure you call setContentView method before updating any view in the screen.
Make sure you call setStartupScreenText() method after setContentView in oncreate of the activity
Fist of all call the setStartUpScreenText() function from inside of setStartUpWorldValues() function (either in the beginning or in the end). You can also call this function inside the onCreate() methods after the setStartUpWorldValues() function.
Secondly replace the entire code of the setStartUpScreenText() function with the following code: (page 90, Learn Android App Development, publisher: Apress)
TextView planetNameValue = (TextView) findViewById(R.id.dataView1);
planetNameValue.setText(earth.planetName);
TextView planetMassValue = (TextView) findViewById(R.id.dataView2);
planetMassValue.setText(String.valueOf(earth.planetMass));
TextView planetGravityValue = (TextView) findViewById(R.id.dataView3);
planetGravityValue.setText(String.valueOf(earth.planetGravity));
TextView planetColoniesValue = (TextView) findViewById(R.id.dataView4);
planetColoniesValue.setText(String.valueOf(earth.planetColonies));
TextView planetPopulationValue = (TextView) findViewById(R.id.dataView5);
planetPopulationValue.setText(String.valueOf(earth.planetPopulation));
TextView planetMilitaryValue = (TextView) findViewById(R.id.dataView6);
planetMilitaryValue.setText(String.valueOf(earth.planetMilitary));
TextView planetBaseValue = (TextView) findViewById(R.id.dataView7);
planetBaseValue.setText(String.valueOf(earth.planetBases));
TextView planetForceFieldValue = (TextView) findViewById(R.id.dataView8);
planetForceFieldValue.setText(String.valueOf(earth.planetProtection));
protected void setStartUpWorldValues() {
earth.setPlanetColonies(1); //Set planet colonies to 1
earth.setPlanetMilitary(1); // set planet military to 1
earth.setColonyImmigration(1000); // set planet population to 1000
earth.setBaseProtection(100); // set Planet armed force to 100
earth.turnForceFieldOn(); // Turn on the planet force field
setStartUpScreenText() ;
TextView planetNameValue = (TextView)findViewById(R.id.dataView1);
planetNameValue.setText(earth.planetName);
TextView planetMassValue = (TextView)findViewById(R.id.dataView2);
planetMassValue.setText(String.valueOf(earth.planetMass));
TextView planetGravityValue = (TextView)findViewById(R.id.dataView3);
planetGravityValue.setText(String.valueOf(earth.planetGravity));
TextView planetColoniesValue = (TextView)findViewById(R.id.dataView4);
planetColoniesValue.setText(String.valueOf(earth.planetColonies));
TextView planetPopulationValue = (TextView)findViewById(R.id.dataView5);
planetPopulationValue.setText(String.valueOf(earth.planetPopulation));
TextView planetMilitaryValue = (TextView)findViewById(R.id.dataView6);
planetMilitaryValue.setText(String.valueOf(earth.planetMilitary));
TextView planetBasesValue = (TextView)findViewById(R.id.dataView7);
planetBasesValue.setText(String.valueOf(earth.planetBases));
TextView planetForceFieldValue = (TextView)findViewById(R.id.dataView8);
planetForceFieldValue.setText(String.valueOf(earth.planetProtection));
}
private void setStartUpScreenText() {
// TODO Auto-generated method stub
}

Categories