0

我参考http://developer.android.com/guide/topics/ui/controls/pickers.html试图创建一个时间选择器。

我按照说明为 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
        }
        }

并将以下代码添加到我的 MainActivity 中,在那里我还为我的应用程序创建了一个选项卡式 UI。

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

主要活动 :

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");
    }**



}

现在我的 getSupportFragmentManager() 是未定义的。我正在运行 Android 2.2。如何以及添加什么来消除此错误。

4

3 回答 3

0

TabActivity 已弃用。因此,如果需要为选项卡添加代码,您可以使用 FragmentActivity 扩展活动。你可以使用 tabhost 。以下是详细信息

/**

* 这演示了如何使用 FragmentTabHost 通过 Fragment 实现 TabHost 的选项卡之间的切换。*/ public class FragmentTabs extends FragmentActivity { private FragmentTabHost mTab​​Host;

@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);
}

}

于 2013-06-11T11:12:52.287 回答
0

您的 MainActivity 必须为此扩展FragmentActivity。您还应该查看TabActivity文档,该文档现已弃用,应使用FragmentTabHost替换。

于 2013-06-11T11:07:12.953 回答
0

TabActivity 现在已弃用。如果你想使用支持库,你的所有活动都应该从支持包中扩展FragmentActivity 。

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

请参阅文档的后面部分,其中解释了如何实现 TabActivity,并使用支持库。

于 2013-06-11T11:08:04.970 回答