2012-03-16 44 views
0

我有一个选项Activity,其中我使用RadioButton选择颜色。默认情况下,White颜色设置为android:checked="true"。现在,当我到达我的Canvas时,我需要动态更改Paint颜色,具体取决于哪个RadioButton被选中。 下面是我曾尝试代码:在Android中动态更改绘画对象的颜色

  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 

在类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); 

,但我得到每当ActivityCanvas启动一个NullPointerException。重要的是要注意,默认情况下,白色应该是颜色。另外,这不会永久保存它吗?我应该看看SharedPreferences吗?

+0

如果这不是你开始第二个活动的地方,那么你可以把无论你是否开始第二个活动的相关代码。 – Deva 2012-03-16 09:36:09

+0

我不从此活动开始其他活动。我感觉我在这种方法中完全错误,即选择putExtra()。 – 2012-03-16 09:38:36

回答

1

在阅读完您的评论之后,它看起来像是您没有使用给定的Intent启动其他活动,因此NullPointerException作为Bundle将不会在下一个活动中包含相同的字符串。

您可以选择退出以下选项:

1> Shared Prefrences (As highlighted by you) 
2> Some DB entry. 
3> Some file storage 
4> Singleton pattern 

,但对我最好的人会是一个共享prefrence。你也许还想看看这个link的详细信息