【问题标题】:Where and how to extend FragmentActivity在哪里以及如何扩展 FragmentActivity
【发布时间】:2013-06-11 19:12:52
【问题描述】:

我指的是 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。如何以及添加什么来消除此错误。

【问题讨论】:

    标签: android


    【解决方案1】:

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

    /**
    

    * 这演示了如何实现一个标签之间的切换 * TabHost 通过 Fragment,使用 FragmentTabHost。 */ 公共类 FragmentTabs 扩展 FragmentActivity { 私有 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);
    }
    

    }

    【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 是的。我已更改为编码,但 FragmentStackSupport.CountingFragment 等现在带有错误下划线。
    • 嗨。如何在我的项目中添加这些类?我已经下载了 android 2.2 的示例。请具体说明,我是 eclipse 和 android SDK 的新手。谢谢!
    【解决方案3】:

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

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

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

    【讨论】:

    • 是的。我已更改为编码,但 FragmentStackSupport.CountingFragment 等现在带有错误下划线。
    • 您的构建路径中添加了支持库吗?它是否在您的 Android 应用程序的 libs 文件夹中。
    猜你喜欢
    • 2016-01-28
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 2023-04-02
    相关资源
    最近更新 更多