0

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!

GSee
  • 48,880
  • 13
  • 125
  • 145
Cal S.
  • 29
  • 7

1 Answers1

0

you are using support library right?? ok lets try some options

1: goto your support library folder and open libs and copy support V7 appcompat jar,
   pat it to your project and add  to build path-(thats if you've not done that)
   if its already done delete what's there-(in ur project) & redo.

it happens most of the time..

2: check your resource folder for both support library and project.. right click on
   support library and click properties and select android and choose the latest
   build target.. and clean and rebuild

an error there can make your project not rebuild correctly

3: R is an automatically generated class that holds the constants used to identify
 your >resources. If you don't have an R.java file I would recommend closing and reopening your
 project or going to >Project > Build all (and selecting "Build Automatically"). If that doesn't work than try making a new project

lastly do you have android build tools? check this post hope it helps

Edit: on your d.format(planetWeight) i dont see any where in your code that you instantiated "d" as variable to use it..(java is object onriented)...hope am right

Community
  • 1
  • 1
Elltz
  • 10,730
  • 4
  • 31
  • 59
  • Ok, thank you for the response. It looks like closing it and reopening it worked entirely, with the exception of the following: edtfinalWeight.setText("Your wieght on "+strPlanet+" is " +d.format(planetWeight)); It still says "d cannot be resolved". I do have java.text.DecimalFormat imported. How do I fix that? Note: original post had error in that it was originally "edttxtfinalWeight". – Cal S. Oct 15 '14 at 14:50
  • You are absolutely correct, sir. I completely forgot to add that declaration. Thanks so much! – Cal S. Oct 15 '14 at 15:01