title: How to get the RGB values from am imageview in android
body: <p><strong>What i am trying to do:</strong> I am trying to get the RGB values from an image</p>
  
  <hr>
  
  <p><strong>What is happening:</strong> I am getting the null pointer exception in </p>
  
  <pre><code>final Bitmap bitmap = ((BitmapDrawable)imgUsrClrId.getDrawable()).getBitmap();
  </code></pre>
  
  <hr>
  
  <p><strong>Question:</strong> This is happening in <code>fragments</code> as seen <a href="https://stackoverflow.com/a/14920800/1083093">here</a> it is working for activity, i guess i am not able to get the <code>imageview</code> properly for <code>fragments</code>. <strong><em>HOW CAN I RESOLVE THIS !!</em></strong></p>
  
  <hr>
  
  <p><strong>MyClass.java</strong></p>
  
  <pre><code>public class MyClass extends Fragment {
  
  
      //View object variables
      TextView tvAllSymptomsId,tvGenderId,txtColorCodeId;
      Button btnRotateId;
      ImageView imgUsrClrId;
  
  
      public MyClass() {
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
  
          View rootView = inflater.inflate(R.layout.symptom_checker, container,false);
  
          //Find the views
          tvAllSymptomsId=(TextView) rootView.findViewById(R.id.tvAllSymptomsId);
          tvGenderId=(TextView) rootView.findViewById(R.id.tvGenderId);
          btnRotateId=(Button) rootView.findViewById(R.id.btnRotateId);
          imgUsrClrId=(ImageView) rootView.findViewById(R.id.imgUsrClrId);
          txtColorCodeId=(TextView) rootView.findViewById(R.id.txtColorCodeId);
  
          return rootView;
      }
  
      @Override
      public void onStart() {
          super.onStart();
  
          //Initially set the image as female facing front
          imgUsrClrId.setBackgroundDrawable(getResources().getDrawable(R.drawable.female_front));
  
          final Bitmap bitmap = ((BitmapDrawable)imgUsrClrId.getDrawable()).getBitmap();
          imgUsrClrId.setOnTouchListener(new OnTouchListener(){
              @Override
              public boolean onTouch(View v, MotionEvent event){
              int x = (int)event.getX();
              int y = (int)event.getY();
              int pixel = bitmap.getPixel(x,y);
  
              //then do what you want with the pixel data, e.g
              int redValue = Color.red(pixel);
              int blueValue = Color.blue(pixel);
              int greenValue = Color.green(pixel);  
  
              txtColorCodeId.setText(redValue+greenValue+blueValue);
              return false;
              }
         });
      }
  }
  </code></pre>
  
  <p><strong>XML code</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" &gt;
  
      &lt;ImageView
          android:id="@+id/imgUsrClrId"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:background="@drawable/female_front"
          android:layout_centerVertical="true" /&gt;
  
      &lt;TextView
          android:id="@+id/tvGenderId"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_above="@+id/imgUsrClrId"
          android:layout_toLeftOf="@+id/imgUsrClrId"
          android:text="Gender" /&gt;
  
      &lt;TextView
          android:id="@+id/tvAllSymptomsId"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_above="@+id/imgUsrClrId"
          android:layout_alignParentRight="true"
          android:layout_marginRight="14dp"
          android:text="All Symptoms" /&gt;
  
      &lt;Button
          android:id="@+id/btnRotateId"
          style="?android:attr/buttonStyleSmall"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignBottom="@+id/imgUsrClrId"
          android:layout_alignRight="@+id/tvAllSymptomsId"
          android:background="@drawable/rotate" /&gt;
  
      &lt;TextView
          android:id="@+id/txtColorCodeId"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true"
          android:text="TextView" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>