【问题标题】:Getting a 'Cannot resolve method' error收到“无法解析方法”错误
【发布时间】:2015-07-15 01:38:54
【问题描述】:

我在下面的代码中遇到错误。错误是'无法解析方法'getApplication()'' 有什么帮助吗?我认为这是因为我拉动全局变量的方式。也许是设置变量的更好方法。

public class game_select extends Activity {



  protected void onCreate(Bundle icicle) {


    super.onCreate(icicle);

    setContentView(R.layout.game_select);

    final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {


      public void onClick(View v) {

        // Perform action on click
        Toast.makeText(getApplicationContext(),
          "Your Message", Toast.LENGTH_LONG).show();
        ((Global_variables) this.getApplication()).setGameselected("http://vector-runner-remix.tresensa.com/index.html?dst=B0000");
      }


    });
  }

}

【问题讨论】:

    标签: javascript java android variables


    【解决方案1】:

    您的“this”指的是 onClickListener

    这样做:

    ((Global_variables) game_select.this.getApplication()).setGameselected("http://vector-runner-remix.tresensa.com/index.html?dst=B0000");
    

    这次的“this”指的是你的activity类

    【讨论】:

    • 好的,现在我可以运行我的游戏了,但是当我点击按钮时它崩溃了。
    • 嗨,那已经是另一个问题了:),我只回答了你当前未解决的方法问题。您可能会为崩溃提出一个新问题。哎哟
    • 嗯。对于您的下一个问题,如果您发现该问题的答案令人满意,您应该给他们应得的:)。谢谢
    • 如果您的意思是接受答案,我已经这样做了。虽然我是这个论坛的新手,但很抱歉我忘记了一些事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 2019-12-26
    • 1970-01-01
    相关资源
    最近更新 更多