2014-01-05 79 views
0

我知道這個問題上還有其他帖子,但我真的不知道如何將它應用到我的情況。我是一名新程序員,我正在嘗試使用我在網上找到的一些模板。我試圖讓忘記密碼按鈕工作。誤差即時得到是 方法getSupportFragmentManager()是未定義的類型LoginFragmentAndroid編程eclipse 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; 
     } 
    } 
} 
+0

如果我沒有記錯,'getSupportFragmentManager'是'FragmentActivity'中定義的一個方法。如果你需要在'Fragment'中獲取一個實例,請嘗試'getActivity()。getSupportFragmentManager()' - oops Szymon擊敗了我。 – Squonk

回答

2

getSupportFragmentManager()FragmentActivity類的方法。

更換

FragmentManager fm = getSupportFragmentManager(); 

FragmentManager fm = getActivity().getSupportFragmentManager(); 

前提是你的活動來延長FragmentActivity類。

+1

打我幾秒鐘。 – Squonk

0

這可能是因爲BaseFragment,其中LoginFragment是從Android支持庫中不是部分和只是本機sdk的一部分。如果是這樣的話,我只會使用getFragmentManager(),因爲它不依賴於android支持庫。

+0

嘗試使用'com.keyconsultant.parse.logintutorial'搜索 - 'BaseFragment'類在該教程中定義,它擴展了'android.support.v4.app.Fragment'。 – Squonk

相關問題