Android app development error - multiple markers at this line - java

I have just started android development in eclipse with android and am trying to program a button, this is my code,
package my.Apprentice;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class ApprenticeVoteActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startVotingListener();
}
private void startVotingListener() {
final Button startVoting = (Button) findViewById(R.id.startVoting);
startVoting.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
} // Multiple markers at this line error shows here
};)
}
}
The location of my error is commented above. I really have no idea on how to get rid of it, I have tried cleaning my project with no success. Does anyone have any ideas ? Thanks !
The Updated and working code is shown below:
package my.Apprentice;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ApprenticeVoteActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startVotingListener();
}
private void startVotingListener() {
final Button startVoting = (Button) findViewById(R.id.startVoting);
startVoting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
}

Try });
import android.view.View;
import android.view.View.OnClickListener;
startVoting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
} // Multiple markers at this line error shows here
}); // Sequence is wrong

Did you try to remove the ';' after the listener declaration ? Remove also the View reference :
startVoting.setOnClickListener(new View.OnClickListener() { <-- HERE
#Override
public void onClick(View v) {
} // Multiple markers at this line error shows here
};) // <--- HERE
Final :
startVoting.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});

Related

Cannot Resolve Symbol 'setOnClickListener'

I am fairly new to Android/Java developing and I have run into this error. I cannot figure out what is wrong after doing extensive research and playing with my methods. Here is my code:
package org.flinthill.finalprojectv2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.text.method.DigitsKeyListener;
import android.text.InputFilter;
public class mainactivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
final Button SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener()
{
new View.OnClickListener() {
#Override
public void onClick(View view){
}
};
}
}
If anyone knows what I could've done wrong, please let me know! Thanks!
Put the code inSide onCreate()
public class mainactivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Button Clicked
}
});
}
}
put your code inside onCreate() method
see the android lifeCycle to understand why :https://developer.android.com/guide/components/activities/activity-lifecycle.html
Your code is out of your method, Change it to this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener()
{
new View.OnClickListener() {
#Override
public void onClick(View view){
}
};
}
}
Do Button initialization and adding listener to Button inside onCreate() method.
Try this:
package org.flinthill.finalprojectv2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.text.method.DigitsKeyListener;
import android.text.InputFilter;
public class mainactivity extends AppCompatActivity {
Button SuSe;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something
}
});
}
}

Button and click display

I have tried Everything to my knowledge and nothing seems to work. Any help would be appreciated.
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class activity_main extends Activity {
TextView txtCount;
Button btnCount;
int count=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtCount= (TextView) findViewById(R.id.textView);
txtCount.setText(String.valueOf(count));
btnCount= (Button)findViewById(R.id.button);
btnCount.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
count++;
txtCount.setText(String.valueOf(count));
}
});
}
}
}
No errors appear at any point and the program works without errors; the button clicks and does not crash, but no click is counted numerically in the textView.
you onClick() listener is wrong.
try this one
btnCount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count++;
txtCount.setText(String.valueOf(count));
}
});

Android Java implement 2 buttons in 1 page

