I have a Options Activity, where I select the color using a RadioButton. By default, the White color is set android:checked="true". Now when I reach to my Canvas,I need to change the Paint color dynamically depending on which RadioButton was selected.
Here’s the code that I have tried:
String radioButtonSelected = "";
switch (checkedRadioButton) {
case R.id.CheckRed : radioButtonSelected = "Red";
break;
case R.id.CheckBlue : radioButtonSelected = "Blue";
break;
case R.id.CheckWhite : radioButtonSelected = "White";
break;
}
Intent i = new Intent(HandwritingRecognitionOptionTab.this,HandwritingRecognitionCanvas.class);
i.putExtra("setColor",radioButtonSelected);
//startActivity(i); // because I don't want to start the activity immediately after this
In the class for Canvas:
Bundle getValue = getIntent().getExtras();
drawColor = getValue.getString("setColor");
if(drawColor.equals("White"))
intColor = 1;
if(drawColor.equals("Red"))
intColor = 2;
if(drawColor.equals("Blue"))
intColor = 3;
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.WHITE);
if(intColor == 1)
mPaint.setColor(Color.WHITE);
if(intColor == 2)
mPaint.setColor(Color.RED);
if(intColor == 3)
mPaint.setColor(Color.BLUE);
But I get a NullPointerException whenever the Activity for Canvas is started. It is important to note that by default White should be the color. Also, this doesn’t store it persistently right? Should I look into SharedPreferences for this ?
After reading through your comments it looks like that you are not starting the other activity using the given Intent and hence the NullPointerException as the Bundle wont be containg the same string in the next activity.
You can opt out for the following options:
but for me the best one would have been a shared prefrence. You might also want to have a look at this link for a detail