title: Android getting result of AsyncTask in main Activity and add views to XML layout outside onCreate method
body: <p>I'm trying to obtain a list of videos from <code>Youtube</code> to be displayed on my <code>Activity</code>.
  I've created the main activity <code>MediaActivity.java</code> with two methods, <code>onCreate</code> and <code>getResult</code> which is called from the <code>onPostExecute</code> method in the <code>AsyncTask</code> class.
  Here is the <code>MediaActivity</code> code: </p>
  
  <pre><code>    public class MediaActivity extends Activity {
      YoutubeConnect youtubeRequest = new YoutubeConnect(); 
      ViewGroup media;
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          youtubeRequest.execute();
          setContentView(R.layout.media);
          media = (ViewGroup) this.findViewById(R.layout.media);
  
      }
  
      public void getResult(String result) throws JSONException {
  
  
          JSONObject jsonObj = new JSONObject(result);
  
          JSONObject jsonObj2 = jsonObj.getJSONObject("feed");
          JSONArray the_json_array = jsonObj2.getJSONArray("entry");
  
          int size = the_json_array.length();
  
          for (int i = 0; i &lt; size; i++) {
  
              //get all infos
                 [...]
              LinearLayout main = new LinearLayout(this);
              main.setOrientation(0);
              main.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  
              //thumb
              WebView thumb = new WebView(this);
              thumb.loadUrl(video_thumb);
              main.addView(thumb);
  
              LinearLayout right = new LinearLayout(this);
              right.setOrientation(1);
              right.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
  
              //title
              TextView title = new TextView(this);
              title.setText(video_title);
              right.addView(title);
  
              //description
              TextView description = new TextView(this);
              description.setText(video_description.substring(100)+"...");
              right.addView(description);
  
              //views
              TextView views = new TextView(this);
              views.setText("Views: "+video_views);
              right.addView(views);
  
              main.addView(right);                
  
              this.media.addView(main);
          }
  
  
       }      
  
  }
  </code></pre>
  
  <p>This is my custom <code>AsyncTask</code> class code:</p>
  
  <pre><code> public class YoutubeConnect extends AsyncTask&lt;String, String, String&gt; {
  
      public YoutubeConnect() {
      }
  
      @Override
      protected String doInBackground(String... params) {
           try {
               String APIurl = "https://gdata.youtube.com/feeds/api/videos?author=author&amp;alt=json";
                  DefaultHttpClient httpClient = new DefaultHttpClient();
                  HttpGet request = new HttpGet(APIurl);
                  HttpResponse response = httpClient.execute(request);
                  BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
                  StringBuilder builder = new StringBuilder();
                  for (String line = null; (line = reader.readLine()) != null;) {
                      builder.append(line).append("\n");
                  }
                  return builder.toString();
  
              } catch (Exception e) {
                  return null;
              }
  
      }
  
      @Override
      protected void onPostExecute(String result) {
          super.onPostExecute(result);
          MediaActivity mainthread = new MediaActivity();
          try {
              mainthread.getResult(result);
          } catch (JSONException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
  
  
  
   }
  </code></pre>
  
  <p>This is the log:</p>
  
  <pre><code> 05-17 11:01:26.383: E/AndroidRuntime(2687): FATAL EXCEPTION: main
  05-17 11:01:26.383: E/AndroidRuntime(2687): java.lang.NullPointerException
  05-17 11:01:26.383: E/AndroidRuntime(2687):     at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
  05-17 11:01:26.383: E/AndroidRuntime(2687):     at android.view.View.&lt;init&gt;(View.java:2696)
  05-17 11:01:26.383: E/AndroidRuntime(2687):     at android.view.ViewGroup.&lt;init&gt;(ViewGroup.java:374)
  05-17 11:01:26.383: E/AndroidRuntime(2687):     at android.widget.LinearLayout.&lt;init&gt;(LinearLayout.java:166)
  05-17 11:01:26.383: E/AndroidRuntime(2687):     at wachipi.palio.MediaActivity.getResult(MediaActivity.java:47)
  05-17 11:01:26.383: E/AndroidRuntime(2687):     at wachipi.palio.YoutubeConnect.onPostExecute(YoutubeConnect.java:41)
  05-17 11:01:26.383: E/AndroidRuntime(2687):     at wachipi.palio.YoutubeConnect.onPostExecute(YoutubeConnect.java:1)
  05-17 11:01:26.383: E/AndroidRuntime(2687):     at android.os.AsyncTask.finish(AsyncTask.java:602)
  05-17 11:01:26.383: E/AndroidRuntime(2687):     at android.os.AsyncTask.access$600(AsyncTask.java:156)
  05-17 11:01:26.383: E/AndroidRuntime(2687):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
  05-17 11:01:26.383: E/AndroidRuntime(2687):     at android.os.Handler.dispatchMessage(Handler.java:99)
  05-17 11:01:26.383: E/AndroidRuntime(2687):     at android.os.Looper.loop(Looper.java:137)
  05-17 11:01:26.383: E/AndroidRuntime(2687):     at android.app.ActivityThread.main(ActivityThread.java:4424)
  05-17 11:01:26.383: E/AndroidRuntime(2687):     at java.lang.reflect.Method.invokeNative(Native Method)
  05-17 11:01:26.383: E/AndroidRuntime(2687):     at java.lang.reflect.Method.invoke(Method.java:511)
  05-17 11:01:26.383: E/AndroidRuntime(2687):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  05-17 11:01:26.383: E/AndroidRuntime(2687):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  05-17 11:01:26.383: E/AndroidRuntime(2687):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Before various attempt the <code>setContenView</code> and <code>ViewGroup</code> lines were inside the <code>getResult</code> method, but it gave me other errors. I've also tried to use the debugger but it gives me error <code>"source not found"</code>, I think something is missing in my installation.
  I really can't understand why it throws this error and can't find anything about it. </p>