Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Need help with Android and Eclipse?
New Posts  All Forums:Forum Nav:

Need help with Android and Eclipse?

post #1 of 8
Thread Starter 
Trying to finish up my code for my app for my class, making a small mpg calculator, it keeps giving me error on my setOnClickListener. Does anyone have any suggestions on my code?

Quote:
public class mpgCalculator extends Activity {



final Button mpgbutton = (Button) findViewById(R.id.button1);
final EditText miles = (EditText) findViewById(R.id.editText1);
final EditText gallons = (EditText) findViewById(R.id.editText2);
final TextView display = (TextView) findViewById(R.id.textView1);
double miles_double;
double gallons_double;
double finalCal;


mpgbutton.setOnClickListener(new OnClickListener() {
//error here ^^
public void onClick(View v){

miles_double = Double.parseDouble(miles.getText().toString());
gallons_double = Double.parseDouble(gallons.getText().toString());
finalCal = miles_double / gallons_double;
display.setText(String.valueOf(finalCal));

}
});


}// end class
Error eclipse shows:
Multiple markers at this line
- Syntax error on token(s), misplaced construct(s)
- Syntax error on token "setOnClickListener", = expected after
Edited by -bl4ck- - 4/28/11 at 12:19pm
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
955 BE Crosshair V MSI 6950 twin frozr III 8GB Vengence 1600MHz 
Hard DriveOptical DriveOSMonitor
F3 1TB ASUS 24" Asus 
PowerCaseMouse
Corsair AX850 Corsair 600T White Mionix Naos 5000 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
955 BE Crosshair V MSI 6950 twin frozr III 8GB Vengence 1600MHz 
Hard DriveOptical DriveOSMonitor
F3 1TB ASUS 24" Asus 
PowerCaseMouse
Corsair AX850 Corsair 600T White Mionix Naos 5000 
  hide details  
Reply
post #2 of 8
I think you messed up your closing parenthesis location:

Code:
public class mpgCalculator extends Activity {



final Button mpgbutton = (Button) findViewById(R.id.button1);
final EditText miles = (EditText) findViewById(R.id.editText1);
final EditText gallons = (EditText) findViewById(R.id.editText2);
final TextView display = (TextView) findViewById(R.id.textView1);
double miles_double;
double gallons_double;
double finalCal;


mpgbutton.setOnClickListener(new OnClickListener()) {
//error here ^^
public void onClick(View v){

miles_double = Double.parseDouble(miles.getText().toString());
gallons_double = Double.parseDouble(gallons.getText().toString());
finalCal = miles_double / gallons_double;
display.setText(String.valueOf(finalCal));

}
}; //Deleted ")"


}// end class
i7
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 D0 w/ HT On [4.01ghz @ 1.208v] Asus P6X58D Premium Asus HD5870 G.SKILL 6GB (3X2GB) DDR3 @ 1528MHz 
Hard DriveOptical DriveOSMonitor
OCZ Vertex Turbo 60GB FW1.5 2xSamsung Spinpoint F3 LG 8x Blu-ray Combo Drive Windows 7 64-bit Dell U2410 Rev. A04 
PowerCase
OCZ ModXStream Pro 700W V2.2 HAF 922 
  hide details  
Reply
i7
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 D0 w/ HT On [4.01ghz @ 1.208v] Asus P6X58D Premium Asus HD5870 G.SKILL 6GB (3X2GB) DDR3 @ 1528MHz 
Hard DriveOptical DriveOSMonitor
OCZ Vertex Turbo 60GB FW1.5 2xSamsung Spinpoint F3 LG 8x Blu-ray Combo Drive Windows 7 64-bit Dell U2410 Rev. A04 
PowerCase
OCZ ModXStream Pro 700W V2.2 HAF 922 
  hide details  
Reply
post #3 of 8
It would also be very helpful if you told us the error the compiler was throwing, too.
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
post #4 of 8
The problem is in the way you are declaring the onClickListener. I think you may need to declare it somewhere else rather than new it in the arguments list.
Edited by sidcitris - 4/29/11 at 6:34am
i7
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 D0 w/ HT On [4.01ghz @ 1.208v] Asus P6X58D Premium Asus HD5870 G.SKILL 6GB (3X2GB) DDR3 @ 1528MHz 
Hard DriveOptical DriveOSMonitor
OCZ Vertex Turbo 60GB FW1.5 2xSamsung Spinpoint F3 LG 8x Blu-ray Combo Drive Windows 7 64-bit Dell U2410 Rev. A04 
PowerCase
OCZ ModXStream Pro 700W V2.2 HAF 922 
  hide details  
Reply
i7
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 D0 w/ HT On [4.01ghz @ 1.208v] Asus P6X58D Premium Asus HD5870 G.SKILL 6GB (3X2GB) DDR3 @ 1528MHz 
Hard DriveOptical DriveOSMonitor
OCZ Vertex Turbo 60GB FW1.5 2xSamsung Spinpoint F3 LG 8x Blu-ray Combo Drive Windows 7 64-bit Dell U2410 Rev. A04 
PowerCase
OCZ ModXStream Pro 700W V2.2 HAF 922 
  hide details  
