Android编程eclipse getSupportFragmentManager()未定义

时间:2014-01-05 22:31:56

标签: android android-fragments

我知道这个问题还有其他帖子,但我真的不知道如何将它应用到我的情况。我是一个新的程序员,我试图使用我在网上找到的一些模板。我试图让忘记密码按钮工作。我得到的错误是 对于LoginFragment类型

,方法getSupportFragmentManager()未定义
    /**
     * Fragment for logging in. Includes button for loading the Create account view.
     * 
     * @author Trey Robinson
     * 
     */
    public class LoginFragment extends BaseFragment {

        public static final String EXTRA_USERNAME = "com.keyconsultant.parse.logintutorial.activity.extra.USERNAME";
        public static final String EXTRA_PASSWORD = "com.keyconsultant.parse.logintutorial.activity.extra.PASSWORD";

        // UI references.
        private EditText mUserNameEditText;
        private EditText mPasswordEditText;

    /**
     * Factory method for creating new fragments
     * 
     * @return
     */
    public static LoginFragment newInstance() {
        return new LoginFragment();
    }

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

        View view = inflater.inflate(R.layout.fragment_login, container, false);
        mUserNameEditText = (EditText) view.findViewById(R.id.username);

        mPasswordEditText = (EditText) view.findViewById(R.id.password);
        mPasswordEditText
                .setOnEditorActionListener(new TextView.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView textView, int id,
                            KeyEvent keyEvent) {
                        if (id == EditorInfo.IME_NULL) {
                            attemptLogin();
                            return true;
                        }
                        return false;
                    }
                });

        view.findViewById(R.id.sign_in_button).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        attemptLogin();
                    }
                });

        view.findViewById(R.id.register_button).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        createAccount();
                    }
                });
        view.findViewById(R.id.forgot_button).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        forgotPassword();
                    }
                });
        return view;
    }

    /**
     * Open the forgotPassword dialog
     */
    private void forgotPassword() {
        FragmentManager fm = getSupportFragmentManager();
        ForgotPasswordDialogFragment forgotPasswordDialog = new ForgotPasswordDialogFragment();
        forgotPasswordDialog.show(fm, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (savedInstanceState != null) {
            mUserNameEditText.setText(savedInstanceState
                    .getString(EXTRA_USERNAME));
            mPasswordEditText.setText(savedInstanceState
                    .getString(EXTRA_PASSWORD));
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(EXTRA_USERNAME, mUserNameEditText.getText()
                .toString());
        outState.putString(EXTRA_PASSWORD, mPasswordEditText.getText()
                .toString());
    }

    /**
     * Attempts to sign in or register the account specified by the login form.
     * If there are form errors (invalid email, missing fields, etc.), the
     * errors are presented and no actual login attempt is made.
     */
    public void attemptLogin() {

        clearErrors();

        // Store values at the time of the login attempt.
        String username = mUserNameEditText.getText().toString();
        String password = mPasswordEditText.getText().toString();

        boolean cancel = false;
        View focusView = null;

        // Check for a valid password.
        if (TextUtils.isEmpty(password)) {
            mPasswordEditText
                    .setError(getString(R.string.error_field_required));
            focusView = mPasswordEditText;
            cancel = true;
        } else if (password.length() < 4) {
            mPasswordEditText
                    .setError(getString(R.string.error_invalid_password));
            focusView = mPasswordEditText;
            cancel = true;
        }

        // Check for a valid email address.
        if (TextUtils.isEmpty(username)) {
            mUserNameEditText
                    .setError(getString(R.string.error_field_required));
            focusView = mUserNameEditText;
            cancel = true;
        }

        if (cancel) {
            // There was an error; don't attempt login and focus the first
            // form field with an error.
            focusView.requestFocus();
        } else {
            // perform the user login attempt.
            UserManager.getInstance().authenticate(
                    username.toLowerCase(Locale.getDefault()), password);
        }
    }

    /**
     * Load the create account view.
     */
    private void createAccount() {
        FragmentManager fragmentManager = getActivity()
                .getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();
        fragmentTransaction
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        fragmentTransaction.replace(
                ((ViewGroup) getView().getParent()).getId(),
                CreateAccountFragment.newInstance());
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

    /**
     * Remove all edit text errors
     */
    private void clearErrors() {
        mUserNameEditText.setError(null);
        mPasswordEditText.setError(null);
    }

    @Subscribe
    public void onSignInError(AuthenticateUserErrorEvent event) {
        clearErrors();
        switch (event.getErrorCode()) {
        case ParseException.OBJECT_NOT_FOUND:
            mPasswordEditText
                    .setError(getString(R.string.error_incorrect_password));
            mPasswordEditText.requestFocus();
            break;
        default:
            UnknownErrorDialogFactory.createUnknownErrorDialog(
                    this.getActivity()).show();
            break;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

getSupportFragmentManager()FragmentActivity类的方法。

替换

FragmentManager fm = getSupportFragmentManager();

FragmentManager fm = getActivity().getSupportFragmentManager();

如果您的活动延伸FragmentActivity类。

答案 1 :(得分:0)

这可能是因为继承自BaseFragment的{​​{1}}不是android支持库的一部分,只是本机sdk的一部分。如果是这种情况,我会使用LoginFragment,因为它不依赖于android支持库。