我得到一个"无法设置静态引用错误"如何将视图设置为xml文件

时间:2015-04-01 20:54:17

标签: java android xml

所以我试着去一个名为&#34的XML文件;你失败了#34;由于某种原因,我收到一个错误,说它无法获得静态引用。有没有办法做我想做的事情?

public class DrawingView extends View {



    public DrawingView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub

    }
    RectF rectf = new RectF(0, 0, 200, 0);

    private static final int w = 100;
    public static int lastColor = Color.BLACK;
    private final Random random = new Random();
    private final Paint paint = new Paint();
    private final int radius = 230;
    private final Handler handler = new Handler();
    public static int redColor = Color.RED;
    public static int greenColor = Color.GREEN;
    int randomWidth = 0;
    int randomHeight = 0;
    public static int addPoints = 0;



    private final Runnable updateCircle = new Runnable() {
        @Override 
        public void run() {
            lastColor = random.nextInt(2) == 1 ? redColor : greenColor;
            paint.setColor(lastColor);
            invalidate();
            handler.postDelayed(this, 1000);

        }
    };



    @Override 
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        handler.post(updateCircle);
    }

    @Override 
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        handler.removeCallbacks(updateCircle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // your other stuff here
        if(random == null){
            randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
            randomHeight = (random.nextInt((int)Math.abs((getHeight()-radius/2 + radius/2f))));
        }else {
            randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
            randomHeight = (random.nextInt((int)Math.abs((getHeight()-radius/2 + radius/2f))));
        }

        canvas.drawCircle(randomWidth, randomHeight + radius/2f, radius, paint);
    }

    public boolean onTouch(View v, MotionEvent event) {
   int x = (int) event.getX();
   int y = (int) event.getY();
   if(isInsideCircle(x, y) ==  true){
      //Do your things here
       if(redColor == lastColor){
           Activity.setContentView(R.layout.youfailed);
       } else {
           addPoints++;
       }
   }else {

   }
   return true;
}

private boolean isInsideCircle(int x, int y){
  if ((((x - randomWidth)*(x - randomWidth)) + ((y - randomHeight)*(y - randomHeight))) < ((radius)*(radius)))
    return true;
  return false;    
}




}

此方法中出现错误

public boolean onTouch(View v, MotionEvent event) {
       int x = (int) event.getX();
       int y = (int) event.getY();
       if(isInsideCircle(x, y) ==  true){
          //Do your things here
           if(redColor == lastColor){
               //it shows up right below here
               Activity.setContentView(R.layout.youfailed);
           } else {
               addPoints++;
           }
       }else {

       }
       return true;
    }

1 个答案:

答案 0 :(得分:1)

你的问题就在这一行:

Activity.setContentView(R.layout.youfailed);
它不会这样工作。 您需要实际活动来设置内容视图。 setContentView不是静态方法。

更新:

您似乎尝试从其中一个Child-View对象中更改Activity的内容,这是非常糟糕的样式,不应该这样做。

视图永远不应该控制它的父实际显示的内容。 但是,回调父母可以解决您的问题。

回调只是一个带有一个或多个方法的接口,在你的情况下是一个:&#34; onFailure()&#34;,它将由例如活动。这&#34; onFailure()&#34;在你的onTouch()而不是setContentView()中调用,并且在你的实际Activity实现中(它必须像onClickListeners或onTouchListeners那样将自己注册为回调接收器),你可以做任何你喜欢的事情

回调接口示例:

public interface FailureCallback {
  public void onFailure();
}

您可以将它作为内部接口放入DrawingView类中。在DrawingView类中,您需要一个用于实际回调实例的setter,然后您可以在onTouch()方法中调用。

在你的回调实现中,你不应该改变正在运行的活动的内容,而是考虑使用startActivity(...)来调用一个新的Activity。