首页 > 解决方案 > 即使我没有使用静态我得到以下警告

问题描述

即使我没有在 Android Studio 中使用静态表达式,我也会收到“无法从静态上下文引用非静态变量”警告。我不明白为什么。能否请你帮忙?

我试过分享代码,但因为太长了,我不能。

问题在 onClick 方法之后开始。

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    // gameStart = (Button) findViewById(R.id.start);
    newRoundButton = (Button) findViewById(R.id.newRound);
    hitButton = (Button) findViewById(R.id.hit);
    newGameButton = (Button) findViewById(R.id.newGame);
    passButton = (Button) findViewById(R.id.pass);

    playerC1 = (ImageView) findViewById(R.id.playerC1);


    newRoundButton.setOnClickListener(this);
    hitButton.setOnClickListener(this);
    newGameButton.setOnClickListener(this);
    passButton.setOnClickListener(this);

    //hitButton.setVisibility(View.INVISIBLE);
    //newGameButton.setVisibility(View.INVISIBLE);
    // passButton.setVisibility(View.INVISIBLE);

    dealer = (TextView) findViewById(R.id.dealer);
    player = (TextView) findViewById(R.id.player);
    playerTotal = (TextView) findViewById(R.id.playerTotal);
    dealerDeckTable = (TextView) findViewById(R.id.dealerDeckTable);
    playerDeckTable = (TextView) findViewById(R.id.playerDeckTable);
    skor = (TextView) findViewById(R.id.score);
    gameMessage = (TextView) findViewById(R.id.gameMessage);
    dealerTotal = (TextView) findViewById(R.id.dealerTotal);
    playerSkor = (TextView) findViewById(R.id.playerSkor);
    dealerSkor = (TextView) findViewById(R.id.dealerSkor);
    kontrol = (TextView) findViewById(R.id.kontrol);
    playerDeckTable = (TextView) findViewById(R.id.playerDeckTable);
    dealerDeckTable = (TextView) findViewById(R.id.dealerDeckTable);

    playerC1.setImageDrawable((ContextCompat.getDrawable(getApplicationContext(),R.drawable.nocard)));

}
 public void onClick(View v) {
   if (v.getId() == newGameButton.getId()) {
        gameMessage.setText(Cards.firstStart());
        playerSkor.setText(Cards.pskorS);
        dealerSkor.setText(Cards.dskorS);
        playerTotal.setText(Cards.pValS);
        dealerTotal.setText("N/A");
        playerDeckTable.setText(Cards.pHand);
        dealerDeckTable.setText(Cards.dHand2);
        newGameButton.setVisibility(View.INVISIBLE);
        kontrol.setText(Cards.kontrolS);

代码继续...

Cards类不包含任何静态变量,并且我从Cards类中获取了警告行。

我希望细节足以解释问题..

标签: androidandroid-studio

解决方案


Prolem 是,你调用 Cards.firstStart() 就像一个静态方法。您应该使用初始化 CardobjectCards cards = new Cards()


推荐阅读