【发布时间】:2013-12-24 23:01:25
【问题描述】:
嗨,我今天问了这个问题how to drag and drop a image onto a image,有人很高兴地给了我一个看起来应该可行的答案,但是将它放入我的代码中并取出我被告知的内容,仍然给我错误
无法解析符号getId,还告诉我有语法错误
谁能帮忙(也许请解释一下)
我的更新代码如下,永远感谢所有帮助
.Java (UPDTAED)
import android.os.Bundle;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.app.Activity;
public class DragandDrop extends Activity implements OnTouchListener, OnDragListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.draganddrop);
findViewById(R.id.squareImage).setOnTouchListener(this);
findViewById(R.id.circleImage).setOnTouchListener(this);
findViewById(R.id.squareImage1).setOnDragListener(this);
findViewById(R.id.circleImage1).setOnDragListener(this);
findViewById(R.id.triangleImage1).setOnDragListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(null, shadowBuilder, v, 0);
v.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
@Override
public boolean onDrag(View v, DragEvent e) {
if (e.getAction()==DragEvent.ACTION_DROP) {
View view = (View) e.getLocalState();
if(view.getId()==R.id.squareImage && v.getId()==R.id.squareImage1)
{
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
v.setBackground(R.drawable.dragsquare);
return true;
} else if(view.getId()==R.id.circleImage1 && v.getId()==R.id.circleImage){
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
v.setBackground(R.drawable.dragcircle);
return true;
} else if(view.getId()==R.id.triangleImage1 && v.getId()==R.id.triangleImage){
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
v.setBackground(R.drawable.dragtriangle);
return true;
}
else {
return false;
}
}
return true;
}
}
这是我更新的 logcat
Information:3 errors
Information:Compilation completed with 13 errors and 0 warnings in 3 min 40 sec
Information:13 errors
Information:0 warnings
Error:Gradle: Execution failed for task ':SocialStories2:compileDebug'.
> Compilation failed; see the compiler error output for details.
C:\Users\dawne allen\AndroidStudioProjects\SocialStories2Project\
SocialStories2\src\main\java\com\martinsapp\socialstories\DragandDrop.java
Error:Error:line (46)Gradle: error: method setBackground in
class View cannot be
applied to given types;
Error:Error:line (46)Gradle: error: method setBackground in
class View cannot be applied to given types;
required: Drawable
Error:Error:line (46)Gradle: error: method setBackground in
class View cannot be applied to given types;
required: Drawable
found: int
Error:Error:line (46)Gradle: error: method
setBackground in class View cannot be applied to given types;
required: Drawable
found: int
reason: actual argument int cannot be converted to Drawable by
method invocation conversion
Error:Error:line (51)Gradle: error: method setBackground in
class View cannot be applied to given types;
Error:Error:line (51)Gradle: error: method setBackground in
class View cannot be applied to given types;
required: Drawable
Error:Error:line (51)Gradle: error: method setBackground in
class View cannot be applied to given types;
required: Drawable
found: int
Error:Error:line (51)Gradle: error: method setBackground in
class View cannot be
applied to given types;
required: Drawable
found: int
reason: actual argument int cannot be converted to Drawable by
method invocation conversion
Error:Error:line (56)Gradle: error: method setBackground in
class View cannot be applied to given types;
Error:Error:line (56)Gradle: error: method setBackground in
class View cannot be applied to given types;
required: Drawable
Error:Error:line (56)Gradle: error: method setBackground in
class View cannot be applied to given types;
required: Drawable
found: int
Error:Error:line (56)Gradle: error: method setBackground
in class View cannot be applied to given types;
required: Drawable
found: int
reason: actual argument int cannot be converted to Drawable
by method invocation conversion
【问题讨论】:
标签: android drag-and-drop imageview android-imageview