mQuestionBank array on page 39


#1

per page 39 I added this to QuizActivity.java:

private TrueFalse[] mQuestionBank = new TrueFalse[]{
new TrueFalse(R.string.question_oceans, true),
new TrueFalse(R.string.question_mideast, false),
new TrueFalse(R.string.question_africa, false),
new TrueFalse(R.string.question_americas, true),
new TrueFalse(R.string.question_asia, true),
};

I’m getting an error “TrueFalse cannot be resolved to a type”

I suppose I missed something somewhere…if you could point me to it, that would be great. Thanks.


#2

Figured it out. I built the TrueFalse class (p 32) in the wrong folder


#3

I have chack the folder of TrueFalse class (p 32), but I still have the same problem, I don’t know why.


#4

my TrueFalse class is in the com.bignerdranch.android.geoquiz package,
but i still have the same problem.


#5

If QuizActivity.java and TrueFalse.java are both part of the com.bignerdranch.android.geoquiz package, then the only way QuizActivity would not have access to TrueFalse is if TrueFalse was private.

So check:

  1. QuizActivity.java and TrueFalse.java both have package com.bignerdranch.android.geoquiz;.
  2. TrueFalse is public:

public class TrueFalse { ... }


#6

[quote=“embeddedmatt”]If QuizActivity.java and TrueFalse.java are both part of the com.bignerdranch.android.geoquiz package, then the only way QuizActivity would not have access to TrueFalse is if TrueFalse was private.

So check:

  1. QuizActivity.java and TrueFalse.java both have package com.bignerdranch.android.geoquiz;.
  2. TrueFalse is public:

public class TrueFalse { ... }[/quote]

  1. QuizActivity and TureFalse both have the same package.
  2. TrueFalse class is public.
    but my problem still exist.
    Description Resource Path Location Type
    question_asia cannot be resolved or is not a field QuizActivity.java /GeoQuiz/src/com/bignerdranch/android/geoquiz line 28 Java Problem
    question_oceans cannot be resolved or is not a field QuizActivity.java /GeoQuiz/src/com/bignerdranch/android/geoquiz line 24 Java Problem
    question_mideast cannot be resolved or is not a field QuizActivity.java /GeoQuiz/src/com/bignerdranch/android/geoquiz line 25 Java Problem

this is my code:

[code]package com.bignerdranch.android.geoquiz;

public class TrueFalse {
private int mQuestion;
private boolean mTrueQuestion;

public TrueFalse(int question, boolean trueQuestion) {
	mQuestion = question;
	mTrueQuestion = trueQuestion;
}

public int getQuestion() {
	return mQuestion;
}

public void setQuestion(int mQuestion) {
	this.mQuestion = mQuestion;
}

public boolean isTrueQuestion() {
	return mTrueQuestion;
}

public void setTrueQuestion(boolean mTrueQuestion) {
	this.mTrueQuestion = mTrueQuestion;
}

}
[/code]

and this is the QuizActivity

[code]package com.bignerdranch.android.geoquiz;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends ActionBarActivity {

private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private TextView mQuestionTextView;

private TrueFalse[] questionBank = new TrueFalse[] {
		new TrueFalse(R.string.question_oceans, true),
		new TrueFalse(R.string.question_mideast, false),
		new TrueFalse(R.string.question_africa, false),
		new TrueFalse(R.string.question_americas, true),
		new TrueFalse(R.string.question_asia, true),
};

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

	/*if (savedInstanceState == null) {
		getSupportFragmentManager().beginTransaction()
				.add(R.id.container, new PlaceholderFragment()).commit();
	}*/
}

@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;
}

@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);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

	public PlaceholderFragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_quiz, container,
				false);
		return rootView;
	}
}

}
[/code]


#7

I copied and pasted your code over mine and I didn’t get the errors that you did. I did get some errors that I think may be because you are using a newer version of the SDK than I am. I noticed that your QuizActivity extends ActionBarActivity while mine extends Activity.

Since you are struggling to get started, I’d recommend that you download the source code published by the authors and see if it will build properly. You might want to also consider using v22.3.0 of the ADT. That’s what I’m using and so far the wizards have generated the code exactly like the book says.

I guess my final suggestion is to try Project/Clean… to force your project to rebuild from scratch. This isn’t likely to help, but it’s quick and easy to try.


#8

my error disappear when I added the TextView element in the onCreate block, as the following codes:

[code]protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);

	mQuestionTextView = (TextView)findViewById(R.id.text_view_question);
            int question = mQuestionBank[myCurrentIndex].getQuestion();
	mQuestionTextView.setText(question);

[/code]
It is amazing.
after all, Thanks very muck!


#9

I did start to have problems when I went to 22.6. I’m going to go to 22.3 so that what I do more closely follows what’s in the book. One problem with books is that they’re published to a particular version of the SDK and any significant changes (such as this ActionBarActivity thing) can either cause problems, or can be used to be blamed for things that are not a result of that change.


#10

I tried reverting to ADT 22.3 from 22.6 but have not been able to. First I tried to find a download site from Google, I was not able to do so. Then I thought I’d try doing it from ADT-> Help -> About ADT -> Installation Details and then selected the most recent version of 22.3 that I had in my history. It comes back with an error “Revert Configuration has encountered a problem…” There must be someplace I can find 22.3 and download and reinstall on top of 22.6.


#11

I ran into the same problem using SDK v22.6.2. Project->Clean… seemed to do the trick. I also moved TrueFalse.java under GeoQuiz->src->com.bignerdranch.android.qeoquiz. For some reason I found it under GeoQuiz->gen->com.bignerdranch.android.qeoquiz, which didn’t seem right to me so I moved it. I see no errors now! …Moving slowly but I am moving :slight_smile:


#12

I was also stuck on this for a while. When I was typing the explicit initialization of the TrueFalse [] array of objects, the eclipse wasn’t recognizing the strings in the suggestions menus when I typed the following code:

private TrueFalse [] mQuestionBank = new TrueFalse[]{ new TrueFalse(R.string.question_oceans, true), new TrueFalse(R.string.question_mideast, false), new TrueFalse(R.string.question_africa, false), new TrueFalse(R.string.question_americas, true), new TrueFalse(R.string.question_asia, true), };

I just decided to push ahead and finish coding even though it was showing as an error and eventually all the red underlines disappeared.