Syntax error, insert ";" to complete Statement [closed] - android

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
This is the .java file for a small application I am writing in Android through Eclipse, and I am having a minor error or syntax glitch.
at the end bracket marked by asterisks, eclipse is reporting the error 'Syntax error, insert ";" to complete Statement'. I have searched the code, and find nothing unaccounted for or out of place. Could someone please identify or tell me how to fix this? If you need other files, tell me in the comments. Thanks in advance :)!
package org.example.knittingframe;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class KnittingFrame extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textview = (TextView)findViewById(R.id.textview);
final EditText op1 = (EditText)findViewById(R.id.NumBox1);
final EditText op2 = (EditText)findViewById(R.id.NumBox2);
final Button btnAdd = (Button)findViewById(R.id.addBox);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
**}** // Here is where the error occurs
}
}

The compiler is right. You need to replace
**}** // Here is where the error occurs
with
});
to finish the btnAdd.setOnClickListener method call.

This is a statement in the onCreate(xx) and its an anonymous class, because an anonymous class is a statement you MUST terminate the statement:
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
**}** // Here is where the error occurs
TO:
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
});

Since you are using an annonymous inner class, you need to end the setOnClickListener method call with ");" as follows:
public class KnittingFrame extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textview = (TextView)findViewById(R.id.textview);
final EditText op1 = (EditText)findViewById(R.id.NumBox1);
final EditText op2 = (EditText)findViewById(R.id.NumBox2);
final Button btnAdd = (Button)findViewById(R.id.addBox);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
});
}
}

You started a function call: setOnClickListener(). You need to finish it. Like so:
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
});

If you indented properly, maybe you'd see it, but you're missing an extra parenthesis and semicolon:
package org.example.knittingframe;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class KnittingFrame extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textview = (TextView)findViewById(R.id.textview);
final EditText op1 = (EditText)findViewById(R.id.NumBox1);
final EditText op2 = (EditText)findViewById(R.id.NumBox2);
final Button btnAdd = (Button)findViewById(R.id.addBox);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
});
}
}

Related

Android Activity keeps crashing without an error [duplicate]

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 5 years ago.
I made an incredibly simple app but for some reason it's just crashing. The first page is a simple login screen however the moment I click login it just crashes. The strange part is that there's not even any heavy code to make it do anything strange like that; I'm just making it travel between activities so far.
The main activity
package com.example.philip.lottery1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class LoginActivity extends AppCompatActivity {
Button loginButton;
EditText userNameField;
EditText passwordField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loginButton = (Button) findViewById(R.id.loginButton);
userNameField = (EditText) findViewById(R.id.userNameField);
passwordField = (EditText) findViewById(R.id.passwordField);
final String userName = userNameField.getText().toString();
String password = passwordField.getText().toString();
loginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), CreateTicket.class);
intent.putExtra("ClerkID", userName);
startActivity(intent);
}
});
}
}
The activity it leads to
package com.example.philip.lottery1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Random;
public class CreateTicket extends AppCompatActivity {
private Button randomButton;
private Button OKButton;
private Button searchButton;
private EditText[] lottoNumberFields;
private int[] lottoNumbers;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_ticket);
lottoNumberFields = new EditText[5];
lottoNumberFields[1] = (EditText)findViewById(R.id.num1);
lottoNumberFields[2] = (EditText)findViewById(R.id.num2);
lottoNumberFields[3] = (EditText)findViewById(R.id.num3);
lottoNumberFields[4] = (EditText)findViewById(R.id.num4);
lottoNumberFields[5] = (EditText)findViewById(R.id.num5);
randomButton = (Button) findViewById(R.id.randomButton);
OKButton = (Button) findViewById(R.id.OKButton);
searchButton = (Button) findViewById(R.id.searchButton);
randomButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
randommize();
}
});
OKButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createTicket();
}
});
searchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
search();
}
});
}
private void search()
{
//insert code here
Intent intent = new Intent (getBaseContext(),TicketActivity.class);
startActivity(intent);
}
private void randommize()
{
for (int i = 0 ; i < 5 ; i ++)
{
Random random = new Random();
int num = random.nextInt(49) + 1;
lottoNumberFields[i].setText(num+"");
}
}
private void createTicket()
{
for (int i = 0; i < 5; i++)
{
lottoNumbers[i] = Integer.parseInt(lottoNumberFields[i].getText().toString());
}
Intent intent = new Intent(getBaseContext(), FinalActivity.class);
intent.putExtra("lottoNumbers",lottoNumbers);
startActivity(intent);
}
}
Your lottoNumberFields array is of length 5, but you are going from 1 to 5 index values for it, instead of 0 to 4. Change the code as below:
lottoNumberFields = new EditText[5];
lottoNumberFields[0] = (EditText)findViewById(R.id.num1);
lottoNumberFields[1] = (EditText)findViewById(R.id.num2);
lottoNumberFields[2] = (EditText)findViewById(R.id.num3);
lottoNumberFields[3] = (EditText)findViewById(R.id.num4);
lottoNumberFields[4] = (EditText)findViewById(R.id.num5);

