How to use a spinner within a fragment in Android studio












-2















I'm getting the error code I've printed below. I'm trying to use fragments and on my first fragment, i want to give users a choice of options using a spinner. To me it seems most logical to have this within my fragment1 class. However it doesn't seem to work. Furthermore at the moment my code works by calling the fragment 1 straight away, like my main activity.xml isn't shown (i'm not sure if this is correct or not, i'm a newb when it comes to android app development).



I'm getting the error on the ArrayAdapter call, marking 'this' as the culprate.



Fragment



public class Page1Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {

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


Spinner spinner = (Spinner) view.findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, //this is where the error is called
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);

return view;

}



}


Main Activity



package provider.ac.stir.ac.uk.finallab;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);



Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_page1));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_page2));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(),
tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new
TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {}
@Override
public void onTabReselected(TabLayout.Tab tab) {}
});



}
}


Error



Wrong 1st argument type. Found: 'provider.ac.tir.ac.uk.finallab.Page1Fragment', required: 'android.content.Context









share|improve this question























  • Possible duplicate of Argument this doesn´t work. [required: 'android.content.context'

    – Vladyslav Matviienko
    Nov 23 '18 at 6:40
















-2















I'm getting the error code I've printed below. I'm trying to use fragments and on my first fragment, i want to give users a choice of options using a spinner. To me it seems most logical to have this within my fragment1 class. However it doesn't seem to work. Furthermore at the moment my code works by calling the fragment 1 straight away, like my main activity.xml isn't shown (i'm not sure if this is correct or not, i'm a newb when it comes to android app development).



I'm getting the error on the ArrayAdapter call, marking 'this' as the culprate.



Fragment



public class Page1Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {

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


Spinner spinner = (Spinner) view.findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, //this is where the error is called
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);

return view;

}



}


Main Activity



package provider.ac.stir.ac.uk.finallab;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);



Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_page1));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_page2));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(),
tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new
TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {}
@Override
public void onTabReselected(TabLayout.Tab tab) {}
});



}
}


Error



Wrong 1st argument type. Found: 'provider.ac.tir.ac.uk.finallab.Page1Fragment', required: 'android.content.Context









share|improve this question























  • Possible duplicate of Argument this doesn´t work. [required: 'android.content.context'

    – Vladyslav Matviienko
    Nov 23 '18 at 6:40














-2












-2








-2








I'm getting the error code I've printed below. I'm trying to use fragments and on my first fragment, i want to give users a choice of options using a spinner. To me it seems most logical to have this within my fragment1 class. However it doesn't seem to work. Furthermore at the moment my code works by calling the fragment 1 straight away, like my main activity.xml isn't shown (i'm not sure if this is correct or not, i'm a newb when it comes to android app development).



I'm getting the error on the ArrayAdapter call, marking 'this' as the culprate.



Fragment



public class Page1Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {

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


Spinner spinner = (Spinner) view.findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, //this is where the error is called
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);

return view;

}



}


Main Activity



package provider.ac.stir.ac.uk.finallab;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);



Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_page1));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_page2));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(),
tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new
TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {}
@Override
public void onTabReselected(TabLayout.Tab tab) {}
});



}
}


Error



Wrong 1st argument type. Found: 'provider.ac.tir.ac.uk.finallab.Page1Fragment', required: 'android.content.Context









share|improve this question














I'm getting the error code I've printed below. I'm trying to use fragments and on my first fragment, i want to give users a choice of options using a spinner. To me it seems most logical to have this within my fragment1 class. However it doesn't seem to work. Furthermore at the moment my code works by calling the fragment 1 straight away, like my main activity.xml isn't shown (i'm not sure if this is correct or not, i'm a newb when it comes to android app development).



I'm getting the error on the ArrayAdapter call, marking 'this' as the culprate.



Fragment



public class Page1Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {

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


Spinner spinner = (Spinner) view.findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, //this is where the error is called
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);

return view;

}



}


Main Activity



package provider.ac.stir.ac.uk.finallab;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);



Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_page1));
tabLayout.addTab(tabLayout.newTab().setText(R.string.tab_page2));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(),
tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new
TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {}
@Override
public void onTabReselected(TabLayout.Tab tab) {}
});



}
}


Error



Wrong 1st argument type. Found: 'provider.ac.tir.ac.uk.finallab.Page1Fragment', required: 'android.content.Context






java android






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 0:32







user9730036




















  • Possible duplicate of Argument this doesn´t work. [required: 'android.content.context'

    – Vladyslav Matviienko
    Nov 23 '18 at 6:40



















  • Possible duplicate of Argument this doesn´t work. [required: 'android.content.context'

    – Vladyslav Matviienko
    Nov 23 '18 at 6:40

















Possible duplicate of Argument this doesn´t work. [required: 'android.content.context'

– Vladyslav Matviienko
Nov 23 '18 at 6:40





Possible duplicate of Argument this doesn´t work. [required: 'android.content.context'

– Vladyslav Matviienko
Nov 23 '18 at 6:40












1 Answer
1






active

oldest

votes


















0














ArrayAdapter.createFromResource expects a Context, and Fragment is not a Context, so try with its Activity. Simply change:



ArrayAdapter.createFromResource(this, R.array.planets_array, android.R.layout.simple_spinner_item);`


To:



ArrayAdapter.createFromResource(getActivity(), R.array.planets_array, android.R.layout.simple_spinner_item);`





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439411%2fhow-to-use-a-spinner-within-a-fragment-in-android-studio%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown
























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    ArrayAdapter.createFromResource expects a Context, and Fragment is not a Context, so try with its Activity. Simply change:



    ArrayAdapter.createFromResource(this, R.array.planets_array, android.R.layout.simple_spinner_item);`


    To:



    ArrayAdapter.createFromResource(getActivity(), R.array.planets_array, android.R.layout.simple_spinner_item);`





    share|improve this answer




























      0














      ArrayAdapter.createFromResource expects a Context, and Fragment is not a Context, so try with its Activity. Simply change:



      ArrayAdapter.createFromResource(this, R.array.planets_array, android.R.layout.simple_spinner_item);`


      To:



      ArrayAdapter.createFromResource(getActivity(), R.array.planets_array, android.R.layout.simple_spinner_item);`





      share|improve this answer


























        0












        0








        0







        ArrayAdapter.createFromResource expects a Context, and Fragment is not a Context, so try with its Activity. Simply change:



        ArrayAdapter.createFromResource(this, R.array.planets_array, android.R.layout.simple_spinner_item);`


        To:



        ArrayAdapter.createFromResource(getActivity(), R.array.planets_array, android.R.layout.simple_spinner_item);`





        share|improve this answer













        ArrayAdapter.createFromResource expects a Context, and Fragment is not a Context, so try with its Activity. Simply change:



        ArrayAdapter.createFromResource(this, R.array.planets_array, android.R.layout.simple_spinner_item);`


        To:



        ArrayAdapter.createFromResource(getActivity(), R.array.planets_array, android.R.layout.simple_spinner_item);`






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 0:56









        AaronAaron

        1,7132212




        1,7132212






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439411%2fhow-to-use-a-spinner-within-a-fragment-in-android-studio%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'