Search code examples
androidandroid-intentandroid-tabhost

Cannot resolve constructor Intent


  • Im trying to add a tabhost to my application but i cant figure why this error 'cannot resolve constructor intent' is showing, here's my activity code:

    public class SixthFragment extends Fragment {
    public static final String TAG = "sixth";
    
    
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,       Bundle   savedInstanceState) {
    
    View view =  inflater.inflate(R.layout.sixthfragment, container, false);
    TabHost tabHost = (TabHost) view.findViewById(R.id.tabHost);
    
    TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab");
    TabHost.TabSpec tab2 = tabHost.newTabSpec("Second Tab");
    TabHost.TabSpec tab3 = tabHost.newTabSpec("Third tab");
    
    // Set the Tab name and Activity
    // that will be opened when particular Tab will be selected
    tab1.setIndicator("Tab1");
    tab1.setContent(new Intent(this,MainActivity.class));
    
    tab2.setIndicator("Tab2");
    tab2.setContent(new Intent(this,Tab2Activity.class));
    
    tab3.setIndicator("Tab3");
    tab3.setContent(new Intent(this,Tab3Activity.class));
    
    /** Add the tabs  to the TabHost to display. */
    tabHost.addTab(tab1);
    tabHost.addTab(tab2);
    tabHost.addTab(tab3);
    
    
    return view;
    
    
    }
    
    
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    
    }
    
    
    
    }
    

    some help would be apreciated, thanks for your time.


Solution

  • The first parameter to the Intent constructor that you are trying to use takes a Context. Fragment is not a Context. Use getActivity() instead of this.