尽管我没有使用静态方法,但仍得到警告

时间:2018-11-22 13:02:36

标签: android android-studio

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

我尝试共享代码,但是由于时间太长,我无法共享。

问题在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 类获取信息。

我希望有足够的细节来解释问题。

1 个答案:

答案 0 :(得分:1)

问题是,您像静态方法一样调用Cards.firstStart()。您应该使用Cards cards = new Cards()

初始化Cardobject