string array changing onClick

I wish to have questions that change to the next question in the sequence onClick but I'm having trouble creating the array of questions and writing the onClick code for that portion.
*Too add more context to the app it will be a questionaire app with a question above an editText field and the users inputs will show up in its respective box at the top of the screen.
package com.example.greg;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class menu extends Activity {
Button mButton;
EditText mEdit;
int questionNumber = 0;
String [] questions;
int numberOfQuestions = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button)findViewById(R.id.button);
mEdit = (EditText)findViewById(R.id.userAnswereditText);
questions=new String[numberOfQuestions];
questions[0]="This is first question?";
questions[1]="This is second question?";
final TextView [] myTexts = new TextView[2];
myTexts[0]=(TextView)findViewById(R.id.varATextView);
myTexts[1]=(TextView)findViewById(R.id.varBTextView);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
myTexts[questionNumber].setText(mEdit.getText().toString());
mEdit.setText(null);
questionNumber++;
if(questionNumber < numberOfQuestions)
questionTextViewHolder.setText(questions[questionNumber]);
else
Toast.makeText(menu.this,"No more questions!",Toast.LENGTH_LONG).show();
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}

Why does Text = null?

I am new to Android and Java and don't know why the Text value is null after I assign Text to it? This is a project for University They didn't teach us Java only gave us Eclipse and Android Sdk and Java need some help please!
package com.example.glossaryapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Syntax extends Activity {
static String Text;
public static TextItem[] Syntax = new TextItem[50];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.syntax);
final TextView show = (TextView) Syntax.this
.findViewById(R.id.textViewSyn);
// Linking the text View widget
int Count1 = 0;
// Using the TextView's shows method set text to display the appropriate
// arrays and indexes
if (Syntax[0] != null) {
Text = Syntax[0].displayText();
show.setText(Text);
}
// The Add button for if a user wants to add a new TextItem to the Array
Button but1 = (Button) findViewById(R.id.butAddSyntax);
but1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Create.strSender = "Syntax1";
startActivity(new Intent(Syntax.this, Create.class));
}// end of on click
});// butAddSyntax
// Home Button
Button but2 = (Button) findViewById(R.id.buttHomeSyntax);
but2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}// end of on click
});// buttonHomeSyntax
}
public void onResume() {
super.onResume();
TextView show = (TextView) Syntax.this.findViewById(R.id.textViewSyn);
if (Syntax[0] != null) {
Text = Syntax[0].displayText().toString();
show.setText(Text);
}
}
}
Using Android's LogCat will help you to solve this kind of problems.
Here you have a nice article:
http://www.vogella.com/tutorials/AndroidLogging/article.html
I think the problem is in here:
if( Syntax[0] != null ){
Text = Syntax[0].displayText();
show.setText(Text);
}
you could begin using LogCat trying this:
if( Syntax[0] != null ){
Log.d("MyApp", "Sintax (0) = " + Syntax[0].displayText());
Text = Syntax[0].displayText();
show.setText(Text);
} else Log.d("MyApp", "Nothing ");
And watch the result in the Logcat pane.

