Android 对夏洛克碎片的活动

Android 对夏洛克碎片的活动,android,android-activity,android-fragments,actionbarsherlock,Android,Android Activity,Android Fragments,Actionbarsherlock,我想将我的应用程序(扩展活动)更改为片段(扩展SherlockFragment) 如果我改变它,我会有很多错误 public class AlarmClock extends SherlockFragment implements OnClickListener { 这是我的onCreateView: public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedIn

我想将我的应用程序(扩展活动)更改为片段(扩展SherlockFragment)

如果我改变它,我会有很多错误

public class AlarmClock extends SherlockFragment implements OnClickListener {
这是我的onCreateView:

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

    {
        // sanity check -- no database, no clock
        if (getContentResolver() == null) {
            new AlertDialog.Builder(this)
                    .setTitle(getString(R.string.error))
                    .setMessage(getString(R.string.dberror))
                    .setPositiveButton(android.R.string.ok,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    finish();
                                }
                            })
                    .setOnCancelListener(
                            new DialogInterface.OnCancelListener() {
                                public void onCancel(DialogInterface dialog) {
                                    finish();
                                }
                            }).setIcon(android.R.drawable.ic_dialog_alert)
                    .create().show();
            return;
        }
        View view = inflater.inflate(R.layout.alarm_clock, container, false);
        // menu buttons
        add = (ImageButton) findViewById(R.id.ibAdd);
        snooze = (ImageButton) findViewById(R.id.ibSnooze);

        add.setOnClickListener(this);
        snooze.setOnClickListener(this);
        mFactory = LayoutInflater.from(this);
        mPrefs = getSharedPreferences(PREFERENCES, 0);

        mCursor = Alarms.getAlarmsCursor(getContentResolver());
        mAlarmsList = (ListView) findViewById(R.id.alarms_list);
        mAlarmsList.setAdapter(new AlarmTimeAdapter(this, mCursor));
        mAlarmsList.setVerticalScrollBarEnabled(true);
        mAlarmsList.setItemsCanFocus(true);

        mClockLayout = (ViewGroup) findViewById(R.id.clock);

        mQuickAlarm = findViewById(R.id.ibSnooze);
        mQuickAlarm.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showQuickAlarmDialog();
            }
        });

        setVolumeControlStream(android.media.AudioManager.STREAM_ALARM);
        setQuickAlarmVisibility(mPrefs.getBoolean(PREF_SHOW_QUICK_ALARM, true));
        return view;
    }
有很多错误,因为没有活动。 如果是活动,它就会工作

我使用“扩展SherlockFragment”,因为我想将其添加到表中。
如何解决这个问题?请帮帮我。

如果我是对的,片段一定要在活动中使用

因此,不要使用这个而是使用getActivity()获取活动(使用此片段)上下文

比如:

getActivity.finish();
如果findViewById(//some Id)

像这样使用它:

inflatedView.findViewById(//Id);

如果我是对的,那么片段一定要在活动中使用

因此,不要使用这个而是使用getActivity()获取活动(使用此片段)上下文

比如:

getActivity.finish();
如果findViewById(//some Id)

像这样使用它:

inflatedView.findViewById(//Id);

片段
不是
上下文
(不同于
活动
应用程序
)。所以有很多方法是不可用的

但是,它可以访问它所附加的上下文。通常,您可以在片段中调用
getActivity()
来获取它。您应该使用
isAdded()
方法检查
片段是否是活动的一部分


您应该阅读一些关于片段的内容,移植现有活动以使用片段。。。可以使用Google获得教程。

片段
不是
上下文
(不同于
活动
应用程序
)。所以有很多方法是不可用的

但是,它可以访问它所附加的上下文。通常,您可以在片段中调用
getActivity()
来获取它。您应该使用
isAdded()
方法检查
片段是否是活动的一部分


您应该阅读一些关于片段的内容,移植现有活动以使用片段。。。使用谷歌可以获得教程。

有哪些错误?上下文错误??类型AlarmClock的方法getContentResolver()未定义;类型AlarmClock的方法findViewById(int)未定义;类型new DialogInterface的方法finish()未定义。OnClickListener(){}等。什么错误?上下文错误??类型AlarmClock的方法getContentResolver()未定义;类型AlarmClock的方法findViewById(int)未定义;类型new DialogInterface的方法finish()未定义。OnClickListener(){}等。无法解析充气视图。我必须使用片段,因为我需要在选项卡中使用它。这是我的主要活动(启动器)公共类AbsViewPageActivity扩展SherlockFragmentActivity无法解析。我必须使用片段,因为我需要在选项卡中使用它。这是我的主要活动(启动器)公共类AbsViewPageActivity扩展SherlockFragmentActivity扩展SherlockFragmentActivity如何在扩展SherlockFragment时为ActionBar使用OnCreateOptions菜单Sherlock@MarvinLabshow在扩展SherlockFragment时是否为ActionBar Sherlock使用onCreateOptionMenu@马尔文实验室