Reply
post #5 of 8
Send me a PM, and I'll help you when I get home in a few hours.

EDIT: Change this...
Code:
mpgbutton.setOnClickListener(new OnClickListener() {
... to this
Code:
mpgbutton.setOnClickListener(new View.OnClickListener() {
I think that's it anyway.
Edited by dzalias - 5/1/11 at 8:21pm
Rig
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500k MSI P67A-GD65 (B3) 760ti G.Skill Ripjaws 
Hard DriveCoolingOSMonitor
OCZ Vertex 2 Noctua NH D14 Windows 7 64bit Dell SP2309W 
MonitorMonitorKeyboardCase
Dell S2009W Dell S2009W CM Storm Quickfire XT NZXT Phantom Black 
Mouse
Razer Deathadder Chroma 
  hide details  
Reply
Rig
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500k MSI P67A-GD65 (B3) 760ti G.Skill Ripjaws 
Hard DriveCoolingOSMonitor
OCZ Vertex 2 Noctua NH D14 Windows 7 64bit Dell SP2309W 
MonitorMonitorKeyboardCase
Dell S2009W Dell S2009W CM Storm Quickfire XT NZXT Phantom Black 
Mouse
Razer Deathadder Chroma 
  hide details  
Reply
post #6 of 8
I'd do it like this:

Code:
public class mpgCalculator extends Activity implements OnClickListener {
//...

 mpgbutton.setOnClickListener(this); 

} 
 
public void onClick(View v){

 miles_double = Double.parseDouble(miles.getText().toString());
 gallons_double = Double.parseDouble(gallons.getText().toString());
 finalCal = miles_double / gallons_double;
 display.setText(String.valueOf(finalCal));

 }
}// end class
buka
(17 items)
 
  
Reply
buka
(17 items)
 
  
Reply
post #7 of 8
Thread Starter 
Guys thanks for the help I am a super noob and forgot to set my onCreate method + reps

here it is finished

Quote:
package com.google.examples;

import java.text.DecimalFormat;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class finalApp extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final Button mpgbutton = (Button) findViewById(R.id.button2);
final EditText miles = (EditText) findViewById(R.id.editText2);
final EditText gallons = (EditText) findViewById(R.id.editText1);
final TextView display = (TextView) findViewById(R.id.textView1);

mpgbutton.setOnClickListener(new View.OnClickListener()
{

public void onClick(View v)
{
double miles_double;
double gallons_double;
double finalCal;

miles_double = Double.parseDouble(miles.getText().toString());
gallons_double = Double.parseDouble(gallons.getText().toString());
finalCal = miles_double / gallons_double;
DecimalFormat df = new DecimalFormat("#.00");
display.setText(String.valueOf(df.format(finalCal) + " MPG"));

}
});


}
}
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
955 BE Crosshair V MSI 6950 twin frozr III 8GB Vengence 1600MHz 
Hard DriveOptical DriveOSMonitor
F3 1TB ASUS 24" Asus 
PowerCaseMouse
Corsair AX850 Corsair 600T White Mionix Naos 5000 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
955 BE Crosshair V MSI 6950 twin frozr III 8GB Vengence 1600MHz 
Hard DriveOptical DriveOSMonitor
F3 1TB ASUS 24" Asus 
PowerCaseMouse
Corsair AX850 Corsair 600T White Mionix Naos 5000 
  hide details  
Reply
post #8 of 8
Quote:
Originally Posted by poroboszcz View Post
I'd do it like this:

Code:
public class mpgCalculator extends Activity implements OnClickListener {
//...

 mpgbutton.setOnClickListener(this); 

} 
 
public void onClick(View v){

 miles_double = Double.parseDouble(miles.getText().toString());
 gallons_double = Double.parseDouble(gallons.getText().toString());
 finalCal = miles_double / gallons_double;
 display.setText(String.valueOf(finalCal));

 }
}// end class
I do it that way. Makes calling Activity methods easier.

Quote:
Originally Posted by -bl4ck- View Post
Guys thanks for the help I am a super noob and forgot to set my onCreate method + reps

here it is finished
Ugh. I thought you were leaving it out for clarifications sake . Good job on getting it working, though!
Rig
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500k MSI P67A-GD65 (B3) 760ti G.Skill Ripjaws 
Hard DriveCoolingOSMonitor
OCZ Vertex 2 Noctua NH D14 Windows 7 64bit Dell SP2309W 
MonitorMonitorKeyboardCase
Dell S2009W Dell S2009W CM Storm Quickfire XT NZXT Phantom Black 
Mouse
Razer Deathadder Chroma 
  hide details  
Reply
Rig
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 2500k MSI P67A-GD65 (B3) 760ti G.Skill Ripjaws 
Hard DriveCoolingOSMonitor
OCZ Vertex 2 Noctua NH D14 Windows 7 64bit Dell SP2309W 
MonitorMonitorKeyboardCase
Dell S2009W Dell S2009W CM Storm Quickfire XT NZXT Phantom Black 
Mouse
Razer Deathadder Chroma 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Need help with Android and Eclipse?