[英]How do I pass variables into a new android activity, with the ability to use them throughout the class?
我正在嘗試將變量傳遞到活動中,但是正在獲取空對象引用。
我的意圖聲明使用了putExtra()
public void launch_test(View view) {
playsound.start();
Intent launch_test = new Intent(this, test.class);
launch_test.putExtra("NUM_ROWS", 4);
launch_test.putExtra("NUM_COLS", 4);
startActivity(launch_test);
}
我的活動課(測試)要求額外的費用,
public class test extends Activity {
Intent launch_test = getIntent();
Bundle extras = launch_test.getExtras();
int NUM_ROWS = launch_test.getIntExtra("NUM_ROWS", 0);
int NUM_COLS = launch_test.getIntExtra("NUM_COLS", 0);
Button buttons[][] = new Button[NUM_ROWS][NUM_COLS];
etc...
但是當我嘗試在模擬器中運行它時,我得到的是空對象引用。
我假設可以使用IF語句檢查是否為空,但是Android Studio不允許我在代碼的這一部分使用if語句。 如果我將IF語句放入OnCreate函數中,那么我將無法按照我希望的方式在整個類中使用變量。
我是android編程的新手,如果感到困惑,對不起!
如果您只想使用您的代碼,請對其進行更正,請使用:extras.getIntExtra而不是launch_test.getIntExtra
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.