Syntax Error Insert ; and } to complete statement? - android

I have just started to learn how to program android apps, And im a COMPLETE NOOB.
I cant figure out how to fix this! please help! The format may look butchered... It's cause i have no clue what im doing!
package com.smiggle.bmxhandbook;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Button button;
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick1(View arg0) {
Intent myIntent = new Intent(MainActivity.this, Trick.class
MainActivity.this.startActivity(myIntent);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub; }
}
It keeps on giving me a syntax error!

Try this code instead:
public class MainActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent myIntent = new Intent(MainActivity.this, Trick.class);
MainActivity.this.startActivity(myIntent);
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Your class had a ridiculously high amount of syntax and logical errors:
Arbitrary onCreate1() method, which will never me called.
Arbitrary onClick() method, even though your Activity never implemented the interface
Your anonymous inner class' onClick() method was named onClick1()
The statement with the Intent was missing a ); at the end.
The inner class body was missing a closing });
The addListenerOnButton() method was missing a closing }
Your entire class was missing a closing }
I sincerely recommend you spend a few months (or even a year) learning Java before coming to Android.

You have this wide open
Intent myIntent = new Intent(MainActivity.this, Trick.class
You need to close that with a right parenthesis and and a semicolon.
Also that onClick should not have a 1 in it:
public void onClick1(View arg0) {

Related

Implementing GoogleAnalytics in Android App

I am implementing Google Analytics in Android App using eclipse. After lot of research i implemented it. Here is my Code:
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.MapBuilder;
import com.google.analytics.tracking.android.StandardExceptionParser;
public class MainActivity extends Activity {
private EasyTracker easyTracker = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
easyTracker = EasyTracker.getInstance(MainActivity.this);
Button btn1 = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
easyTracker.send(MapBuilder.createEvent("your_action",
"envet_name", "button_name/id", null).build());
}
});
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
try {
int a[] = new int[2];
int num = a[4];
} catch (ArrayIndexOutOfBoundsException e) {
easyTracker.send(MapBuilder.createException(
new StandardExceptionParser(MainActivity.this, null)
.getDescription(Thread.currentThread().getName(), e), false).build());
}
}
});
}
#Override
public void onStart() {
super.onStart();
EasyTracker.getInstance(this).activityStart(this);
}
#Override
public void onStop() {
super.onStop();
EasyTracker.getInstance(this).activityStop(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The code compiled successfully but the problem which i am facing is that it is not sending any information in the GoogleAnalytics account.I have not received any part of information in google Analytics.

NullPointerException at OnClickListener in Eclipse

package com.example.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.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
public class QuizActivity extends ActionBarActivity {
Button mTrueButton;
Button mFalseButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mTrueButton = (Button)findViewById(R.id.button1);
mTrueButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show();
}
});
mFalseButton = (Button)findViewById(R.id.button2);
mFalseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(QuizActivity.this, R.string.incorrect_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;
}
#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);
}
}
Please help guys. Getting a NullPointerException at mTrueButton.setOnClickListener(new View.OnClickListener().
I know other ways to set the OnClickListener but just wanted to know what's wrong with this code. Thanks in advance
You need to initialize your mTrueButton. Likely with a call to findViewById() after setContentView(), like you're initializing mFalseButton.
You forgot to initialize mTrueButton buttons:
mTrueButton= (Button)findViewById(R.id.buttonId);
You need to initialize mTrueButton like you did with mFalseButton
For eg.
mTrueButton = (Button)findViewById(R.id.button1); \\ button1 is whatever id you have given to mTrueButton
Until you do that mTrueButton is null and trying to call any method on it will result in a NullPointerException
Just initialize your mTrueButton like this:
mTrueButton = (Button)findViewById(R.id.button1);
before setting onClickListener
You forgot to initialize the buttons:
Button mTrueButton;
Button mFalseButton;
just add the findViewById() method to each button so you can access to them.
mTrueButton = (Button)findViewById(R.id.button1);

Home page with several buttons to be tapped that lead to different activities?

I've been trying to make a home page with several buttons that can be tapped to lead to different activities (classes, i'm working in Eclipse). I've got one button leading to an activity fine. On copying and pasting it and changing information to match the new layout and in editing the manifest, I keep getting errors. Either the app crashes or the button remains defunct. I've followed the links below and it's still not matching correctly:
Android - Creating a new activity in Eclipse
How to start new activity on button click
http://www.youtube.com/watch?v=cv2bh53IL_Y
All of which create errors when you duplicate the work. Here's what I have below:
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button userguide;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userguide = (Button)findViewById (R.id.userguide);
userguide.setOnClickListener (new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,userguide.class);
startActivity(intent);
}
});
}
{
Button worldinformation;
Intent startNewActivityOpen = new Intent(MainActivity.this, worldinformation.class);
startActivityForResult (startNewActivityOpen, 0);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Thanks!
Your new Intent line is a little off. Instead of the button (userguide) class you want to use the NewActivity you want to start class.
userguide.setOnClickListener (new View.OnClickListener() {
#Override
public void onClick(View v) {
// CHANGE "NextActivity" to the class of the activity you want to start.
Intent intent = new Intent(v.getContext(), NextActivity.class);
startActivity(intent);
}
});

App crashes when Launching "Unfortunately, has stopped"

I have a program and when I try to start it, it crashes. I really don't understand why, even Eclipse show's that there are no errors.
I can show you the code of a page on which I think is the problem.
package ctect.android.maxipro;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class BasicScreenActivity extends Activity {
private Button butonul1;
private Button butonul2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basic_screen);
butonul1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View currentView) {
// TODO Auto-generated method stub
butonul2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View currentView) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(currentView.getContext(), NeedForSpeedActivity.class);
startActivityForResult(myIntent, 0);
Intent myIntent2 = new Intent(currentView.getContext(), Fifa2012Activity.class);
startActivityForResult(myIntent2, 0);
}
});
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.basic_screen, menu);
return true;
}
}
if somebody understands, please help me.
You forgot to assign an object to butonul1 before using it. You need to add this line before butonul1.setOnClickListener:
butonul1= (Button) findViewById(R.id.butonul1);
This is assuming that you gave it the id butonul1 in your layout file.

