2013-06-28 93 views
0

我正在使用Android Studio創建一個小的拖放應用程序。我遵循了我所知的所有規則,代碼似乎沒有任何錯誤,但是當我在設備上運行它時,它簡單地崩潰。任何人都知道它錯在哪裏?拖放幫助 - Android

的代碼是罰款,直至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); 
} 

} 
} 
+0

如果您可以發佈LogCat會很有幫助。 – MaxAlexander

回答

1

的問題是線

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

這行告訴對象blueballdrag找到一個叫做imageView4的子視圖。我猜你的ImageViews沒有孩子。你想要你的Activity的findViewById()方法,而不是你的View。

將此行更改爲以下內容可以解決您的問題。

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

啊,它工作thx! – jtmwanakhu