2013-04-16 1 views
0

내 애플리케이션에 문제가 있습니다. 볼을 화면 위로 드래그하기 위해 만들어졌습니다. 내 문제는 (제 생각에는) 제 레이아웃의 디자인이었습니다 (어떻게 할 수 있을지 모르겠습니다).레이아웃 디자인으로 인해 레이아웃 버튼 및 토스트가 작동하지 않음 - 업데이트

 //Creates 3 layouts 
    LinearLayout view = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.main, null); 
    view.setClickable(true); 
    RelativeLayout layout = new RelativeLayout(this); 
    final DrawView custom = new DrawView(this); 
    //Creates the background and sets the background. 
    RelativeLayout background = new RelativeLayout(this); 
    Resources res = getResources(); 
    Drawable drawable = res.getDrawable(R.drawable.achtergrond); 
    background.setBackground(drawable); 
    layout.addView(background, new LinearLayout.LayoutParams(widthOfBackground, LayoutParams.WRAP_CONTENT)); 
    //Rules of the background, forced to the right 
    RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams)background.getLayoutParams(); 
    params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
    background.setLayoutParams(params1); 
    //Adding of the layouts to the main layout. 
    layout.addView(custom); 
    layout.addView(view); 
    //Shows the view to the user 
    setContentView(layout); 

내가 3 레이아웃을 만든 : :이처럼 내 MainActivity의 레이아웃이 있습니다. - 내 주요 XML 레이아웃 (보기) 한 - 다른 레이아웃에 배치되는 하나의 RelativeLayout의를 - 예를 들어 : 볼이 만들어지는 drawView입니다.

내 메인 레이아웃에 버튼을 만들 때 문제가 생깁니다. 버튼이 나타나지만 사용할 수 없습니다. MainActivity에서 onClick 메서드를 만든 경우에는 아무 것도 수행하지 않습니다. 또한 MainActivity에서 토스트 메시지를 만들려고해도 표시되지 않습니다.

내 레이아웃에 대한 도움말이나 정보를 알려 주시면 감사하겠습니다.

drawView (사용자 정의)에 대한 코드

은 이것이다 :

내가 좀 더 연구 한
package HVA.getConnected.toptopo; 

import java.util.ArrayList; 
import java.util.Timer; 
import java.util.TimerTask; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Point; 
import android.os.Handler; 
import android.os.Message; 
import android.os.SystemClock; 
import android.view.MotionEvent; 
import android.view.View; 

public class DrawView extends View { 
protected static final int REFRESH = 0; 
public ColorBall[] colorballs = new ColorBall[1]; // array that holds the balls 
private int balID = 0; // variable to know what ball is being dragged 

public DrawView(Context context) { 
    super(context); 
    setFocusable(true); //necessary for getting the touch events 
    // setting the start point for the balls 

    Point point1 = new Point(200,200); 
    Point point1a = new Point(755,502); 

    // declare each ball with the ColorBall class 
    colorballs[0] = new ColorBall(context,R.drawable.vlag_frankrijk, point1, R.drawable.vlag_frankrijk_vink, point1a); 

} 

// the method that draws the balls 
@Override 
protected void onDraw(Canvas canvas) { 
     //draw the balls on the canvas 
    for (ColorBall ball : colorballs) { 
     canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null); 
     } 

} 

// events when touching the screen 
public boolean onTouchEvent(MotionEvent event) { 
    int eventaction = event.getAction(); 

    int X = (int)event.getX(); 
    int Y = (int)event.getY(); 

    switch (eventaction) { 

    case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball 
     balID = 0; 
     System.out.println("debug 1"); 
     for (ColorBall ball : colorballs) { 
      // check if inside the bounds of the ball (circle) 
      // get the center for the ball 
      int centerX = ball.getX() + 25; 
      int centerY = ball.getY() + 25; 

      // calculate the radius from the touch to the center of the ball 
      double radCircle = Math.sqrt((double) (((centerX-X)*(centerX-X)) + (centerY-Y)*(centerY-Y))); 

      // if the radius is smaller then 23 (radius of a ball is 22), then it must be on the ball 
      if (radCircle > 0 && radCircle < 23){ 
       balID = ball.getID(); 
       break; 
      } 

      // check all the bounds of the ball (square) 
      if (X > ball.getX() && X < ball.getX()+50 && Y > ball.getY() && Y < ball.getY()+50){ 
       balID = ball.getID(); 
       break; 
      } 
      } 
     System.out.println("debug 2"); 
     break; 


    case MotionEvent.ACTION_MOVE: // touch drag with the ball 
     // move the balls the same as the finger 
     if (balID > 0) { 
      colorballs[balID-1].setX(X-25); 
      colorballs[balID-1].setY(Y-25); 
     } 

     break; 

    case MotionEvent.ACTION_UP: 
     // touch drop - just do things here after dropping 
     if (balID > 0){ 
      ColorBall a = colorballs[balID-1]; 
      if(a.getX() > a.getDestX()-100 && a.getX() < a.getDestX()+100 && 
        a.getY() > a.getDestY()-100 && a.getY() < a.getDestY()){ 
       System.out.println("___Drag X & Y is good"); 
       colorballs[balID-1].setCanMove(false); 
      } 
     } 
     break; 
    } 
    // redraw the canvas 
    invalidate(); 
    return true; 

} 
} 

, 토스트 메시지가 MainActivity 클래스에서 작동 않지만, 버튼하지 않습니다. 이제 프로그램을 실행할 수 없습니다. 나는이 추가 :

 b1 = (Button) findViewById(R.id.btn1); 
    b1.setOnClickListener(this); 

내가 오류가 나는 OnClickListener를 함께 OnClickListener를 캐스팅 할 필요가 있다는 것입니다. 하지만 그렇게하면 더 이상 움직일 수 없습니다. ClassCastException 오류로 인해 충돌이 발생하며이 문제를 해결하는 방법을 모르겠습니다.

온 클릭 방법 :

@Override 
public void onClick(View v) { 
    // Perform action on click 
    switch(v.getId()) { 
    case R.id.btn1: 
     System.out.println("....."); 
     Toast.makeText(this, "hoi?", Toast.LENGTH_LONG).show(); 
     break; 
    } 

}} 

그리고 토스트는 다음과 같습니다 레이아웃 설정이 onclickListeners 사용하여 응용 프로그램을 충돌 한이

 Button btn = (Button) findViewById(R.id.btn1); 
       btn.setOnClickListener(new OnClickListener() { 
         public void onClick(View v) { 
       showToastMessage(R.id.btn1); 

         } 
         });} 


       //showToastMessage(int id) method definition here 

       public void showToastMessage(int id) 
      {  
        switch(id) { 
       case id: 
       System.out.println("....."); 
      Toast.makeText(this, "hoi?", Toast.LENGTH_LONG).show(); 
       break; 
       } 

       } 
+0

버튼 클릭 방법을 표시하고 사용중인 토스트를 표시 할 수 있습니까? –

+0

Gunjan Verma, 나는 내 게시물을 편집했고 지금 거기에 있습니다. – Marc

+0

나는 어떤 캐스트도 보지 못했다. 당신은 "내가 얻은 에러는 OnClickListener로 onclicklistener를 형 변환 할 필요가 있다는 것"을 의미하는지 설명하고 보여줄 수 있습니까? – quinestor

답변

0

사용, 지금이 내 모든 문제를 해결 한 XML 파일의 View 클래스.

0

같은

Toast.makeText(this, "text", Toast.LENGTH_LONG).show();