Search code examples
androidandroid-fragmentsactionbarsherlockandroid-holo-everywhere

Fragment tab content not shown with HoloEverywhere and ActionbarSherlock


Hi I'm making this app with two fragments that you can swipe between. The code compiles and runs without crashing and both tabs are shown and you can swipe between the tabs but the tabs have no content. I think this is a problem with the fragments trying to setcontentview. If you need anymore info just ask :)

my main activity

import java.util.ArrayList;

import org.holoeverywhere.app.Activity;
import org.holoeverywhere.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockActivity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.WindowManager;

public class SwipeTabsMainActivity extends Activity {
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
TextView tabCenter;
TextView tabText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.pager);

    setContentView(mViewPager);
    ActionBar bar = getSupportActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);

    mTabsAdapter = new TabsAdapter(this, mViewPager);

        mTabsAdapter.addTab(bar.newTab().setText("Stopwatch"),StopWatchFragment.class, null);
    mTabsAdapter.addTab(bar.newTab().setText("Timer"), CountdownFragment.class, null);



}

public static class TabsAdapter extends FragmentPagerAdapter implements
        ActionBar.TabListener, ViewPager.OnPageChangeListener {
    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    static final class TabInfo {
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(Class<?> _class, Bundle _args) {
            clss = _class;
            args = _args;
        }
    }

    public TabsAdapter(Activity activity, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mActionBar = activity.getSupportActionBar();
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mTabs.size();
    }

    @Override
    public Fragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(),
                info.args);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        mActionBar.setSelectedNavigationItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        Object tag = tab.getTag();
        for (int i = 0; i < mTabs.size(); i++) {
            if (mTabs.get(i) == tag) {
                mViewPager.setCurrentItem(i);
            }

        }
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }
}
}

the Main Activity's xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1" />

my countdown timer fragment

import org.holoeverywhere.app.Fragment;
import org.holoeverywhere.internal._View;
import org.holoeverywhere.preference.PreferenceManager;
import org.holoeverywhere.preference.SharedPreferences;
import org.holoeverywhere.widget.Button;
import org.holoeverywhere.widget.TextView;
import org.holoeverywhere.widget.Toast;

import android.content.Context;
import android.os.Bundle;
import android.os.Vibrator;
import org.holoeverywhere.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Chronometer;
import android.widget.ImageButton;
import android.widget.Chronometer.OnChronometerTickListener;
import android.widget.RelativeLayout;

public class CountdownFragment extends Fragment implements
    OnChronometerTickListener {
TextView second, hour, minute;
ImageButton hourup, hourdown, minuteup, minutedown, secondup, seconddown;
Button start, stop;
int hourCount, minCount, secCount;
Context c;
Chronometer count;
boolean viberate, running;
Vibrator v;
boolean startstopshow;
SharedPreferences getPrefs;
View fv = getView();
RelativeLayout rl;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    initilize(c);
    setClicker();
    count.start();
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
    c = getActivity();




}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.countdown, container, false);
     rl = (RelativeLayout )inflater.inflate(R.layout.countdown, container, false);

    return super.onCreateView(inflater, container, savedInstanceState);
}



private void initilize(Context c) {
    hourup = (ImageButton) rl.findViewById(R.id.hourup);
    minuteup = (ImageButton) rl.findViewById(R.id.minuteup);
    secondup = (ImageButton) rl.findViewById(R.id.secondup);
    hourdown = (ImageButton) rl.findViewById(R.id.hourdown);
    minutedown = (ImageButton) rl.findViewById(R.id.minutedown);
    seconddown = (ImageButton) rl.findViewById(R.id.seconddown);
    start = (Button) rl.findViewById(R.id.start);
    stop = (Button) rl.findViewById(R.id.stop);
    second = (TextView) rl.findViewById(R.id.second);
    minute = (TextView) rl.findViewById(R.id.minute);
    hour = (TextView) rl.findViewById(R.id.hour);
    hourCount = 0;
    minCount = 0;
    secCount = 0;

    count = (Chronometer) rl.findViewById(R.id.chronometer1);
    v = (Vibrator) c.getSystemService(Context.VIBRATOR_SERVICE);
    running = false;
    getPrefs = PreferenceManager.getDefaultSharedPreferences(c);
}

@Override
public void onChronometerTick(Chronometer chronometer) {
    switch (chronometer.getId()) {

    case (R.id.chronometer1):
        secCount--;
        updateDisplay();
        break;
    }
}

}

my fragments xml file

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="56dp" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/hourup"
            style="?borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/up" />

        <ImageButton
            android:id="@+id/minuteup"
            style="?borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/up" />

        <ImageButton
            android:id="@+id/secondup"
            style="?borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/up" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <org.holoeverywhere.widget.TextView
            android:id="@+id/hour"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="00 : "
            android:textSize="35sp" />

        <org.holoeverywhere.widget.TextView
            android:id="@+id/minute"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="00 : "
            android:textSize="35sp" />

        <org.holoeverywhere.widget.TextView
            android:id="@+id/second"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="00"
            android:textSize="35sp" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/hourdown"
            style="?borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/down" />

        <ImageButton
            android:id="@+id/minutedown"
            style="?borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/down" />

        <ImageButton
            android:id="@+id/seconddown"
            style="?borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/down" />
    </TableRow>
</TableLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_weight="50"
    android:gravity="bottom"
    android:weightSum="100" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_alignParentBottom="true" >

        <org.holoeverywhere.internal._View
            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="1dip"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="4dip"
            android:layout_marginRight="4dip"
            android:background="?dividerVertical" />

        <org.holoeverywhere.internal._View
            android:id="@+id/ViewColorPickerHelper"
            android:layout_width="1dip"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="4dip"
            android:layout_marginTop="4dip"
            android:background="?dividerVertical" />

        <org.holoeverywhere.widget.Button
            android:id="@+id/start"
            style="@style/Holo.Button.Borderless"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/view1"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/stop"
            android:text="Start" />

        <org.holoeverywhere.widget.Button
            android:id="@+id/stop"
            style="@style/Holo.Button.Borderless"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/ViewColorPickerHelper"
            android:text="Stop" />
    </RelativeLayout>
</LinearLayout>

<Chronometer
    android:id="@+id/chronometer1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Chronometer"
    android:textSize="0px"
    android:visibility="invisible" />

</RelativeLayout>

screenshot


Solution

  • I finally found the problem:

    You have to replace : super.onCreateView(inflater, container, savedInstanceState); to your countdown view :

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.countdown, container, false);
         rl = (RelativeLayout )inflater.inflate(R.layout.countdown, container, false);
    
        return  v;
    }
    

    Here's a screenshot of the result:

    enter image description here