Reputation: 217
I can't figure out why Eclipse is complaining about my second bracket for the "OnClick" method...
I know its a simple thing, but I just can't seem to fix it!
Code for my activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CallService cs = new CallService();
TotalTime = TotalTime + (cs.EndTime - cs.StartTime);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast toast = Toast.makeText(getApplicationContext(), ""+TotalTime, Toast.LENGTH_SHORT);
toast.show();
} // <-- Why is it complaining about this bracket?!
}
Upvotes: 0
Views: 132
Reputation: 2180
I would suggest you to put your code in an IDE such as eclipse. It will generally reflect the point of compilation error and so you don't have to put an extra effort searching for such errors. In case you want to format your code just do the following steps in eclipse
while your cursor is in the coded region
press ctrl + A // it selects all code
then press ctrl + shift + F // this formats your code
now you can easily spot starting and ending point of parenthesis and your code looks nicely formatted.
Upvotes: 1
Reputation: 33544
1. You gave an extra bracket
at the Wrong Place, and missed a bracket
and a brace
with a semicolon
.
- Wrong:
new View.OnClickListener()) // Dont add ")" here.
- Correct:
new View.OnClickListener()
- Missed:
} );
2. Replace this part with yours in the program, and it will work for sure...
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Toast toast = Toast.makeText(getApplicationContext(), ""+TotalTime, Toast.LENGTH_SHORT);
toast.show();
}
} );
3. For further ease i am posting below the entire working code of yours....
///////////// The Whole onCreate() with the corrected code///////////////
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CallService cs= new CallService();
TotalTime = TotalTime + (cs.EndTime-cs.StartTime);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Toast toast = Toast.makeText(getApplicationContext(), ""+TotalTime, Toast.LENGTH_SHORT);
toast.show();
}
} );
}
Upvotes: 1
Reputation: 12823
Your're missing this:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Toast toast = Toast.makeText(getApplicationContext(), ""+TotalTime, Toast.LENGTH_SHORT);
toast.show();
}
}); // <------------- Close the parenthsesis!!
Upvotes: 2
Reputation: 86958
For every open parenthesis or bracket you need to close it, you also need to finish every statement with a semi-colon. The compiler wants });
added:
// You open these, so you need to close them in order
// v v
button.setOnClickListener(new View.OnClickListener() {
// v
public void onClick(View v) {
// Perform action on click
Toast toast = Toast.makeText(getApplicationContext(), ""+TotalTime, Toast.LENGTH_SHORT);
toast.show();
}
// ^
}); // This is what the compiler wants
// ^^
Upvotes: 4