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.
Related
Whenever I run the app, and type in 4 as the answer, it's still showing incorrect (from the else statement). ( I am relatively new to Android coding, and couldn't find an exact solution online.)
Here is my code:
public class MainActivity extends AppCompatActivity {
EditText answer;
ImageButton ttsBtn;
Button submitBtn;
TextToSpeech textToSpeech;
TextView txt;
int num;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
answer = (EditText)findViewById(R.id.answer);
ttsBtn = (ImageButton) findViewById(R.id.ttsButton);
submitBtn = (Button)findViewById(R.id.submitBtn);
txt = (TextView) findViewById(R.id.question);
String ans = answer.getText().toString();
if (!ans.isEmpty())
{num = Integer.parseInt(answer.getText().toString());}
textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int i) {
if(i!=TextToSpeech.ERROR){
// To Choose language of speech
textToSpeech.setLanguage(Locale.UK);
}
}
});
ttsBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String newSpeech = "What is 2 multiplied by 2?";
textToSpeech.speak(newSpeech,TextToSpeech.QUEUE_FLUSH,null);
}
});
submitBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (num == 4){
Toast.makeText(MainActivity.this, "Answer is Correct!", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this, "Answer is Incorrect", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Replace your submitBtn onclick with below code. Issue is that you are not assigning latest value of answer.
submitBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
num = Integer.parseInt(answer.getText().toString());
if (num == 4){
Toast.makeText(MainActivity.this, "Answer is Correct!", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this, "Answer is Incorrect", Toast.LENGTH_SHORT).show();
}
}
});
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();
}
}
});
}
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 trying to create a messaging app with several different xml views/classes:
one for the main menu (MainActivity);
one for the sending message screen (SendMsg);
one for storing sent messages (Logs).
In SendMsg, I want to store my message into an object called msgLog, which I had created in MainActivity. Just for testing, I have set SendMsg to display the Logs class upon pressing the SEND button, and the sent message does indeed appear there.
However, it is not storing it into the msgLog variable that I created, so that I can't access again from MainActivity.
How do I store it into a variable, so that it will always be there when I access it? Any help would be much appreciated.
=========================================================
public class MainActivity extends AppCompatActivity {
Logs msgLog = new Logs();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendOnClick (View view){
Intent intent = new Intent("com.example.android.attone.SendMsg");
startActivity(intent);
}
public void logsOnClick (View view){
Intent intent = new Intent(this, Logs.class);
startActivity(intent);
}
}
=========================================================
public class SendMsg extends AppCompatActivity {
EditText txtPhoneNo;
EditText txtMessage;
Button btnSend;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_send_msg);
txtPhoneNo = (EditText) this.findViewById(R.id.txtPhoneNo);
txtMessage = (EditText) this.findViewById(R.id.txtMessage);
btnSend = (Button) this.findViewById(R.id.btnSend);
btnSend.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
// Get phone number and message sms
String phoneNo = txtPhoneNo.getText().toString();
String message = txtMessage.getText().toString();
Intent writeLog = new Intent(getApplicationContext(), Logs.class);
writeLog.putExtra("link", message);
startActivity(writeLog);
// If phone number and message is not empty
if (phoneNo.length() > 0 && message.length() > 0)
{
sendMessage(phoneNo, message);
}
else
{
Toast.makeText(getBaseContext(), "Please enter both number and message", Toast.LENGTH_LONG).show();
}
}
});
}
// Function send message sms
private void sendMessage(String phoneNo, String message)
{
try
{
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent", Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(), "SMS failed. Please try again!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
=========================================================
public class Logs extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_logs);
String logging = getIntent().getStringExtra("link");
TextView textView = (TextView) findViewById(R.id.txtLog);
textView.setText(logging);
Button log2menu = (Button)findViewById(R.id.logToMenu);
log2menu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
}
}
You can use SharedPreferences to store such data.
One more thing you can try if you don't want to save the data after the app closes is storing the data in a static variable of a separate class.
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
}