2014-02-22 36 views
0

我正在创建使用按钮共享图像的图像共享应用程序。我在下面的代码得到一个unable to resolve the required method error如何调用getDrawable方法

public class ShareActivity extends Activity implements View.OnClickListener { 

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

     button1 = (Button)findViewById(R.id.button1); 
     button1.setOnClickListener(this); 
    } 
    private void button1Click() 
    { 
     BitmapDrawable bm = (BitmapDrawable) getDrawable(button1); 
     Bitmap mysharebmp = bm.getBitmap(); 
     String path = Images.Media.insertImage(getContentResolver(), 
          mysharebmp, "MyImage", null); 

在此行中:

BitmapDrawable bm = (BitmapDrawable) getDrawable(button1);ShareActivity 

我得到The method getDrawable(Button) is undefined for the type ShareActivity。为什么我会得到这个错误?完整的代码是here

+4

你在做什么。 'getDrawable(button1);'你看过api的文档吗?你的问题不清楚。 – Raghunandan

+0

我想要一个按钮,可以分享我的图片社交应用程序,这就是我想要做的先生 –

+0

你的图像在哪里? –

回答

-3

getDrawable()方法需要资源ID为,在这种情况下,您传递的是Button。尝试通过R.id.button1 这里是documentation

+0

它仍然要求为'getDrawable();' –

+0

创建方法发布整个错误 - 无论它可能是什么...... 我假设你实现了onClick(View v)View.OnClickListener()的方法 –

+0

-1,因为您无法在'getDrawable'中传递按钮ID –

2

这是为你想在这里做什么非常清楚,我怀疑你会遇到一些其他问题与此代码。但是,您收到错误“getDrawable未定义为ShareActivity”的原因是因为Activity类没有getDrawable方法。

而不是调用getDrawable,您需要获取应用程序资源,然后检索drawable。什么你要找的是:

BitmapDrawable bm = (BitmapDrawable) getResources().getDrawable(R.drawable.my_bitmap); 

如果你有一些图像中/res/drawable/称为“my_bitmap.png”。

编辑Feb 7 2016:这个答案不再正确。 Context#getDrawable(int)被添加到2014年11月发布的API 21(Lollipop)中。 Note that you probably shouldn't be using it anyway

0

如果您试图使图像显示在按钮内部,请考虑使用图像按钮对象内定义的函数。 我的意思是说使用button1.getDrawable()