Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment cannot be converted to Context

This is my first time using fragments (new to android dev) and I'm trying to set up a spinner. At the moment I am quite confused about context and can't seem to solve this error:

Error:(52, 78) error: incompatible types: HotkeysFragment cannot be converted to Context

Here is the code its referring to:

HotkeysFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }

    Spinner hotkey_selector_spinner = (Spinner) rootView.findViewById(R.id.hotkey_selector_spinner);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.hotkey_options, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    hotkey_selector_spinner.setAdapter(adapter);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootViewB = inflater.inflate(R.layout.fragment_hotkeys, container, false);
    rootView = rootViewB;
    return rootViewB;
}

The specific line is:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.hotkey_options, android.R.layout.simple_spinner_item);

Imports:

import android.app.Activity; 
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import java.util.zip.Inflater;

How can I fix this?

like image 206
mtmeyer Avatar asked Aug 12 '15 13:08

mtmeyer


3 Answers

From the Android docs on Fragments from here:

Caution: If you need a Context object within your Fragment, you can call getActivity(). However, be careful to call getActivity() only when the fragment is attached to an activity. When the fragment is not yet attached, or was detached during the end of its lifecycle, getActivity() will return null.

So, in addition to changing this to getActivity(), I also suggest that you work with getActivity() in onActivityCreated() (since you also need the view to be inflated first)

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }

    Spinner hotkey_selector_spinner = (Spinner) getView().findViewById(R.id.hotkey_selector_spinner);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
            R.array.hotkey_options, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    hotkey_selector_spinner.setAdapter(adapter);
}
like image 83
Andrei Tudor Diaconu Avatar answered Nov 11 '22 07:11

Andrei Tudor Diaconu


Change

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
        R.array.hotkey_options, android.R.layout.simple_spinner_item);

to

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
        R.array.hotkey_options, android.R.layout.simple_spinner_item);

Access context in fragment by using getActivity()

like image 3
M D Avatar answered Nov 11 '22 06:11

M D


Fragment can not be converted into Context, an Activity can.

So you should change

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
        R.array.hotkey_options, android.R.layout.simple_spinner_item);

to

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
        R.array.hotkey_options, android.R.layout.simple_spinner_item);
like image 1
Rohit5k2 Avatar answered Nov 11 '22 05:11

Rohit5k2