title: How to use setContentView in fragment instead of an activity
body: <p>Dear Stackoverflowers :)</p>
  
  <p><code>AllAssetsFragment</code> is opened from the MainActivity. In this fragment, I have trouble setting up the <code>setContentView()</code> and <code>getApplicationContext()</code> properly. </p>
  
  <p>I know there is something wrong. Some of the code works for normal activities but they are not working in this fragment.</p>
  
  <p><br /></p>
  
  <p>AllAssetsFragment:</p>
  
  <pre><code>import android.content.Intent;
  import android.os.Bundle;
  
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.fragment.app.Fragment;
  import androidx.recyclerview.widget.LinearLayoutManager;
  import androidx.recyclerview.widget.RecyclerView;
  import androidx.room.Room;
  
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  
  import com.android.volley.RequestQueue;
  import com.android.volley.Response;
  import com.android.volley.VolleyError;
  import com.android.volley.toolbox.JsonArrayRequest;
  import com.android.volley.toolbox.Volley;
  
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import java.util.ArrayList;
  import java.util.List;
  
  
  public class AllAssetsFragment extends Fragment {
  
      public AllAssetsFragment(){
  
  
      }
  
  
      private static final String HI = "https://uniqueandrocode.000webhostapp.com/hiren/favouritelist.php";
      private List&lt;AssetsItem&gt;assetsItems;
      private RecyclerView recyclerView;
      AssetsAdapter adapter;
  
      private JsonArrayRequest request;
      private RequestQueue requestQueue;
      public static FavoritesDatabase favoritesDatabase;
      Button btn;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          return inflater.inflate(R.layout.fragment_allassets, container, false);
  
  
          recyclerView = (RecyclerView)getView().findViewById(R.id.recyclerview);
          recyclerView.setHasFixedSize(true);
          // +++ PROBLEM 1: Context doesn't work (same on Problem 3 and 4) +++
          recyclerView.setLayoutManager(new LinearLayoutManager(this));
  
          assetsItems = new ArrayList&lt;&gt;();
          btn=(Button)getView().findViewById(R.id.favbtn);
          btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  // +++ PROBLEM 2: This code doesn't work to open Fragment from Fragment +++
                  startActivity(new Intent(AllAssetsFragment.this, FavoritesFragment.class));
              }
  
  
          });
          favoritesDatabase = Room.databaseBuilder(getActivity().getApplicationContext(), FavoritesDatabase.class, "myfavdb").allowMainThreadQueries().build();
          getData();
  
      }
  
      private void getData() {
          request = new JsonArrayRequest(HI, new Response.Listener&lt;JSONArray&gt;() {
              @Override
              public void onResponse(JSONArray response) {
                  JSONObject jsonObject = null;
                  for (int i = 0; i &lt; response.length(); i++) {
                      try {
                          JSONObject ob = response.getJSONObject(i);
                          AssetsItem pr = new AssetsItem(ob.getInt("id"),
                                  ob.getString("product_name"),
                                  ob.getString("product_img"));
                          assetsItems.add(pr);
  
                      } catch (JSONException e) {
                          e.printStackTrace();
                      }
                  }
                  setupData(assetsItems);
              }
          }, new Response.ErrorListener() {
              @Override
              public void onErrorResponse(VolleyError error) {
  
              }
          });
  
          // +++ PROBLEM 3: Context doesn't work +++
          requestQueue = Volley.newRequestQueue(this);
          requestQueue.add(request);
      }
  
      private void setupData(List&lt;AssetsItem&gt;assetsItems){
      adapter=new AssetsAdapter(assetsItems,getActivity().getApplicationContext());
      recyclerView.setAdapter(adapter);
      }
  }
  </code></pre>
  
  <p>And here our 2nd Fragment: FavoritesFragment:</p>
  
  <pre><code>
  
  import android.os.Bundle;
  
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.fragment.app.Fragment;
  import androidx.recyclerview.widget.LinearLayoutManager;
  import androidx.recyclerview.widget.RecyclerView;
  
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  import java.util.List;
  
  
  /**
   * A simple {@link Fragment} subclass.
   */
  public class FavoritesFragment extends Fragment {
  
  
      public FavoritesFragment() {
  
      }
  
      private RecyclerView rv;
      private FavoritesAdapter adapter;
  
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          return inflater.inflate(R.layout.fragment_favorites, container, false);
  
          rv=(RecyclerView)getView().findViewById(R.id.rec);
          rv.setHasFixedSize(true);
          // +++ PROBLEM 4: doesn't work +++
          rv.setLayoutManager(new LinearLayoutManager(this));
  
          getFavData();
      }
  
      private void getFavData(){
          List&lt;FavoritesItem&gt;favoritesItems=AllAssetsFragment.favoritesDatabase.favoritesDao().getFavoritesData();
  
          adapter = new FavoritesAdapter(favoritesItems,getActivity().getApplicationContext());
          rv.setAdapter(adapter);
      }
  
  
  
  }
  </code></pre>