android - 即使我没有使用静态我得到以下警告
问题描述
即使我没有在 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类中获取了警告行。
我希望细节足以解释问题..
解决方案
Prolem 是,你调用 Cards.firstStart() 就像一个静态方法。您应该使用初始化 CardobjectCards cards = new Cards()
推荐阅读
- c - c lang 中的 if 语句有问题
- vim - 如何使用 Ultisnip 获取降价片段以在 vim 中的数学中工作?
- python - AttributeError:尝试在线程中运行 TensorFlow 模型推理时,“密集”对象没有属性“内核”
- angular - 属性 'staticAlert' 没有初始化程序,也没有在 constructor.ts 中明确分配
- python-3.x - 需要 Selenium Webdriver Python 代码解释
- neo4j - 密码查询:返回某些用户评分的食谱,包含特定成分,按结果中重复的食谱数量排序
- sql - for循环中的状态栏
- javascript - Next.js 按需静态再生
- python - 如何在 python 中制作 2 个字典,在 .txt 文件中用空格分隔
- android - 为什么我的lazyColumn 仅在我转到另一个片段并返回 Android 时才加载?