Reputation: 633
I'm using Android Studio to create a small drag and drop application. I have followed all the rules I know and the code doesn't seem to have any errors, however when I run it on my device it simple crashes. Anyone know where its wrong?
The code is fine until initialise();
is called in public void blue(View v)
so I'm suspecting the error is there
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);
}
}
}
Upvotes: 0
Views: 152
Reputation: 67189
The problem is the line
final ImageView imageView = (ImageView) blueballdrag.findViewById(R.id.imageView4);
This line tells the Object blueballdrag
to find a child view called imageView4
. I'm guessing that your ImageViews don't have children. You want the findViewById()
method of your Activity, not your View.
Changing the line to the following should solve your problem.
final ImageView imageView = (ImageView) findViewById(R.id.imageView4);
Upvotes: 1