我正在嘗試在 android studio/java 中聲明一個整數數組。當我嘗試設置這個數組的值時,android studio 無法識別它。我嘗試使用 String 而不是 int 和其他類型的數組,但我仍然得到相同的結果。int[] hello = new int[5];hello[0] = 1;第二條語句在 Android Studio 中帶有下劃線,將鼠標懸停在它上面會顯示不同類型的錯誤,這意味著它無法識別該語句。如果我將指針移到 hello 上,我會得到“未知類:你好”。如果我將它移到語句的其他部分,我會得到“預期的標識符”和“意外的標記”。編輯:我的原始代碼中有分號,我只是沒有正確粘貼它。至于全班: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貢獻1878條經驗 獲得超4個贊
這實際上是你缺乏的關于Java的基本知識,這個問題不應該出現在stackoverflow上。您不能在方法之外實現邏輯。您只能聲明變量。
你應該做的是這樣的:
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])
}
}
添加回答
舉報
0/150
提交
取消