I'm trying to pass a variable into an activity, but I am getting a null object reference.
My intent declaration makes use of 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);
}
and my activity class (test) calls for the extras,
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...
but I'm getting a null object reference when I try to run it in an emulator.
I assume that I could use an IF statement to check for nulls, but Android Studio isn't letting me use the if statement in this area of the code. If I place the IF statement into the OnCreate function, than I won't be able to use the variables throughout the class, the way I would like to.
I'm very new at android coding, so I'm sorry if there's confusion!
如果您只想使用您的代码,请对其进行更正,请使用:extras.getIntExtra而不是launch_test.getIntExtra
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.