我正在嘗試在 android studio/java 中聲明一個(gè)整數(shù)數(shù)組。當(dāng)我嘗試設(shè)置這個(gè)數(shù)組的值時(shí),android studio 無法識(shí)別它。我嘗試使用 String 而不是 int 和其他類型的數(shù)組,但我仍然得到相同的結(jié)果。int[] hello = new int[5];hello[0] = 1;第二條語(yǔ)句在 Android Studio 中帶有下劃線,將鼠標(biāo)懸停在它上面會(huì)顯示不同類型的錯(cuò)誤,這意味著它無法識(shí)別該語(yǔ)句。如果我將指針移到 hello 上,我會(huì)得到“未知類:你好”。如果我將它移到語(yǔ)句的其他部分,我會(huì)得到“預(yù)期的標(biāo)識(shí)符”和“意外的標(biāo)記”。編輯:我的原始代碼中有分號(hào),我只是沒有正確粘貼它。至于全班:public class MainActivity extends AppCompatActivity { int hello[] = new int[5]; hello[1] = 2; TextView texty; String s = String.valueOf(hello[1]); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d("mainy", s); } TextView t1 = findViewById(R.id.textView1); TextView t2 = findViewById(R.id.textView2); TextView t3 = findViewById(R.id.textView3); TextView t4 = findViewById(R.id.textView4); TextView t5 = findViewById(R.id.textView5); public void nextPage(View view) { Intent intent = new Intent(this, SecondActivity.class); startActivityForResult(intent, 1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data){ super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1){ if (resultCode == RESULT_OK){ String s = data.getStringExtra(SecondActivity.KEY); texty.setText(s); } } }}
1 回答

UYOU
TA貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個(gè)贊
這實(shí)際上是你缺乏的關(guān)于Java的基本知識(shí),這個(gè)問題不應(yīng)該出現(xiàn)在stackoverflow上。您不能在方法之外實(shí)現(xiàn)邏輯。您只能聲明變量。
你應(yīng)該做的是這樣的:
public class MainActivity extends AppCompatActivity {
int hello[];
TextView texty;
String s;
void method(){
hello = new int[5];
hello[1] = 2;
s = String.valueOf(hello[1])
}
}
添加回答
舉報(bào)
0/150
提交
取消