简体   繁体   English

为什么我的代码不起作用?

[英]Why does my code not work?

I have the following code to get information from a rating bar and put it into a TextView but it doesn't seem to be working: 我有以下代码从分级栏中获取信息,并将其放入TextView中,但似乎无法正常工作:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView ratingText = (TextView) findViewById(R.id.ratingText);
    RatingBar rating=(RatingBar)findViewById(R.id.rating);
    rating.setOnRatingBarChangeListener((OnRatingBarChangeListener) this);

}

public void onRatingChanged(RatingBar ratingBar,float rating, boolean fromUser){
    ratingText.setText(""+this.rating.getRating()); 
}

At the second last line of code it is giving me the following error: "rating cannot be resolved or is not a field" and "ratingText cannot be resolved". 在代码的第二行,它给了我以下错误:“ rating无法解析或不是字段”和“ ratingText无法解析”。 I am wondering why I am getting these errors and how to fix them. 我想知道为什么会收到这些错误以及如何解决它们。 Thanks. 谢谢。

a-) I think your problem is that you need that your class MainActivity implements OnRatingBarChangeListener : a-)我认为您的问题是您需要您的类MainActivity实现OnRatingBarChangeListener

public class MainActivity extends Activity implements OnRatingBarChangeListener {

}

b-) Another possible error is that you need to make sure that you do NOT have this in your imports: b-)另一个可能的错误是您需要确保导入中没有此错误:

import android.R;

but: 但:

import your.application.packagename.R;

c-) Declare the TextView ratingText and the RatingBar rating outside of the method, as an attribute of the class. c-)在方法外部声明TextView ratingTextRatingBar rating ,作为类的属性。

Hope it helps! 希望能帮助到你!

Take a look at this for a solution. 看一下这个解决方案。

r.string error "cannot be resolved or is not a field" r.string错误“无法解析或不是字段”

Main idea is to make sure your R reference is correct, and try by deleting the gen files and rebuild the project. 主要思想是确保您的R参考正确无误,然后尝试删除gen文件并重建项目。

EDIT:: This was incorrect the problem was stated in another answer. 编辑::这是不正确的问题在另一个答案中指出。 by @Marv 通过@Marv

It is because you create your ratingText and rating in the onCreate method. 这是因为您在onCreate方法中创建了ratingTextrating This means they are only available inside this method. 这意味着它们仅在此方法内可用。

Try to do something like this: 尝试做这样的事情:

private TextView ratingText = null;
private RatingBar rating = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ratingText = (TextView) findViewById(R.id.ratingText);
    rating=(RatingBar)findViewById(R.id.rating);
    rating.setOnRatingBarChangeListener((OnRatingBarChangeListener) this);

}

This way they are available in the entire class and you can use them in onRatingChanged . 这样,它们可以在整个类中使用,您可以在onRatingChanged使用它们。

try this. 尝试这个。

public class Test extends Activity implements OnRatingBarChangeListener{

    TextView ratingText;
    RatingBar rating;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.info);

        ratingText = (TextView) findViewById(R.id.textView_aaa);
        rating=(RatingBar)findViewById(R.id.ratingBar1);
        rating.setOnRatingBarChangeListener((OnRatingBarChangeListener) this);

    }

    @Override
    public void onRatingChanged(RatingBar ratingBar, float rating,
        boolean fromUser) {
        // TODO Auto-generated method stub
        ratingText.setText(""+this.rating.getRating());
    }

}

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

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