R.drawable.RED 显示错误 RED cannot be resolved or is not a field

部分代码如下:
import android.os.Bundle;
import android.widget.*;
import android.app.Activity;
import android.view.Menu;
import android.view.KeyEvent;
import android.view.View;
import android.graphics.drawable.*;
import android.graphics.drawable.Drawable;
import android.content.res.Resources;
import android.graphics.Color;
public class MainActivity extends Activity {
TextView textView;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1 =(Button)findViewById(R.id.Button1);
final Button button2 =(Button)findViewById(R.id.Button2);
final Button button3 =(Button)findViewById(R.id.Button3);
textView =(TextView)findViewById(R.id.myTextView);
Resources resource=this.getBaseContext().getResources();
final Drawable red_Drawable=resource.getDrawable(R.drawable.RED);
final Drawable blue_Drawable=resource.getDrawable(R.drawable.BLUE);
final Drawable yellow_Drawable=resource.getDrawable(R.drawable.YELLOW);

第1个回答  2014-03-28
你这代码好熟悉啊,是不是看的点子工业出版社的那个安卓从入门到精通,嘿嘿。我也在看了。
遇到你这情况的话,我的解决办法是先把已经写了的代码复制出来,然后重新新建项目就行了。
顺便问下,安卓是怎么调试的?比如我想打印出red_Drawable变量怎么弄哦?像php可以直接die(变量)
第2个回答  2017-04-25
在colors.xml里面添加这三种颜色
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<drawable name="RED">#FF0000</drawable>
<drawable name="BLUE">#0000FF</drawable>
<drawable name="YELLOW">#FFFF00</drawable>
</resources>本回答被网友采纳
第3个回答  2013-08-05
clean 一下试试,一般是因为修改了资源,未更新
相似回答