title: Android thread cannot access Main.xml
body: <p>I am trying to use a thread to continuously take pictures. The problem is that I am getting a null pointer exception when I try to create my preview. </p>
  
  <pre><code>private class CameraThread extends ImageView implements Runnable
  {
      ...
      public CameraThread(Context c)
      {
          super(c);
          setContentView(R.layout.main);
          setFocusable(true);
          p = new Preview(c);
          Log.d(TAG,"preview null:"+(p==null));
          s = new SurfaceView(c);
          counter = 0;
          if(findViewById(R.id.preview)==null)
              Log.d(TAG,"preview view is null");
          ((FrameLayout) findViewById(R.id.preview)).addView(p);
          canvas = new Canvas();
          t = new TextView(c);
  
          captureButton = (Button) findViewById(R.id.captureButton);
          captureButton.setOnClickListener(new OnClickListener()
          {
              public void onClick(View v)
              {
                  running = true;
                  //new CaptureTask().execute();
              }
          });
          clearButton = (Button) findViewById(R.id.clearButton);
          clearButton.setOnClickListener(new OnClickListener()
          {
              public void onClick(View v)
              {
                  running = false;
              }
          });
      }
  </code></pre>
  
  <p>The debugger points me to this line:</p>
  
  <pre><code>((FrameLayout) findViewById(R.id.preview)).addView(p);
  </code></pre>
  
  <p>I'm not sure why this is null. This code all worked fine from within the main activity. The Preview object does not come out as null, however the findViewById returns a null object.</p>
  
  <p>Any thoughts?</p>
  
  <p>EDIT</p>
  
  <p>The thread is declared in the main activity. I'm modifying this code, this was originally implemented using buttons to capture individual pictures. I am trying to continuously take pictures. </p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      mCameraThread = new CameraThread(this);
      setContentView(mCameraThread);
      Thread thread = new Thread(mCameraThread);
      thread.start();
      Log.d(TAG,"onCreate done");
  }
  </code></pre>