title: trying to launch a fragment from an async task
body: <p>I have an async task which is called for a fragment and populates a listview. When I try and set the OnClick for the listview I get an error in my code for setting the fragment to load based on the listview item clicked:</p>
  
  <pre><code> FragmentManager man= getFragmentManager();
                          FragmentTransaction tran=man.beginTransaction();
                          Fragment_one = new StylePage2();
                          final Bundle bundle = new Bundle();
                          bundle.putString("beerIDSent", bID);
                          Fragment_one.setArguments(bundle);
                          tran.replace(R.id.main, Fragment_one);//tran.
                          tran.addToBackStack(null);
                          tran.commit();
  </code></pre>
  
  <p>The error shows for the line:</p>
  
  <pre><code>FragmentManager man= getFragmentManager();
  </code></pre>
  
  <p>The error is, can not resolve method getFragmentManager()</p>
  
  <p>I am assuming you can only access that method from within a fragment, so I am a bit lost on how to launch it from something that extends asynctask.</p>
  
  <p>The full code for the async task is below:</p>
  
  <pre><code>public class GetStyleStatisticsJSON extends AsyncTask&lt;String, Void, String&gt; {
  
      Context c;
      private ProgressDialog Dialog;
  
  
      android.support.v4.app.Fragment Fragment_one;
  
      public GetStyleStatisticsJSON(Context context)
      {
          c = context;
          Dialog = new ProgressDialog(c);
      }
  
      @Override
      protected String doInBackground(String... arg0) {
          // TODO Auto-generated method stub
          return readJSONFeed(arg0[0]);
      }
  
      protected void onPreExecute() {
          Dialog.setMessage("Analyzing Statistics");
  
          Dialog.setTitle("Loading");
          Dialog.setCancelable(false);
          Dialog.show();
      }
  
      protected void onPostExecute(String result){
          //decode json here
          try{
              JSONArray jsonArray = new JSONArray(result);
  
  
              //acces listview
              ListView lv = (ListView) ((Activity) c).findViewById(R.id.yourStyleStatistics);
  
              //make array list for beer
              final List&lt;StyleInfo&gt; tasteList = new ArrayList&lt;StyleInfo&gt;();
  
  
  
              for(int i = 0; i &lt; jsonArray.length(); i++) {
  
                  String style = jsonArray.getJSONObject(i).getString("style");
                  String rate = jsonArray.getJSONObject(i).getString("rate");
                  String beerID = jsonArray.getJSONObject(i).getString("id");
  
                  int count = i + 1;
  
                  style = count + ". " + style;
  
  
                  //create object
                  StyleInfo tempTaste = new StyleInfo(style, rate, beerID);
  
                  //add to arraylist
                  tasteList.add(tempTaste);
  
  
                  //add items to listview
                  StyleInfoAdapter adapter1 = new StyleInfoAdapter(c ,R.layout.brewer_stats_listview, tasteList);
                  lv.setAdapter(adapter1);
  
                  //set up clicks
                  lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                      @Override
                      public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1,
                                              int arg2, long arg3) {
                          StyleInfo o=(StyleInfo)arg0.getItemAtPosition(arg2);
  
                          String bID = o.id;
  
                          //todo: add onclick for fragment to load
                          FragmentManager man= getFragmentManager();
                          FragmentTransaction tran=man.beginTransaction();
                          Fragment_one = new StylePage2();
                          final Bundle bundle = new Bundle();
                          bundle.putString("beerIDSent", bID);
                          Fragment_one.setArguments(bundle);
                          tran.replace(R.id.main, Fragment_one);//tran.
                          tran.addToBackStack(null);
                          tran.commit();
  
  
  
                      }
                  });
  
  
              }
  
          }
          catch(Exception e){
  
          }
  
          Dialog.dismiss();
  
      }
  
      public String readJSONFeed(String URL) {
          StringBuilder stringBuilder = new StringBuilder();
          HttpClient httpClient = new DefaultHttpClient();
          HttpGet httpGet = new HttpGet(URL);
          try {
              HttpResponse response = httpClient.execute(httpGet);
              StatusLine statusLine = response.getStatusLine();
              int statusCode = statusLine.getStatusCode();
              if (statusCode == 200) {
                  HttpEntity entity = response.getEntity();
                  InputStream inputStream = entity.getContent();
                  BufferedReader reader = new BufferedReader(
                          new InputStreamReader(inputStream));
                  String line;
                  while ((line = reader.readLine()) != null) {
                      stringBuilder.append(line);
                  }
                  inputStream.close();
              } else {
                  Log.d("JSON", "Failed to download file");
              }
          } catch (Exception e) {
              Log.d("readJSONFeed", e.getLocalizedMessage());
          }
          return stringBuilder.toString();
      }
  
  }
  </code></pre>
  
  <p><em><strong>UPDATE</em></strong></p>
  
  <p>I tried changing it to this:</p>
  
  <pre><code>FragmentManager man = ((Activity) c).getFragmentManager();
  </code></pre>
  
  <p>BUt I am getting this error:</p>
  
  <pre><code>Incompatible types.
  Required:
  android.support.v4.app.FragmentManager
  Found:
  android.app.FragmentManager
  </code></pre>
  
  <p><em><strong>Update 2</em></strong></p>
  
  <p>I just tried this:</p>
  
  <pre><code>FragmentManager man= MainDraw.getFragmentManager();
  </code></pre>
  
  <p>and get this error:</p>
  
  <pre><code>Non-static method 'getFragmentManager()' cannot be referenced from a static context
  </code></pre>