Kemo Kemo - 4 months ago 59
Java Question

Adding a List to Fragment in Android

I am learning Android by modifying examples. I am currently working on this example. What is different in my code is that I want to display the listView in a fragment.

Code (errors are shown in comments):

public class MovieListFragment extends Fragment {

private static final String TAG = MovieListFragment.class.getSimpleName();

private static final String url = "http://example/json/movies.json";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private ListView listView;
private CustomListAdapter adapter;

public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_movies, container, false);

listView = (ListView) findViewById(;
//cannot resolve method findViewById(?) and cannot resolve symbol list
adapter = new CustomListAdapter(this, movieList);

pDialog = new ProgressDialog(this);


getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1b1b1b")));
//cannot resolve getActionBar


//method does not override method from its superclass
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(, menu);
//cannot resolve method getMenuInflater() and cannot resolve symbol menu
return true;

Here I have the following errors:

  1. cannot resolve method findViewById(?)

  2. cannot resolve symbol list

  3. cannot resolve getActionBar

  4. method does not override method from its superclass

  5. cannot resolve method getMenuInflater()

  6. cannot resolve symbol menu

  7. customlistadapter ( list) in customlistadapter cannot be applied to (android.content.context)

  8. in pDialog = new ProgressDialog(this); ProgressDialog( list) in ProgressDialogcannot be applied to (com.kemo.editedtutorial.sliderfragments.MoviesListFragment)


Firstly from your oncreateview() you are returning your view so, instead use

View view=inflater.inflate(R.layout.fragment_movies, container, false);

then use this view to make use of layout elements as view.findViewById() instead of findViewById() and then finally at the end return this view

About error cannot resolve symbol list
Make sure that you have a listview defined in your layout fragment_movies which has id as android:id="@+id/list"

The getActionBar thing you will do in the activity which has this fragment. If you are using support library then you can change the actionbar drawable as

((YourParentActivity which has this fragment)(getActivity())).getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1b1b1b")));  
e.g ((MainActivity)(getActivity())).getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1b1b1b")));

You can do as follows to avoid inspection error around the getsupportActionbar:

 ActionBar actionBar = ((MainActivity)getActivity()).getSupportActionBar();
        if(actionBar!=null) {
            actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#1b1b1b")));

If you wish to use option menus from the fragment, then in your onCreate() of fragment, add setHasOptionMenu(true) line. This will access your Activity's option menu.
Then to use the option menu you have to write

    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);

The menu will be of your android Activity, use that menu to perform your operation. To get selected option menu

    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);

Hope this will help you