• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps AndroidStudio Programming (Beginner)

Roly32

Lurker
Hi.
My name is Roly32. I'm creating this thread because I'm having some problems with AndroidStudio 1.2 developing. I'm using "The Big Nerd Ranch Guide 2nd Edition", and AndroidStudio 1.2, so I can't have problems with version compatibility.
The book recommend to develop an App with him, "GeoQuiz" (a geographic quiz). It gives codes, and I write it. But when I need to run, to test the app.. Errors.


QuizActivity.java

package com.bignerdranch.android.geoquiz;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

public class QuizActivity extends AppCompatActivity {
private Button mTrueButton ButtonfindViewById(R.id.true_button);

@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mTrueButton = (Button) findViewById(R.id.true_button);
@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) {
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.menu_main, 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();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

ActivityQuiz.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >

<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>


strings.xml


GeoQuiz

<string name="question_text">Constantinople is the largest city in Turkey!</string>
<string name="true_button">True</string>
<string name="false_button">False</string>
<string name="correct_toast">Correct!</string>
<string name="incorrect_toast">Incorrect!</string>
<string name="action_settings">Settings</string>


Errors:
Error:(10, 31) error: ';' expected
Error:(10, 32) error: invalid method declaration; return type required
Error:(10, 67) error: expected
Error:(18, 28) error: ';' expected
Error:(18, 36) error: ';' expected
Error:(23, 6) error: illegal start of type
Error:(24, 25) error: expected
Error:(25, 36) error: expected
Error:(25, 37) error: illegal start of type
Error:(25, 40) error: ')' expected
Error:(25, 45) error: ';' expected
Error:(25, 46) error: invalid method declaration; return type required
Error:(27, 28) error: ';' expected
Error:(27, 35) error: ';' expected
Error:(28, 21) error: not a statement
Error:(28, 35) error: ';' expected
Error:(29, 26) error: not a statement
Error:(29, 39) error: ';' expected
Error:Execution failed for task ':app:compileDebugJava'.

Compilation failed; see the compiler error output for details.

(All errors are referred to QuizActivity.java.) I don't know what to do, now. I copied the text exactly from the book, but nothing..
 
I suspect you're trying to run before you can walk. If you don't have any knowledge of the Java programming language, then I recommend you take some time to go through a tutorial, or read a book, to understand the basics.

As to your current problem, then the compiler is getting very confused over this statement

Code:
private Button mTrueButton ButtonfindViewById(R.id.true_button);

I think what you meant to write was a class variable declaration.

Code:
private Button mTrueButton;
 
I think the error log is telling you that you don't have enough semicolons in your code.

Well if he fixes the variable declaration line, everything should be ok. The huge number of spurious errors are because the compiler is trying to make sense of that line, and everything that comes after. It's basically got itself in one heck of a confused state. Not surprising really, as it looks like the code is trying to declare a method, but it's incorrect syntax for a method declaration.
Basically with compiler errors, you can forget everything that comes after the first error, because all those subsequent errors are a result of the compiler trying to recover from the first one, and make sense of it all. They don't generally do a very good job of this.
 
First issue that I see is this:

private Button mTrueButton ButtonfindViewById ( R.idtrue - button )

Should be

private Button mTrueButton;

This is before the onCreate.
 
Oh, thanks. I changed it as you recommend. Now I have still two errors:


mFalseButton.setOnClickListener(new View.OnClickListener() {


Toast.LENGTH_SHORT).show();
 
What's the error? If you hover over the text in red, Android Studio should tell you what the problem is.
Also please update your code to show its current state.
Thanks.
 
What's the error? If you hover over the text in red, Android Studio should tell you what the problem is.
Also please update your code to show its current state.
Thanks.
setOnClickListener - > "Cannot resolve symbol setOnClickListener"
show - > "Cannot resolve method show"
 
Sorry but you really need to post more code, because it's not clear what's causing the problem.
In particular, how do you declare variable mFalseButton?
 
Sorry but you really need to post more code, because it's not clear what's causing the problem.
In particular, how do you declare variable mFalseButton?


(The offending code: QuizActivity.java.)
I marked with bold the mFalseButton.

package com.bignerdranch.android.geoquiz;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.bignerdranch.andorid.geoquiz.R;

public class QuizActivity extends AppCompatActivity {
private Button mTrueButton;
private Button mFalseButton;

@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mTrueButton = (Button) findViewById(R.id.true_button);
}
@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) {
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.menu_main, 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();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}
 
This is incorrect syntax:

Code:
mFalseButton.setOnClickListener(new View.OnClickListener() {
);}

Try

Code:
mFalseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        }
     });
 
This is incorrect syntax:

Code:
mFalseButton.setOnClickListener(new View.OnClickListener() {
);}

Try

Code:
mFalseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        }
     });

I did what you said. The errors are still there:

Error:(29, 36) error: <identifier> expected
Error:(29, 37) error: illegal start of type
Error:(29, 40) error: ')' expected
Error:(29, 45) error: ';' expected
Error:(29, 46) error: invalid method declaration; return type required
Error:(31, 28) error: ';' expected
Error:(31, 35) error: ';' expected
Error:(37, 17) error: not a statement
Error:(33, 6) error: illegal start of type
Error:(37, 31) error: ';' expected
Error:(38, 22) error: not a statement
Error:(38, 35) error: ';' expected
:app:compileDebugJavaWithJavac FAILED
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
 
Ok my human compiler abilities have failed me. I'll have to fire up Android Studio and paste your code in, but I haven't got time right now.
 
So this bit of code is problematic

Code:
mFalseButton= (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        }
     });

The issue is that these lines of code are not in a method! You probably want to move this code into the onCreate() method.
 
Back
Top Bottom