Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试使用拖放方式运行Android应用程序时出现意外错误_Java_Android_Drag And Drop - Fatal编程技术网

Java 尝试使用拖放方式运行Android应用程序时出现意外错误

Java 尝试使用拖放方式运行Android应用程序时出现意外错误,java,android,drag-and-drop,Java,Android,Drag And Drop,我正在尝试制作一个非常简单的Android应用程序(使用Eclipse和Android 4.0.2,API 15)来实现拖放。它有一个需要拖放到不同ImageView上的ImageView。但我似乎有点问题。该应用程序编译正确,但当我在模拟器和真实设备上运行它时,它会被强制关闭。 我的代码有3个类:一个用于(仅)活动,一个用于“可拖动”图像侦听器,另一个用于“目标”图像侦听器: public final class ChoiceTouchListener implements OnTouchLi

我正在尝试制作一个非常简单的Android应用程序(使用Eclipse和Android 4.0.2,API 15)来实现拖放。它有一个需要拖放到不同ImageView上的ImageView。但我似乎有点问题。该应用程序编译正确,但当我在模拟器和真实设备上运行它时,它会被强制关闭。 我的代码有3个类:一个用于(仅)活动,一个用于“可拖动”图像侦听器,另一个用于“目标”图像侦听器:

public final class ChoiceTouchListener implements OnTouchListener {

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            //start dragging the item touched
            view.startDrag(data, shadowBuilder, view, 0);
            return true;
        }
        else {
            return false;
        }
    }
}
public class ChoiceDragListener implements OnDragListener {

    @Override
    public boolean onDrag(View v, DragEvent event) {
        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            //no action necessary
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            //no action necessary
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            //no action necessary
            break;
        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;
            //Dim the target image when the other ImageView is dropped on it
            dropTarget.setAlpha(100);
            break;
        case DragEvent.ACTION_DRAG_ENDED:
            //no action necessary
            break;
        default:
            break;
        }
        return true;
    }
}
活动:

public class MainActivity extends Activity {

    ImageView imageToBeDragged = (ImageView)findViewById(R.id.imagetodrag);
    ImageView targetImage = (ImageView)findViewById(R.id.target);

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

        imageToBeDragged.setOnTouchListener(new ChoiceTouchListener());
        targetImage.setOnDragListener(new ChoiceDragListener());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
可拖动侦听器:

public final class ChoiceTouchListener implements OnTouchListener {

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            //start dragging the item touched
            view.startDrag(data, shadowBuilder, view, 0);
            return true;
        }
        else {
            return false;
        }
    }
}
public class ChoiceDragListener implements OnDragListener {

    @Override
    public boolean onDrag(View v, DragEvent event) {
        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            //no action necessary
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            //no action necessary
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            //no action necessary
            break;
        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;
            //Dim the target image when the other ImageView is dropped on it
            dropTarget.setAlpha(100);
            break;
        case DragEvent.ACTION_DRAG_ENDED:
            //no action necessary
            break;
        default:
            break;
        }
        return true;
    }
}
目标侦听器:

public final class ChoiceTouchListener implements OnTouchListener {

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            //start dragging the item touched
            view.startDrag(data, shadowBuilder, view, 0);
            return true;
        }
        else {
            return false;
        }
    }
}
public class ChoiceDragListener implements OnDragListener {

    @Override
    public boolean onDrag(View v, DragEvent event) {
        switch (event.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            //no action necessary
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            //no action necessary
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            //no action necessary
            break;
        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;
            //Dim the target image when the other ImageView is dropped on it
            dropTarget.setAlpha(100);
            break;
        case DragEvent.ACTION_DRAG_ENDED:
            //no action necessary
            break;
        default:
            break;
        }
        return true;
    }
}
什么可能导致错误


谢谢

在黑暗中拍摄时没有看到错误,但您的活动看起来是错误的。在设置内容视图之前,无法通过其id找到视图

public class MainActivity extends Activity {

    ImageView imageToBeDragged;
    ImageView targetImage;

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

        imageToBeDragged = (ImageView)findViewById(R.id.imagetodrag);
        targetImage = (ImageView)findViewById(R.id.target);
        imageToBeDragged.setOnTouchListener(new ChoiceTouchListener());
        targetImage.setOnDragListener(new ChoiceDragListener());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

请张贴错误。太好了!成功了!我猜是初学者的错误。非常感谢!