【问题标题】:Drag and Drop help - Android拖放帮助 - Android
【发布时间】:2013-06-28 10:17:22
【问题描述】:

我正在使用 Android Studio 创建一个小型拖放应用程序。我遵循了所有我知道的规则,并且代码似乎没有任何错误,但是当我在我的设备上运行它时,它简单地崩溃了。有谁知道错在哪里?

public void blue(View v) 中调用initialise(); 之前,代码是可以的

所以我怀疑是否存在错误

public class MainActivity extends Activity {
private ImageView blueball;
private ImageView blueballdrag;

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


@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 void colourGen(View view){
    int i =1;
    if (i==i){
        blue(view);
    }
}

public void brown(View v){
    setContentView(R.layout.activity_brown);
}
public void yellow (View v){
    setContentView(R.layout.activity_yellow);
}
public void green (View v){
    setContentView(R.layout.activity_green);
}
public void blue (View v){
    setContentView(R.layout.activity_blue);
    initialise();
}



@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void initialise() {
    final ImageView imageView = (ImageView) blueballdrag.findViewById(R.id.imageView4);
    imageView.setOnDragListener(new View.OnDragListener() {
        public boolean onDrag(View v, DragEvent dragEvent) {
            switch (dragEvent.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:
                    v.setBackgroundColor(Color.RED);
                case DragEvent.ACTION_DRAG_ENTERED:
                   v.setBackgroundColor(Color.BLACK);
                case DragEvent.ACTION_DRAG_ENDED:
                    v.setBackgroundColor(Color.GREEN);
                case DragEvent.ACTION_DROP:
                    v.setBackgroundColor(Color.WHITE);
            }
            return false;
        }
    });
    blueball = (ImageView) findViewById(R.id.imageView6);
    blueball.setOnLongClickListener(new OnLongClickListener(){
        @Override
        public boolean onLongClick(View v) {
            View.DragShadowBuilder myShadow = new MyDragShadowBuilder(blueball);
            v.startDrag(null, myShadow, null, 0);
            return false;
        }
    });
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private static class MyDragShadowBuilder extends View.DragShadowBuilder {
    private static Drawable shadow;
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)

    public MyDragShadowBuilder(View v) {
        super(v);
        shadow = new ColorDrawable(Color.RED);
}
    public void onProvideShadowMetrics(Point size, Point touch){
        int width, height;
        width = getView().getWidth() * 2;
        height = getView().getHeight() * 2;
        shadow.setBounds(0, 0, width, height);
        size.set(width, height);
        touch.set(width*2, height*2);
}
    public void onDrawShadow(Canvas canvas){
        shadow.draw(canvas);
}

}
}

【问题讨论】:

  • 如果您能发布 LogCat 将会很有帮助。

标签: android android-studio


【解决方案1】:

问题是线

final ImageView imageView = (ImageView) blueballdrag.findViewById(R.id.imageView4);

这一行告诉对象blueballdrag 找到一个名为imageView4 的子视图。我猜你的 ImageViews 没有孩子。您需要 Activity 的 findViewById() 方法,而不是 View。

将行更改为以下内容应该可以解决您的问题。

final ImageView imageView = (ImageView) findViewById(R.id.imageView4);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多