繁体   English   中英

“ R无法解析为变量”

[英]“R cannot be resolved to a variable”

所有,

我知道这是一个不断出现在这个论坛上的问题,但是以前的解决方案似乎都没有为我解决问题。 以下是错误:

“ R无法解析为变量”

“'d'无法解析”(在“ btnCalculate”块的最后一行)

“ edttxtfinalWeight无法解析”(同一行)

我没有导入android.R,我还尝试了“ import com.example.spinnertutorial.R”(无法正常工作,使我所有的文本字段和按钮都无法解析),并且我尝试了清理并且重建项目的次数超出了我的预期,那无济于事。 我有点机智在这里...

这是我的代码:

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);
}
}

任何帮助将不胜感激,谢谢!

您正在使用支持库吗? 好的,让我们尝试一些选项

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.

它发生在大多数时间。

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

出现错误可能会使您的项目无法正确重建

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

最后,您有android构建工具吗? 检查这篇文章希望对您有帮助

编辑:在您的d.format(planetWeight)上,我看不到您在代码中将“ d”实例化为变量以使用它的任何地方。.(java是面向对象的)...希望是对的

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM