(Android Question)Getting references to widgets


#1

Hello I am on page 20 of the book Android Programming The big nerd ranch guide.

More specifically, I have to get references to widgets for this part(Listing 1.9).

I wrote the code as instructed by the author but Eclipse keeps showing an error message for this particular line of my code: mFalseButton = (Button)findViewById(R.id.false_button);
by sating “Syntax error on token “mTrueButton” variable DeclarlatorId expected after this token.”

And here is my code:

package com.example.geoquiz;

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

public class MainActivity extends Activity {

private Button mTrueButton;
private Button mFalseButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
}

mTrueButton = (Button)findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
});
mFalseButton = (Button)findViewById(R.id.false_button); // This line shows an error message
mFalseButton.setOnClickListener(new View.OnClickListener() {
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
});

}

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

}

How should I fix this?
Thank you so much for reading my question and please help me!!


#2

I think the reason of this compile error caused by closing curly brace(}).

private Button mTrueButton;
private Button mFalseButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} –> if this is removed, this error will be fixed…

mTrueButton = (Button)findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {

Good luck!


#3

Thanks! However as soon as I erased “}” on the line, it started saying that true_button and false_button cannot be resolved or are not a field. Do you see any reason why?


#4

actually it’s been solved! Thank you android77!!


#5

Can someone tell me how it was solved? I seem to have the same problem.

I tried the O as recommended, but it didn’t seem to produce the expected “import android.view”. I do have
import android.view.Menu;
import android.view.View;

Also tried to import “view” manually, but it said I can’t import a package.
Comments welcome


#6

Don’t worry - it’s not supposed to import “android.view”, because it is a package, not a class.

If it’s in your code showing up as a type error, it should probably be capital V View instead.