简体   繁体   中英

tabhost inside actionbar using fragment

I have used actionbar and it is working fine. I have four fragment in the actionbar. I want to implement tabhost in one of the fragment. I have used the following code.

    package com.main.udebate;

import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class Info extends Fragment {
    public Info() {
    }

    public static final String ARG_SECTION_NUMBER = "section_number";
    private TabHost mTabHost;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        Intent i = new Intent(Info.this, about.class);

         View view = inflater.inflate(R.layout.info, container, false);
         mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
         mTabHost.setup();

        TabHost.TabSpec tab = mTabHost.newTabSpec("my tab content");
        tab.setIndicator("my tab content");
        tab.setContent(i);
        mTabHost.addTab(tab);
        mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
        return view;

    }
}

I am getting error on Intent i = new Intent(Info.this, about.class); line as The constructor Intent(Info, Class) is undefined.

About.java

  package com.main.udebate;

import android.app.Activity;
import android.os.Bundle;

public class about extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
    }
}

Can someone pls help me to set tabhost inside fragment.

Thanks,

try to use this

Intent i = new Intent(getActivity(), about.class); 

instead of this line

Intent i = new Intent(Info.this, about.class);

As Info.this does not mean any context thats why you get "The constructor Intent(Info, Class) is undefined." this error.

In fragment where you use this reference you should instead use getActivity() reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM