3

I am using fragments. I have a spinner in the fragment. I want to lauch a new activity when spinner item is selected. I am getting this error

Error

The constructor Intent(UserHomeActivity, Class) is undefined UserHomeActivity.java /SwipeyTabs/src/com/recscores/android line 28 Java Problem

public class UserHomeActivity extends SherlockFragment{

Spinner spinnerTeam;
Spinner spinnerLeague;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View view =inflater.inflate(R.layout.user_home, container, false);


    // Team Spinner
    spinnerTeam = (Spinner)view.findViewById(R.id.spinner_team);
    spinnerTeam.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
            **Intent ii = new Intent(UserHomeActivity.this,TeamHomeActivity.class);
            startActivity(ii); **
        } 

        public void onNothingSelected(AdapterView<?> adapterView) {
            return;
        } 
    }); 
vikas sharma
  • 161
  • 4
  • 13

1 Answers1

10

Start new Activity as :

Intent ii = new Intent(getActivity(),TeamHomeActivity.class);
 startActivity(ii); 

because Context is not super class of SherlockFragment class and you will need to use getActivity() which return Activity to current fragment is associated.

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Thanks! I modified little bit this.getActivity() to getActivity(). – vikas sharma Mar 26 '13 at 04:52
  • @Android_Surfer : thanks for telling i missed Class name before `this` – ρяσѕρєя K Mar 26 '13 at 04:53
  • There is one more issue now...I want new activity to launch when i select an item from spinner dialog..but at present...by default the first item in the spinner is selected automatically and a TeamHomeActivity gets launched. Do you have any idea how to prevent it?..thanks – vikas sharma Mar 26 '13 at 04:57
  • @Android_Surfer : lots of questions already asked about same issue (to avoid default selection of Spinner) plz just make a search and if u still not get any success then ask new question we will sure help u – ρяσѕρєя K Mar 26 '13 at 05:00
  • I will search it again...I tried looking for a solution before but could not find an answer..thanks for your help. – vikas sharma Mar 26 '13 at 05:03
  • @Android_Surfer : have u seen this post [Android Spinner - How to default list selection to none](http://stackoverflow.com/questions/4424938/android-spinner-how-to-default-list-selection-to-none) – ρяσѕρєя K Mar 26 '13 at 05:11
  • @ ρяσѕρєя K: You motivated me to find an answer. I got it thanks!!!!!..http://stackoverflow.com/questions/12243834/spinner-onitemselectedlistener-issue – vikas sharma Mar 26 '13 at 05:12