Elad Naeh Elad Naeh - 4 months ago 6
Android Question

The value of "key" is null

I have this problem:

I want to use variables in different activities, but my key value is null.

I have this written on the main activity:

Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("score",score);
startActivity(intent);
}


And I have this written in my second activity:

Bundle extras = getIntent().getExtras();
score10 = extras.getString("score");
score20 = Integer.parseInt(score10);
score30 = score20;


What should I do?

Answer

MainActivity.Java

    Intent intent = new Intent(MainActivity.this, Main2Activity.class);
    if(score != null ){
        intent.putExtra("score", String.valueOf(score));
    }
    else {
        String test = "";
        intent.putExtra("score", test);
    }
    startActivity(intent);
}

MainActivity2.java

Bundle extras = getIntent().getExtras();
score10 = extras.getString("score");
if(score10 != null || score10 != ""){
    score20 = Integer.parseInt(score10);
    score30 = score20;
}

That's done.