如何在 activity 中使用 Fragment - 出现很多错误

How to use Fragment in an activity - giving lots of errors

这是ImageActivity.java

package com.example.app6;

import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.FrameLayout;

public abstract class ImageActivity extends FragmentActivity {

    private ExampleFragment mFragment;

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

        FrameLayout frame = new FrameLayout(this);
        if (savedInstanceState == null) {
            mFragment = new ExampleFragment();
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.add(frame.getId(), mFragment).commit();
        }

        setContentView(frame);
    }
}

这是ExampleFragment.java

package com.example.app6;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class ExampleFragment extends Activity {

    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        Button button = new Button(getActivity());
        button.setText("Hello There");
        return button;
    }
}

现在这两个文件都给我错误。在 ExampleFragment 中,

cannot resolve methos 'getActivity()'

并且在 ImageActivity 中,

cannot resolve method 'add(int, com.example.app6.ExampleFragment)'

我是 Android 的新手,所以我对此了解不多。请帮帮我。提前致谢:)

public class ExampleFragment extends Activity {

您的 Fragment class 需要扩展 Fragment。