Errors in GeoQuiz - mTrueButton and mFalseButton


#1

I get errors on the following four lines (these are just excerpts from the actual code after these four lines):

    mTrueButton = (Button)findViewByID(R.id.true_button);
    				R.string.incorrect_toast, 
    mFalseButton = (Button)findViewByID(R.id.false_button);
    				R.string.correct_toast, 

Here is my code:

package com.bignerdranch.android.geoquiz;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class QuizActivity extends Activity {

private Button mTrueButton;
private Button mFalseButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);
    
    mTrueButton = (Button)findViewByID(R.id.true_button);
    mTrueButton.setOnClickListener(new View.OnClickListener(){
    	@Override
    	public void onClick(View v){
    		Toast.makeText(QuizActivity.this,
    				R.string.incorrect_toast, 
    				Toast.LENGTH_SHORT).show();
    	}
    });
    
    mFalseButton = (Button)findViewByID(R.id.false_button);
    mFalseButton.setOnClickListener(new View.OnClickListener(){
    	@Override
    	public void onClick(View v){
    		Toast.makeText(QuizActivity.this,
    				R.string.correct_toast, 
    				Toast.LENGTH_SHORT).show();        	}
    });

}

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

}


#2

Those four lines are making reference to values in R.java so have you checked to make sure that true_button and the others are defined in R.java?


#3

No, but I don’t know how to do that. R.java is auto-generated, so… I’m not sure. Would you please tell me?


#4

Do you have android:id="@+id/true_button" in your activity_quiz.xml? Especially check for the plus sign.


#5

I do, and likewise for false_button. Here is the activity_quiz.xml file:

<TextView
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:padding="24dp"
    android:text="@string/question_text" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    
<Button
	android:id="@+id/true_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/true_button" />

<Button
    android:id="@+id/false_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/false_button" />

</LinearLayout>

#6

Do any of these ideas help? http://stackoverflow.com/questions/11256049/r-java-not-updating


#7

How do I “fix project properties” as they mention on that website?


#8

http://stackoverflow.com/questions/10197920/how-to-fix-the-android-project


#9

In your code, findViewByID should be findViewById. Please take note of the capitalization. Java is case sensitive language.


#10

I did change findViewById. I cleaned the project a few times and got more errors at setContentView(R.layout.activity_quiz); [line 19]
and for the two toasts. I assume this means that there are still problems generating R correctly, which I am working on.

I am also getting this error message:
[2014-03-25 15:09:33 - GeoQuiz] C:\Android\GeoQuiz\res\menu\quiz.xml:3: error: Error: No resource found that matches the given name (at ‘title’ with value ‘@string/action_settings’).

I don’t know why it expects ‘quiz.xml’ to exist.


#11

It expects quiz.xml to exist because of this line in QuizActivity.java:

In my version of QuizActivity.java it refers to R.menu.main instead R.menu.quiz and main.xml exists in my project.


#12

One poster suggested deleting R and that it would automatically regenerate, but I must admit I have looked for R. and don’t know its location.


#13

It’s in the gen folder.


#14

Maybe I’m missing the really obvious stuff here, but now I’m blocked by the command ‘select java compiler’, listed below. R. is still not in my Gen folder. Maybe I’m missing mmy head right now, but again, I’m blocked.

Right-click on the project name in the package explorer.

Select Java Compiler.

Check off enable project specific settings.

Select Compiler compliance level 1.6

Apply/OK.

stackoverflow.com/questions/4139 … 77#9732077


#15

Honestly, if I were in your shoes, I’d start over. Reinstall ADT if you have to and then download the code from the book and make sure you can build it. Once you get to that point successfully, then you can start working on your own modifications again.


#16

Thank you. What a weird scenario.


#17

I had the same problem. It wasted one whole day to figure out what was wrong.

My symptom was the same: R.java was not updated. I followed everything what this book asked. I also search this forum and googled it. I did “clean -> project” and “fix the project properties”. Nothing worked. I created and deleted GeoQuiz project at least 10 times. Never worked.

How did I solve?
It was simple. I deleted ADK folder, and re-downloaded and re-installed ADK. Then? It started to work at the first trial!! What the… :cry:
Anyway, if someone has the same error, delete your Eclipse & ADK directory, re-download it and re-install it. Don’t waste your day like me. :sunglasses: