0

I refer to http://developer.android.com/guide/topics/ui/controls/pickers.html trying to create a timepicker.

I follow the instruction and created a class for TimePickerFragment.

import java.util.Calendar;

import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.widget.TimePicker;


public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute,
        DateFormat.is24HourFormat(getActivity()));
        }

        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // Do something with the time chosen by the user
        }
        }

and added this following code to my MainActivity where I had also created a tabbed UI for my app.

public void showTimePickerDialog(View v) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getSupportFragmentManager(), "timePicker");
}

MainActivity :

import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TabHost;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;


public class MainActivity extends TabActivity { 

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)


        intent = new Intent().setClass(this, Create.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("Create").setIndicator("Create",
                          res.getDrawable(R.drawable.ic_tab_artists))
                      .setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, View.class);

        spec = tabHost.newTabSpec("View").setIndicator("View",
                          res.getDrawable(R.drawable.ic_tab_albums))
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(2);



    }

    **public void showTimePickerDialog(View v) {
        DialogFragment newFragment = new TimePickerFragment();
        newFragment.show(getSupportFragmentManager(), "timePicker");
    }**



}

Now my getSupportFragmentManager() is undefined. I'm running Android 2.2. How and what to add to get rid of this error.

user2474106
  • 15
  • 1
  • 3

3 Answers3

0

Your MainActivity have to extend FragmentActivity for that. You should also take a look at the TabActivity documentation which is now deprecated and should be replaced using a FragmentTabHost.

Laurent Dezitter
  • 710
  • 1
  • 8
  • 16
  • Yes. I've changed to the coding but theFragmentStackSupport.CountingFragment and etc are now underlined with error. – user2474106 Jun 11 '13 at 11:45
  • Hi. How can I add those classes in my project? I've downloaded sample for android 2.2. Please be specific, I'm new to eclipse and android SDK. Thank you! – user2474106 Jun 11 '13 at 12:02
0

TabActivity is deprecated now. If you want to use the support library, all your activities should extend FragmentActivity from the support package.

http://developer.android.com/reference/android/app/TabActivity.html

See the later part of the document, where it explains how to implement a TabActivity, and use the support library.

Kumar Bibek
  • 9,016
  • 2
  • 39
  • 68
  • Yes. I've changed to the coding but theFragmentStackSupport.CountingFragment and etc are now underlined with error. – user2474106 Jun 11 '13 at 11:46
  • Do you have the support library added to your build path? Is it there in your libs folder of your Android app. – Kumar Bibek Jun 12 '13 at 06:19
0

TabActivity is depricated. So you can extends the activity with FragmentActivity while if you need to add code for tab. you can use tabhost . below is the details

/**

* This demonstrates how you can implement switching between the tabs of a * TabHost through fragments, using FragmentTabHost. */ public class FragmentTabs extends FragmentActivity { private FragmentTabHost mTabHost;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.fragment_tabs);
    mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

    mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
            FragmentStackSupport.CountingFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
            LoaderCursorSupport.CursorLoaderListFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
            LoaderCustomSupport.AppListFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("throttle").setIndicator("Throttle"),
            LoaderThrottleSupport.ThrottledLoaderListFragment.class, null);
}

}

chaitanya
  • 1,726
  • 2
  • 17
  • 13
  • Yes. I've changed to the above coding but theFragmentStackSupport.CountingFragment and etc are now underlined with error. – user2474106 Jun 11 '13 at 11:38
  • ok. did you check this thread http://stackoverflow.com/questions/8867134/android-sdk-fragment-support – chaitanya Jun 11 '13 at 12:06