2012-03-16 22 views
0

私はRadioButtonを使用して色を選択するオプションActivityを持っています。既定では、の色はandroid:checked="true"に設定されています。今度私がCanvasに到達したら、RadioButtonが選択されたことに応じて、Paintの色を動的に変更する必要があります。 CanvasのためのクラスでPaintオブジェクトの色を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 

: は、ここで私が試してみましたコードだ

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

しかし、私はCanvasためActivityが開始されるたびにNullPointerExceptionを取得します。デフォルトでは、白​​が色でなければならないことに注意することが重要です。また、これはそれを永続的に格納しませんか?これについてSharedPreferencesを調べる必要がありますか?

+0

2つ目のアクティビティを開始する場所ではない場合は、2番目のアクティビティを開始する際に関連コードを入力することができます。 – Deva

+0

私はこのアクティビティから他のアクティビティを開始しません。私はこのアプローチでは全く間違っている、つまりputExtra()を選択していると感じています。 –

答えて

1

あなたのコメントを読んだ後、Bundleは次のアクティビティで同じ文字列を使用しないので、指定されたインテントとNullPointerExceptionを使用して他のアクティビティを開始していないように見えます。

次のオプションのためのオプトアウトすることができます

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

私にとって最良のものは、共有prefrenceされていると思います。詳細についてはlinkを参照してください。

関連する問題