Android Apptitude practice app

I want to develop an aptitude app..
for that in my text view i have to display first question .. On clicking next button i have to display second question.. on again clicking same next button third question have to be displayed.. liked that i want to display some 30 questions ..all questions should be displayed in single java file.I Tried to display two questions . but for multiple questions i could not find the code..
package com.example.asl;
import java.util.Arrays;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Aptitude extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aptitude);
Button b=(Button) findViewById(R.id.button1);
final TextView tv=(TextView) findViewById(R.id.textView1);
final String Question[]={"what is UR Name","What is ur Age","Whats ur Qualification"};
Button btnNext = (Button) findViewById(R.id.button1);
final TextView cumulos = (TextView) findViewById(R.id.textView1);
//TextView respostas = (TextView)findViewById(R.id.respostas);
Random randPhrase = new Random();
final String[] cum = {"what is UR Name","What is ur Age","Whats ur Qualification"};
//String[] resp = getResources().getStringArray(R.array.resp_cumulos);
String textout = "";
String textresp = "";
//Button btnPrevious = (Button) findViewById(R.id.yourPreviousbutton);
btnNext.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
int i = 0;
if(i<cum.length-1){
i+=1;
cumulos.setText(cum[i]);
// respostas.setText(resp[i]);
}
}
});
//btnPrevious.setOnClickListener(new OnClickListener(){
//public void onClick(View arg0) {
//if(i>0){
// i-=1;
// cumulos.setText(cum[i]);
// respostas.setText(resp[i]);
// }
// }
//});
}
}
enter code here
Initializing your counter in your onClick() is always going to reset it
Initialize it outside of onClick() and increment it in onClick() as you are.
public class Aptitude extends Activity {
int i = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aptitude);
Button b=(Button) findViewById(R.id.button1);
...
}
public void onClick(View arg0) { // rename arg0 to something meaningful
// like v or view for readibility
// int i = 0; remove this guy
if(i<cum.length-1){
i+=1;
cumulos.setText(cum[i]);
If this doesn't fix your problem then please explain what the problem is but I'm sure this part was causing you trouble.

How can I keep my Java Android code short?

Hi I am developing an Android app using an EditText, and a add en minus button to control it's value.
I got it working now, but I wanna repeat it to multiple values. I know I can just repeat the code with different variables but the code would big really large.
Anyone an idea how to repeat the same code for multiple values?
This is my current code:
package com.lars.MyApp;
import com.lars.MyApp.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.view.View.OnClickListener;
public class MyAppActivity extends Activity {
int currentValue = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText Value = (EditText) findViewById(R.id.Value);
Button addButton = (Button) findViewById(R.id.addButton);
Button minusButton = (Button) findViewById(R.id.minusButton);
addButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
addValue();
Value.setText("" + currentValue);
}
});
minusButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
minusValue();
Value.setText("" + currentValue);
}
});
}
public void addValue(){
if(currentValue <= 999){
currentValue = currentValue + 1;
}
}
public void minusValue(){
if(currentValue >= 1){
currentValue = currentValue - 1;
}
}
}
You should refactor your OnClickListeners so that they are generic. Probably the easiest way to do this is to change your addValue() method to be addValue(View v), and minusValue() to minusValue(View v). Then, in the layout xml, you add a property android:onClick=addValue or android:onClick=minusValue. You'll have to update the method bodies so that they check the view's id and do the right thing based on that, but you won't have to create and set a whole bunch of OnClickListeners in the onCreate method - you'll get that for free.

Categories

Resources