【问题标题】:drag and drop image on an image view在图像视图上拖放图像
【发布时间】: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


    【解决方案1】:

    getId是一个方法名,所以应该用()调用

    你应该这样做

    if(view.getId()==R.id.squareImage && v.getId()==R.id.squareImage1)
    

    如果有其他语法错误,请发布您的logcat

    【讨论】:

    • 嗨,非常感谢@gaurav5430,你的回答解决了我 90% 的问题,但我仍然收到语法错误,我用我的 logcat 更新了原始帖子,但再次非常感谢你
    • @martinseal1987 您是否在所有 getId() 中进行了这些更改?您现在可以发布更新的代码吗
    • @martinseal1987 将您的@drawable/dragsquare 更改为 R.drawable.dragsquare 等
    • 我将代码更改为您的建议,看来我仍然有错误,它现在说 setBackground(android.graphics.drawable.Drawable)in view cannot be applied to (int) 我更新了代码和 logcat为了反映上面的 android studio 提示我添加另一个返回值
    • 尝试将其更改为 setBackgroundResource,我看不到您更新的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多