0

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

代码很好,直到initialise();被调用public void blue(View v)

所以我怀疑错误在那里

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);
}

}
}
4

1 回答 1

1

问题是线路

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

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

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

final ImageView imageView = (ImageView) findViewById(R.id.imageView4);
于 2013-06-28T18:24:39.223 回答