so I started some Android programming with Java 7. I have Eclipse Juno (I think that's 4.2).
The problem is that it gives me an error "Multiple markers at this line
- Syntax error on token ")", ;
expected
- Syntax error on token ")", ;
expected
On the line with the sendMessage method. Here is the code:
public class MainActivity extends ActionBarActivity {
int counter;
Button login;
EditText username, password;
String success;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
username = (EditText)findViewById(R.id.getEmail);
password = (EditText)findViewById(R.id.getPassword);
login.setOnClickListener(new View.OnClickListener() {
public void onClick (View v) {
if(username.getText().toString().equals("admin") && password.getText().toString().equals("admin")){
success = "Successful";
counter = 0;
public void sendMessage (View view){
Intent intent = new Intent("com.example.linked1n.SCREENAFTLOG");
startActivity(intent);
};
} else {
counter++;
login.setText("Unsuccessful. Try again. " + 3-counter + " tries left.");
}
}
});
}
I haven't found a solution anywhere and I did exactly as the tutorial told me. I tried to clean the project thrice and restart eclipse/computer nothing worked.
Your calling method inside onclick is wrong so try below way :-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
username = (EditText)findViewById(R.id.getEmail);
password = (EditText)findViewById(R.id.getPassword);
login.setOnClickListener(new View.OnClickListener() {
public void onClick (View v) {
if(username.getText().toString().equals("admin") && password.getText().toString().equals("admin")){
success = "Successful";
counter = 0;
sendMessage(v);
} else {
counter++;
login.setText("Unsuccessful. Try again. " + 3-counter + " tries left.");
}
}
});
public void sendMessage (View view){
Intent intent = new Intent("com.example.linked1n.SCREENAFTLOG");
startActivity(intent);
}
You have 2 problems in your code :
You are not calling the function, just defining it, that too inside the onClick method, which you should not do.This problem can be resolved by calling the method inside the onClick and defining it outside the onClickListener.
You shouldn't terminate your sendMessage method declaration with a semi colon after the ending curly brace.This problem can be resolved by removing the semicolon from there.
So your code finally looks as below :
public class MainActivity extends ActionBarActivity {
int counter;
Button login;
EditText username, password;
String success;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
username = (EditText)findViewById(R.id.getEmail);
password = (EditText)findViewById(R.id.getPassword);
login.setOnClickListener(new View.OnClickListener() {
public void onClick (View v) {
if(username.getText().toString().equals("admin") && password.getText().toString().equals("admin")){
success = "Successful";
counter = 0;
sendMessage(v);//Method sendMesaage is called inside onClick
} else {
counter++;
login.setText("Unsuccessful. Try again. " + 3-counter + " tries left.");
}
}
});
//Definition of method sendMessage
public void sendMessage (View view){
Intent intent = new Intent("com.example.linked1n.SCREENAFTLOG");
startActivity(intent);
}//Removed semicolon from the method sendMessage
}
Related
Main Activity
i want to check the amount is greater than 100 , after clicking button
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
buyItemButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (amount_checker >= 100){
Intent intent = new Intent(CartActivity.this,AddressActivity.class);
intent.putExtra("itemList", (Serializable) itemsList);
startActivity(intent);
}else {
Toast.makeText(getApplicationContext(), ""+amount_checker, Toast.LENGTH_SHORT).show();
}
}
});
}
i want to get the amount checker value to go to oncreate function
private void calculateAmount() {
amount_checker = 1223.00023223;
}
I want to get the amount checker value to go to onCreate function
Since you need the amount_checker value inside a click listener of a button (and not when the onCreate method is called) you can make amount_checker a field of your CartActivity class.
Then you are just referencing amount_checker as this.amount_checker instead anywhere you need to read or assign the value (maybe in some scopes you might have to access it with CartActivity.this.amount_checker).
class CartActivity {
// this stores the latest amount_checker value
private double amount_checker = 0;
private void calculateAmount() {
this.amount_checker = 1223.00023223;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
buyItemButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (this.amount_checker >= 100){
Intent intent = new Intent(CartActivity.this,AddressActivity.class);
intent.putExtra("itemList", (Serializable) itemsList);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), ""+this.amount_checker, Toast.LENGTH_SHORT).show();
}
}
});
}
where is error? how can I fix it? layout is true error in 24th line that
kaleciKayit.setOnClickListener(new View.OnClickListener() {
here is my all codes
public class oyuncuAdlariActivity extends AppCompatActivity {
Button kaleciKayit;
Button oyuncuKayit;
EditText isimGiris;
String isimGirisString;
int kaleciSayisi = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_oyuncu_adlari);
kaleciKayit = (Button) findViewById(R.id.kaleciButton);
oyuncuKayit = (Button) findViewById(R.id.oyuncuButton);
isimGiris = (EditText) findViewById(R.id.isimGir);
kaleciKayit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
kaleciSayisi++;
isimGirisString = isimGiris.getText().toString();
if (isimGirisString.isEmpty()){
Toast toast1 = Toast.makeText(getApplicationContext(),getApplicationContext().getString(R.string.isimBos), Toast.LENGTH_SHORT);
toast1.show();
}
else if (kaleciSayisi >2)
kaleciKayit.setEnabled(false);
}
});
}
}
First of all, I will recommend setting the objects in a class to private. I think the problem is that maybe you are assigning the id of the wrong widget? check the FindViewByID again.
The other 3 possibilities that I can think about are-
The problem is in the first activity(In this case I ask you to post here the content of the first activity)
The setContentView method points at a wrong XML file.
Your AVD ran out of memory and you need to add more to it in the AVD Manager tool.
Change MainActivity to this,
public class MainActivity extends AppCompatActivity {
Button devamButton;
EditText takim1, takim2;
String takimTextA;
String takimTextB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
devamButton = findViewById(R.id.devamButton);
takim1 = findViewById(R.id.takimB);
takim2 = findViewById(R.id.takimA);
devamButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
takimTextA = takim1.getText().toString();
takimTextB = takim2.getText().toString();
if (takimTextA.equals(takimTextB)) {
Toast toast1 = Toast.makeText(getApplicationContext(),getApplicationContext().getString(takimAyniOlmaz), Toast.LENGTH_SHORT);
toast1.show();
} else if (takimTextA.isEmpty() || takimTextB.isEmpty()) {
Toast toast2 = Toast.makeText(getApplicationContext(), getApplicationContext().getString(R.string.takimBosBirakma), Toast.LENGTH_SHORT);
toast2.show();
} else {
activityGecis1(v);
}
}
});
}
private void activityGecis1(View v) {
Intent gecis1 = new Intent(v.getContext(), oyuncuAdlariActivity.class);
startActivity(gecis1);
}
Since you are calling inside onclicklistener maybe this in intent may point to that functions class.
In my Code I have an Intent to an other Activity but when I use my Phone to test it nothing appears. The program doesn't crashes or something like that. It simply does nothing. I have another Intent and this works perfectly. I don't know what the problem is.
I am using the onClick feature on the xml file
On the Main Activity:
public class MainActivity extends AppCompatActivity {
private Object TextView;
int eggcounter;
Button b1;
android.widget.TextView textClicks;
private Object SafeBrowsingResponse;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button b1 = findViewById(R.id.b1);
eggcounter = 100;
final ImageButton ImgButton = findViewById(R.id.eggBtn);
ImgButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
eggcounter = eggcounter - 1;
updateEgg();
if (eggcounter < 80) {
ImgButton.setImageResource(R.drawable.egg_2);
if (eggcounter < 60){
ImgButton.setImageResource(R.drawable.egg_3);
if (eggcounter < 40) {
ImgButton.setImageResource(R.drawable.egg_4);
if (eggcounter < 15) { ImgButton.setImageResource(R.drawable.egg_5);
if (eggcounter <= 0) {
b1.setVisibility(View.VISIBLE);
ImgButton.setImageResource(R.drawable.egg_ende);
b1.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
}
}
);
}
}
}
}
}
}
}
);
}
public void updateEgg() {
textClicks = (TextView) findViewById(R.id.textScore);
textClicks.setText(eggcounter + " ");
}
public void backstartseite(View view) {
Intent back = new Intent(this, Startseite.class);
startActivity(back);
}
public void ende (View view) {
Intent e = new Intent(this, Ende.class);
startActivity(e);
}
}
You are never calling backStartSeite or ende therefore no Intent fires.
Also don't set the onClickListener of b1 inside another onClickListener (your listener for b1 will only be able to handle click events once the other button has been clicked already - this would confuse users).
If you want your Intent to work, call either startSeite or ende in the outer onClickListener.
I am making this beer/drink app as a fun project. I checked around the site and couldn't find anything specific to my problem. I would greatly appreciate help at two things;
I would like a button to reset whatever the count gets to in this code, I can't seem to figure out what I need..
The countButton is for beer and the drinkButton is for, well yeah, drinks.
public class Activity2 extends AppCompatActivity {
// Private member field to keep track of the count
private int mCount = 0;
private int mSum = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
final TextView countTextView = (TextView) findViewById(R.id.TextViewCount);
final TextView sumTextView = (TextView) findViewById(R.id.textSum);
final ImageButton countButton = (ImageButton) findViewById(R.id.beerCount);
final ImageButton drinkButton = (ImageButton) findViewById(R.id.drinkCount);
countButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mCount++;
mSum += 72;
countTextView.setText("You have been drinking " + mCount + " units!");
sumTextView.setText("Sum:" + mSum + "!");
}
});
drinkButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mCount++;
mSum += 96;
countTextView.setText("You have been drinking " + mCount + " units!");
sumTextView.setText("Sum:" + mSum + "!");
}
});
Also, the count does remove itself when I toggle through tabs in the app, any ideas to make it stay, and only removing by the reset button?
Greatly appreciate any help!
For what you require, simply add a third button to your activity and in its onCLickListener, set the values to 0.
Something like this.
resetButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
this.mSum = 0;
}
});
After trying lot of things found here, nothing worked.
To make it simple :
I have a main activity with a button.
I have a "child" activity with 2 text fields and a button.
When I click the main activity button, the child activity opens (it works). Then I put some text to the two text fields and when I click the button I want the data from the text fields to be transfered to the main activity, and I can't get this working.
Here is my main activity code
public class MainMenu extends AppCompatActivity {
Button btnAddBib;
public final static int REQUEST_CODE_B = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
btnAddBib = (Button)findViewById(R.id.btnAddBib);
btnAddBib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), MainActivity.class);
startActivityForResult(myIntent,REQUEST_CODE_B);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case REQUEST_CODE_B:
String Qte = data.getStringExtra("qte");
String Time = data.getStringExtra("time");
if (Qte != null && !Qte.isEmpty() && Time != null && !Time.isEmpty()) {
final SQLiteDatabase dbBib;
dbBib = openOrCreateDatabase("bibi", Context.MODE_PRIVATE, null);
dbBib.execSQL("CREATE TABLE IF NOT EXISTS bibi(time VARCHAR, qte VARCHAR);");
dbBib.execSQL("INSERT INTO bibi VALUES ('" + Time + "','" +
Qte + "')");
Toast.makeText(getApplicationContext(), "Bibi bien enregistré",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Fail get extraData",
Toast.LENGTH_LONG).show();
}
break;
}
}
And the child activity code :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ajouter_bibi);
btnValideBib = (Button)findViewById(R.id.btnValideBib);
btnValideBib.setBackgroundResource(R.drawable.logo_biberon);
txtTime = (EditText)findViewById(R.id.txtTime);
txtQte = (EditText)findViewById(R.id.txtQte);
Calendar c = Calendar.getInstance();
int hh = c.get(Calendar.HOUR_OF_DAY);
int mm = c.get(Calendar.MINUTE);
txtTime.setText(hh + ":" + mm);
btnValideBib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent backIntent = new Intent();
backIntent.putExtra("qte", txtQte.getText());
backIntent.putExtra("time", txtTime.getText());
setResult(RESULT_OK, backIntent);
finish();
}
});
The problem :
In the main activity, when I want to get back my Extra Strings I have null fields.
String Qte = data.getStringExtra("qte");
String Time = data.getStringExtra("time");
What is wrong with it ?
Thanks!
Change the code :
backIntent.putExtra("qte", txtQte.getText());
backIntent.putExtra("time", txtTime.getText());
to below code:
backIntent.putExtra("qte", txtQte.getText().toString()); //append .toString() method
backIntent.putExtra("time", txtTime.getText().toString());