Don't know why condition in the for statement executing in android

on clicking the button the 'x' value is storing in array[0][0]. But in the check method both if statements in the for loop are exicuting , i don't know why the conditions are exicuting.
So i put a dialogue box in the if statement, while i am giving the array[0][0] to setMessage
it printing "x", But if i am giving the array[i][j] it printing the null value even both i and j values are zero.
i don't know what is the problem.
package com.example.tictactoe3;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.app.AlertDialog;
public class MainActivity extends Activity {
String array[][]=new String[3][3];
void check(String array[][])
{
int i=0,j=0;
for(i=0;i<3;i++)
{
for(j=0;j<1;j++)
{
if(array[i][j]==array[i][j+1])
{
if(array[i][j]==array[i][j+2])
{
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage(array[0][0]);
dlgAlert.setTitle("Tic Tac Toe");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}
}
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button bt1= (Button) findViewById(R.id.button1);
bt1.setOnClickListener(new View.OnClickListener() {
boolean value=true;
public void onClick(View v) {
// TODO Auto-generated method stub
bt1.setText("x");
array[0][0]="x";
check(array);
}
});
}
#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;
}
}
For strings, you should compare using
if (strOne.equals(strTwo))
or
if (strOne.equalsIgnoreCase(strTwo))
Not using "==" operator
So replace
if(array[i][j]==array[i][j+1])
with
if(array[i][j].equalsIgnoreCae(array[i][j+1]))
and
if(array[i][j]==array[i][j+2])
with
if(array[i][j].equalsIgnoreCase(array[i][j+2]))

Categories

Resources