The method getResources() is undefined for the type

来源:互联网 发布:win to go mac 编辑:程序博客网 时间:2024/05/21 02:51

在非activity的独立类(public class drawThreeD implements Renderer)中调用getResources()时Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.dg);此为调用的语句会出现The method getResources() is undefined for the type

这是因为 getResources() 其实是contex中传递过去的,在非activity类中没有找到可以传递的cavas

解决:将activity类中contex传递过去就好了

1.在activity类中

  在创建非activity类时构造函数中添加参数:drawThreeD myRender = new drawThreeD(this.getApplicationContext());

2.在非activity类中 

  2.1 创建全局变量,用于保存传递过来的context

          private Context context;

  2.2构造函数中添加参数,并将传递过来的参数赋给全局变量

  public drawThreeD(Context context)
{
this.context=context;
}

   2.3调用

   Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.dg); //这里的context其实是全局变量

   

0 0
原创粉丝点击