I finally learned about adding a button to a page and actually making it navigate to another activity "XML Page". Anyway, I have been trying to add 2 buttons in the same page which navigate each to a different XML's Pages. All I did was copy the first button which worked and then change the button name and all other things the first button works but the second isn't. It shows a click but nothing happens after.
Back1 Button works. TMode Button does the trouble.
Eclipse is not showing errors.
Here is my code -
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class GameMode extends Activity {
/** Called when the activity is first created.*/
Button btn;
Button btn1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode);
btn=(Button)findViewById(R.id.Back1);
btn.setOnClickListener(btn2Listener);
}
private OnClickListener btn2Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent2=new Intent(GameMode.this,MainActivity.class);
startActivity(intent2);
}
};
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode);
btn=(Button)findViewById(R.id.TMode);
btn.setOnClickListener(btn3Listener);
}
private OnClickListener btn3Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent3=new Intent(GameMode.this,CharacterSelect.class);
startActivity(intent3);
}
};
}
Try something like this:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class GameMode extends Activity {
Button btn1;
Button btn2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode);
btn1=(Button)findViewById(R.id.Back1);
btn1.setOnClickListener(btn1Listener);
btn2=(Button)findViewById(R.id.TMode);
btn2.setOnClickListener(btn2Listener);
}
private OnClickListener btn1Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent1=new Intent(GameMode.this,MainActivity.class);
startActivity(intent2);
}
};
private OnClickListener btn2Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent1=new Intent(GameMode.this,CharacterSelect.class);
startActivity(intent2);
}
};
}
You should in your XML file define two buttons
<Button
android:id="#+id/button1"
... />
<Button
android:id="#+id/button2"
... />
And then in your Activity in onCreate() method you do
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
...
})
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
...
});
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class GameMode extends Activity {
Button btn1;
Button btn2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_mode);
btn1=(Button)findViewById(R.id.Back1);
btn1.setOnClickListener(btn1Listener);
btn2=(Button)findViewById(R.id.TMode);
btn2.setOnClickListener(btn2Listener);
}
private OnClickListener btn1Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent1=new Intent(GameMode.this,MainActivity.class);
startActivity(intent1);
}
};
private OnClickListener btn2Listener=new OnClickListener() {
public void onClick(View v) {
Intent intent2=new Intent(GameMode.this,CharacterSelect.class);
startActivity(intent2);
}
};
}

Dismiss activity in android

I am using the following code to show “Hello” message with ok button when the user clicks a button. In some applications this is working fine. ie while clicking the ok button, the activity is dismissed. But in one application, this is not getting dismissed after clicking the ok button. What to do? Please help.
public class MyClass extends Activity {
private TextView labelTxt;
private Button okBtn;
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.message);
labelTxt = (TextView) findViewById(R.id.txt);
labelTxt.setText("Hello");
okBtn = (Button) findViewById(R.id.okBtn);
okBtn.setOnClickListener(okBtnClickListener);
}
private final OnClickListener okBtnClickListener = new OnClickListener() {
public void onClick(View v) {
finish();
}
};
}
I have tested your code and modified little. Please check below
package test.stackoverflow;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.btnOK);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
Try like this....
okBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
finish();// Closing Activity
}
});
In your layout make sure your Button is named:
android:id="#+id/okBtn"
.....
Did you import:
import android.view.View.OnClickListener;
Please look at this tutorial, it should help:
http://martin.cubeactive.com/android-onclicklitener-tutorial/

Android Activities startactivity [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Android - How to open Activity by clicking button
i want to open another Activity(GameProcess) from this one (KlikomaniaActivity) through button, but when i tap the button the program crashes. i don't a proffesional android programmer, please say what mistakes i have:
packagecom.makeandroid.klikomania;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class KlikomaniaActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button Butstart = (Button)findViewById(R.id.butstart);
final Button Butrez = (Button)findViewById(R.id.butrez);
Butstart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("com.makeandroid.klikomania.GameProcess");
// эапускаем деятельнсть
startService(intent);
}
});
}
}
and here GameProcess Acticity:
public class GameProcess extends KlikomaniaActivity {
private static int rezult = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gameprocess);
final Button Butklik = (Button)findViewById(R.id.klik);
final TextView TextTime = (TextView)findViewById(R.id.texttime);
final TextView TextKolvo = (TextView)findViewById(R.id.kolvo);
Butklik.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
rezult=rezult+1;
TextKolvo.setText(rezult);
}
});
}
}
First make sure that you added your activity to the manifest file :
<activity android:name="com.makeandroid.klikomania.GameProcess"></activity>
second, to start the activity use this code:
final Intent gameProcessIntent= new Intent(this, GameProcess.class);
Butstart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(gameProcessIntent);
}
});
this should work normaly

Categories

Resources