2016-03-11 139 views
0

我想在图片上即可丢弃的形象和我在这ViewImageViewv.name键入铸造代码“回落”这种观点将显示在名为“DropTarget的”为背景,但不能happend其他ImageView,plzz告诉我一个办法。我的代码是在这里。:如何通过imageview将图像背景设置为imageView View?

case DragEvent.ACTION_DROP: 

      //handle the dragged view being dropped over a target view 

      View view = (View) event.getLocalState(); 

      //stop displaying the view where it was before it was dragged 

      view.setVisibility(View.INVISIBLE); 

      //view dragged item is being dropped on 

      ImageView dropTarget = (ImageView) v; 

      //view being dragged and dropped 

      ImageView dropped = (ImageView) view; 

      //it give me error in this line 

      **dropTarget.setBackgroundResource(dropped);** 
+0

什么是具体错误? –

+0

错误是:类型View中的方法setBackgroundResource(int)不适用于参数(ImageView) –

回答

0

你应该给我们更多关于代码的信息,但无论如何我会建议一些东西。

我想你是在一个空对象上调用setBackgroundResource。所以你的错误是在下一行

ImageView dropTarget = (ImageView) v; 

有可能没有一个叫“v”的视图。也许你应该改变它?

+0

sir在onDrag()中有一个视图参数,我在ImageView中种姓并且wana在其他ImageView上显示它。 –

+0

这里是onDragListener http://developer.android.com/reference/android/view/View.OnDragListener.html是的,你是对的有一个视图参数。 – Gerrerth

+0

sir这是错误:
类型View中的方法setBackgroundResource(int)不适用于参数(ImageView)。
所以PLZ告诉我,我怎么能typecaste查看对象,以便我在setBackgroundRecources参数中使用该视图对象。并且也认为不能种姓于Int。 –

0

我相信你正在寻找ImageView.setImageResource。你也可以设置backgroundImage,但最好的做法是使用setimageResource。

+0

先生我尝试每一个选项,但它给相同的错误。类型View中的setBackgroundResource(int)方法不适用于参数(ImageView) –