【问题标题】:Cannot make a static reference error无法产生静态引用错误
【发布时间】:2015-04-20 04:39:12
【问题描述】:

所以,如果有人知道为什么这不起作用,我会发布给出错误的代码,让我知道,并给出解释。 (不要只是说这是一种不好的编码方式或其他东西)(或者至少如果你自己解释的话)。所以下面的代码应该是当某人点击的颜色为真时切换到另一个屏幕!

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){
// error is here   Intent i = new Intent(this, YouFailed.class);
// and here        Activity.startActivity(i);
       } else {
           addPoints++;
       }
   }else {

   }
   return true;
}

有两个错误:

构造函数Intent(DrawingView, Class<YouFailed>)未定义

无法从 Activity 类型对非静态方法 startActivity(Intent) 进行静态引用

【问题讨论】:

    标签: java android ontouchlistener touch-event ontouch


    【解决方案1】:

    使用v 访问startActivity 方法,而不是尝试以static 方式调用non-static 方法:

    Intent i = new Intent(v.getContext(), YouFailed.class);
    v.getContext().startActivity(i);
    

    【讨论】:

    • 我如何找到更多关于 v 或其方法 getContext(); ?而不是来自 java api 我在那里找不到它
    • @devin: vView 类的对象,getContext() 方法在View 类中定义,它返回Context,您可以通过它访问startActivity 方法。在这里找到更多细节View.getContext ()
    • 是的,我在输入后意识到了这一点。它实际上是在该方法中传递的参数,谢谢
    【解决方案2】:

    好像是这里出错了,

       Intent i = new Intent(this, YouFailed.class);
    

    因为在Intent构造函数中你的第一个参数是DrawingView,而不是它应该是

    Intent 操作,例如 ACTION_VIEW。

    所以改成

       Intent i = new Intent(v.getContext(), YouFailed.class);
    

    【讨论】:

    【解决方案3】:

    尝试使用您的活动上下文而不是内联类上下文:

    Intent i = new Intent(YourActivity.this, YouFailed.class);
    YourActivity.startActivity(i);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-31
      • 1970-01-01
      • 2019-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-23
      相关资源
      最近更新 更多