all,
I know this is a question that keeps coming up on this forum, but none of the previous solutions appears to be solving the problem for me. The following are the errors:
"R cannot be resolved to a variable"
"'d' cannot be resolved" (in the final line of the "btnCalculate" block)
"edttxtfinalWeight cannot be resolved" (same line)
I didn't import android.R, I've also tried 'import com.example.spinnertutorial.R' (didn't work, made all my textfields and buttons not be able to be resolved), and I've tried cleaning and rebuilding the project more times than I can count to no avail. I'm sorta at my wits end here...
Here's my code:
package com.example.spinnertutorial;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.text.DecimalFormat;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class MainActivity extends ActionBarActivity {
public Spinner spinnerPlanet;
final double gravity = 9.8;
double planetWeight=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinnerPlanet = (Spinner)findViewById(R.id.spinnerPlanet);
Button btnCalculate = (Button)findViewById(R.id.btnCalculate);
final EditText edtWeight = (EditText)findViewById(R.id.edttxtEnterWeight);
final EditText edtfinalWeight = (EditText)findViewById(R.id.edttxtfinalWeight);
edtfinalWeight.setEnabled(false);
btnCalculate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//TODO Auto-generated method stub
double dblWeight = Double.valueOf(edtWeight.getText().toString());
String strPlanet = spinnerPlanet.getSelectedItem().toString();
if (strPlanet.equals("Mercury"))
{
planetWeight = (dblWeight*3.7)/gravity;
}
else if (strPlanet.equals("Venus"))
{
planetWeight = (dblWeight*8.87)/gravity;
}
else if (strPlanet.equals("Earth"))
{
planetWeight = (dblWeight*9.8)/gravity;
}
else if (strPlanet.equals("Mars"))
{
planetWeight = (dblWeight*3.69)/gravity;
}
else if (strPlanet.equals("Jupiter"))
{
planetWeight = (dblWeight*20.87)/gravity;
}
else if (strPlanet.equals("Saturn"))
{
planetWeight = (dblWeight*10.4)/gravity;
}
else if (strPlanet.equals("Uranus"))
{
planetWeight = (dblWeight*8.43)/gravity;
}
else if (strPlanet.equals("Neptune"))
{
planetWeight = (dblWeight*10.71)/gravity;
}
else if (strPlanet.equals("Pluto"))
{
planetWeight = (dblWeight*0.658)/gravity;
}
edttxtfinalWeight.setText("Your wieght on "+strPlanet+" is " +d.format(planetWeight));
}});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Any assistance would be appreciated, thanks!