title: Draw rectangle over camera2 preview android
body: <p>I'm trying to draw a rectangle  on a camera2 preview using the basic Camera2 from this link : 
  <a href="https://github.com/googlesamples/android-Camera2Basic" rel="nofollow noreferrer">https://github.com/googlesamples/android-Camera2Basic</a></p>
  
  <p>and i followed the answer from this question on the forum :</p>
  
  <p><a href="https://stackoverflow.com/questions/44208122/android-camera2-api-textureview-overlay-for-drawing-on-camera-preview">android Camera2 API + TextureView overlay for drawing on camera preview</a></p>
  
  <p>but i can't get it to work. I'll be very grateful if you can help me figure out how to solve my problem. Here is the class used to draw the rectangle : <strong>Rectangle.java</strong></p>
  
  <pre><code>package com.example.android.camera2basic;
  
  import android.content.Context;
  import android.graphics.Canvas;
  import android.graphics.Color;
  import android.graphics.Paint;
  import android.graphics.Rect;
  import android.view.View;
  
  public class Rectangle extends View {
  Paint paint = new Paint();
  
  public Rectangle(Context context) {
      super(context);
  }
  
  
  @Override
  public void onDraw(Canvas canvas) {
      paint.setColor(Color.GREEN);
      paint.setStyle(Paint.Style.FILL);
      Rect rect = new Rect(20, 56, 200, 112);
      canvas.drawRect(rect, paint );
  }
  }
  </code></pre>
  
  <p>xml file of the camera <strong>activity_camera.xml</strong> :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#000"
  tools:context="com.example.android.camera2basic.CameraActivity"&gt;
  
  &lt;LinearLayout
  android:id="@+id/surface"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" /&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>and this is the method used in  the class where i added the rectangle to camera preview <strong>Camera2BasicFragment.java</strong> :</p>
  
  <pre><code>  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
  
      View view = inflater.inflate(R.layout.fragment_camera2_basic, container, 
      false);
      LinearLayout linearLayout = (LinearLayout) 
      view.findViewById(R.id.surface);
      linearLayout.addView(new Rectangle(getActivity()));
      return view;
      }
  </code></pre>
  
  <p>and finaly the error appeared after the application crushed at launching :</p>
  
  <pre><code>java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.camera2basic/com.example.android.camera2basic.CameraActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
                                                                                    at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                                    at android.os.Looper.loop(Looper.java:156)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6523)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
                                                                                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference
                                                                                    at com.example.android.camera2basic.Camera2BasicFragment.onCreateView(Camera2BasicFragment.java:431)
                                                                                    at android.app.Fragment.performCreateView(Fragment.java:2361)
                                                                                    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1000)
                                                                                    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1178)
                                                                                    at android.app.BackStackRecord.run(BackStackRecord.java:815)
                                                                                    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1585)
                                                                                    at android.app.FragmentController.execPendingActions(FragmentController.java:371)
                                                                                    at android.app.Activity.performStart(Activity.java:6929)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2756)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864) 
                                                                                    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                                    at android.os.Looper.loop(Looper.java:156) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:6523) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
  </code></pre>