上下文无法转换为活动

时间:2015-12-25 02:18:39

标签: java android android-fragments parse-platform

所以我得到了上述错误,这显然与这是一个片段而不是活动这一事实有关,但我不知道为什么会这样。这是来自ParseLoginUI中的文件,即PareLoginFragment.java

这些是出现错误的代码行

@Override
public void onAttach(Context context) {
  super.onAttach(context);

我正在附上完整的代码,但我怀疑它会有用。

/*
 *  Copyright (c) 2014, Parse, LLC. All rights reserved.
 *
 *  You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
 *  copy, modify, and distribute this software in source code or binary form for use
 *  in connection with the web services and APIs provided by Parse.
 *
 *  As with any software that integrates with the Parse platform, your use of
 *  this software is subject to the Parse Terms of Service
 *  [https://www.parse.com/about/terms]. This copyright notice shall be
 *  included in all copies or substantial portions of the software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

package com.parse.ui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.AccessToken;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.parse.LogInCallback;
import com.parse.ParseException;
import com.parse.ParseFacebookUtils;
import com.parse.ParseTwitterUtils;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import com.parse.twitter.Twitter;

import org.json.JSONObject;

/**
 * Fragment for the user login screen.
 */
public class ParseLoginFragment extends ParseLoginFragmentBase {

  public interface ParseLoginFragmentListener {
    public void onSignUpClicked(String username, String password);

    public void onLoginHelpClicked();

    public void onLoginSuccess();
  }

  private static final String LOG_TAG = "ParseLoginFragment";
  private static final String USER_OBJECT_NAME_FIELD = "name";

  private View parseLogin;
  private EditText usernameField;
  private EditText passwordField;
  private TextView parseLoginHelpButton;
  private Button parseLoginButton;
  private Button parseSignupButton;
  private Button facebookLoginButton;
  private Button twitterLoginButton;
  private ParseLoginFragmentListener loginFragmentListener;
  private ParseOnLoginSuccessListener onLoginSuccessListener;

  private ParseLoginConfig config;

  public static ParseLoginFragment newInstance(Bundle configOptions) {
    ParseLoginFragment loginFragment = new ParseLoginFragment();
    loginFragment.setArguments(configOptions);
    return loginFragment;
  }

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

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup parent,
                           Bundle savedInstanceState) {
    config = ParseLoginConfig.fromBundle(getArguments(), getActivity());

    View v = inflater.inflate(R.layout.com_parse_ui_parse_login_fragment,
        parent, false);
    ImageView appLogo = (ImageView) v.findViewById(R.id.app_logo);
    parseLogin = v.findViewById(R.id.parse_login);
    usernameField = (EditText) v.findViewById(R.id.login_username_input);
    passwordField = (EditText) v.findViewById(R.id.login_password_input);
    parseLoginHelpButton = (Button) v.findViewById(R.id.parse_login_help);
    parseLoginButton = (Button) v.findViewById(R.id.parse_login_button);
    parseSignupButton = (Button) v.findViewById(R.id.parse_signup_button);
    facebookLoginButton = (Button) v.findViewById(R.id.facebook_login);
    twitterLoginButton = (Button) v.findViewById(R.id.twitter_login);

    if (appLogo != null && config.getAppLogo() != null) {
      appLogo.setImageResource(config.getAppLogo());
    }
    if (allowParseLoginAndSignup()) {
      setUpParseLoginAndSignup();
    }
    if (allowFacebookLogin()) {
      setUpFacebookLogin();
    }
    if (allowTwitterLogin()) {
      setUpTwitterLogin();
    }
    return v;
  }

  @Override
  public void onAttach(Context context) {
    super.onAttach(context);

    final Activity activity = getActivity();
    if (activity instanceof ParseLoginFragmentListener) {
      loginFragmentListener = (ParseLoginFragmentListener) activity;
    } else {
      throw new IllegalArgumentException(
          "Activity must implemement ParseLoginFragmentListener");
    }

    if (activity instanceof ParseOnLoginSuccessListener) {
      onLoginSuccessListener = (ParseOnLoginSuccessListener) activity;
    } else {
      throw new IllegalArgumentException(
          "Activity must implemement ParseOnLoginSuccessListener");
    }

    if (activity instanceof ParseOnLoadingListener) {
      onLoadingListener = (ParseOnLoadingListener) activity;
    } else {
      throw new IllegalArgumentException(
          "Activity must implemement ParseOnLoadingListener");
    }
  }

  @Override
  protected String getLogTag() {
    return LOG_TAG;
  }

  private void setUpParseLoginAndSignup() {
    parseLogin.setVisibility(View.VISIBLE);

    if (config.isParseLoginEmailAsUsername()) {
      usernameField.setHint(R.string.com_parse_ui_email_input_hint);
      usernameField.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
    }

    if (config.getParseLoginButtonText() != null) {
      parseLoginButton.setText(config.getParseLoginButtonText());
    }

    parseLoginButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        String username = usernameField.getText().toString();
        String password = passwordField.getText().toString();

        if (username.length() == 0) {
          if (config.isParseLoginEmailAsUsername()) {
            showToast(R.string.com_parse_ui_no_email_toast);
          } else {
            showToast(R.string.com_parse_ui_no_username_toast);
          }
        } else if (password.length() == 0) {
          showToast(R.string.com_parse_ui_no_password_toast);
        } else {
          loadingStart(true);
          ParseUser.logInInBackground(username, password, new LogInCallback() {
            @Override
            public void done(ParseUser user, ParseException e) {
              if (isActivityDestroyed()) {
                return;
              }

              if (user != null) {
                loadingFinish();
                loginSuccess();
              } else {
                loadingFinish();
                if (e != null) {
                  debugLog(getString(R.string.com_parse_ui_login_warning_parse_login_failed) +
                      e.toString());
                  if (e.getCode() == ParseException.OBJECT_NOT_FOUND) {
                    if (config.getParseLoginInvalidCredentialsToastText() != null) {
                      showToast(config.getParseLoginInvalidCredentialsToastText());
                    } else {
                      showToast(R.string.com_parse_ui_parse_login_invalid_credentials_toast);
                    }
                    passwordField.selectAll();
                    passwordField.requestFocus();
                  } else {
                    showToast(R.string.com_parse_ui_parse_login_failed_unknown_toast);
                  }
                }
              }
            }
          });
        }
      }
    });

    if (config.getParseSignupButtonText() != null) {
      parseSignupButton.setText(config.getParseSignupButtonText());
    }

    parseSignupButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        String username = usernameField.getText().toString();
        String password = passwordField.getText().toString();

        loginFragmentListener.onSignUpClicked(username, password);
      }
    });

    if (config.getParseLoginHelpText() != null) {
      parseLoginHelpButton.setText(config.getParseLoginHelpText());
    }

    parseLoginHelpButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        loginFragmentListener.onLoginHelpClicked();
      }
    });
  }


  private LogInCallback facebookLoginCallbackV4 = new LogInCallback() {
    @Override
    public void done(ParseUser user, ParseException e) {
      if (isActivityDestroyed()) {
        return;
      }

      if (user == null) {
        loadingFinish();
        if (e != null) {
          showToast(R.string.com_parse_ui_facebook_login_failed_toast);
          debugLog(getString(R.string.com_parse_ui_login_warning_facebook_login_failed) +
                  e.toString());
        }
      } else if (user.isNew()) {
        GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
            new GraphRequest.GraphJSONObjectCallback() {
              @Override
              public void onCompleted(JSONObject fbUser,
                                      GraphResponse response) {
                  /*
                    If we were able to successfully retrieve the Facebook
                    user's name, let's set it on the fullName field.
                  */
                ParseUser parseUser = ParseUser.getCurrentUser();
                if (fbUser != null && parseUser != null
                        && fbUser.optString("name").length() > 0) {
                  parseUser.put(USER_OBJECT_NAME_FIELD, fbUser.optString("name"));
                  parseUser.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                      if (e != null) {
                        debugLog(getString(
                                R.string.com_parse_ui_login_warning_facebook_login_user_update_failed) +
                                e.toString());
                      }
                      loginSuccess();
                    }
                  });
                }
                loginSuccess();
              }
            }
        ).executeAsync();
      } else {
        loginSuccess();
      }
    }
  };

  private void setUpFacebookLogin() {
    facebookLoginButton.setVisibility(View.VISIBLE);

    if (config.getFacebookLoginButtonText() != null) {
      facebookLoginButton.setText(config.getFacebookLoginButtonText());
    }

    facebookLoginButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        loadingStart(false); // Facebook login pop-up already has a spinner
        if (config.isFacebookLoginNeedPublishPermissions()) {
          ParseFacebookUtils.logInWithPublishPermissionsInBackground(getActivity(),
                  config.getFacebookLoginPermissions(), facebookLoginCallbackV4);
        } else {
          ParseFacebookUtils.logInWithReadPermissionsInBackground(getActivity(),
                  config.getFacebookLoginPermissions(), facebookLoginCallbackV4);
        }
      }
    });
  }

  private void setUpTwitterLogin() {
    twitterLoginButton.setVisibility(View.VISIBLE);

    if (config.getTwitterLoginButtonText() != null) {
      twitterLoginButton.setText(config.getTwitterLoginButtonText());
    }

    twitterLoginButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        loadingStart(false); // Twitter login pop-up already has a spinner
        ParseTwitterUtils.logIn(getActivity(), new LogInCallback() {
          @Override
          public void done(ParseUser user, ParseException e) {
            if (isActivityDestroyed()) {
              return;
            }

            if (user == null) {
              loadingFinish();
              if (e != null) {
                showToast(R.string.com_parse_ui_twitter_login_failed_toast);
                debugLog(getString(R.string.com_parse_ui_login_warning_twitter_login_failed) +
                    e.toString());
              }
            } else if (user.isNew()) {
              Twitter twitterUser = ParseTwitterUtils.getTwitter();
              if (twitterUser != null
                  && twitterUser.getScreenName().length() > 0) {
                /*
                  To keep this example simple, we put the users' Twitter screen name
                  into the name field of the Parse user object. If you want the user's
                  real name instead, you can implement additional calls to the
                  Twitter API to fetch it.
                */
                user.put(USER_OBJECT_NAME_FIELD, twitterUser.getScreenName());
                user.saveInBackground(new SaveCallback() {
                  @Override
                  public void done(ParseException e) {
                    if (e != null) {
                      debugLog(getString(
                          R.string.com_parse_ui_login_warning_twitter_login_user_update_failed) +
                          e.toString());
                    }
                    loginSuccess();
                  }
                });
              }
            } else {
              loginSuccess();
            }
          }
        });
      }
    });
  }

  private boolean allowParseLoginAndSignup() {
    if (!config.isParseLoginEnabled()) {
      return false;
    }

    if (usernameField == null) {
      debugLog(R.string.com_parse_ui_login_warning_layout_missing_username_field);
    }
    if (passwordField == null) {
      debugLog(R.string.com_parse_ui_login_warning_layout_missing_password_field);
    }
    if (parseLoginButton == null) {
      debugLog(R.string.com_parse_ui_login_warning_layout_missing_login_button);
    }
    if (parseSignupButton == null) {
      debugLog(R.string.com_parse_ui_login_warning_layout_missing_signup_button);
    }
    if (parseLoginHelpButton == null) {
      debugLog(R.string.com_parse_ui_login_warning_layout_missing_login_help_button);
    }

    boolean result = (usernameField != null) && (passwordField != null)
        && (parseLoginButton != null) && (parseSignupButton != null)
        && (parseLoginHelpButton != null);

    if (!result) {
      debugLog(R.string.com_parse_ui_login_warning_disabled_username_password_login);
    }
    return result;
  }

  private boolean allowFacebookLogin() {
    if (!config.isFacebookLoginEnabled()) {
      return false;
    }

    if (facebookLoginButton == null) {
      debugLog(R.string.com_parse_ui_login_warning_disabled_facebook_login);
      return false;
    } else {
      return true;
    }
  }

  private boolean allowTwitterLogin() {
    if (!config.isTwitterLoginEnabled()) {
      return false;
    }

    if (twitterLoginButton == null) {
      debugLog(R.string.com_parse_ui_login_warning_disabled_twitter_login);
      return false;
    } else {
      return true;
    }
  }

  private void loginSuccess() {
    onLoginSuccessListener.onLoginSuccess();
  }

}

这是事件日志

2:18:25 AM Gradle sync started
2:19:12 AM Gradle sync failed: Cause: failed to find target with hash string 'android-21' in: C:\Users\mishr\AppData\Local\Android\sdk
           Consult IDE log for more details (Help | Show Log)
2:20:40 AM NullPointerException: null
2:20:42 AM NullPointerException: null
2:25:46 AM Gradle sync started
2:26:00 AM Gradle sync failed: Cause: failed to find target with hash string 'android-21' in: C:\Users\mishr\AppData\Local\Android\sdk
           Consult IDE log for more details (Help | Show Log)
8:26:07 AM NullPointerException: null
8:26:09 AM NullPointerException: null
8:26:16 AM Gradle sync started
8:27:07 AM Gradle sync failed: failed to find Build Tools revision 21.1.1
           Consult IDE log for more details (Help | Show Log)
8:27:59 AM Gradle sync started
8:28:35 AM Gradle sync failed: Cause: failed to find target with hash string 'android-23' in: C:\Users\mishr\AppData\Local\Android\sdk
           Consult IDE log for more details (Help | Show Log)
8:28:44 AM NullPointerException: null
8:28:45 AM NullPointerException: null
8:29:17 AM Executing tasks: [clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources]
8:29:24 AM Gradle build finished with 1 error(s) and 1 warning(s) in 6s 468ms
8:30:13 AM NullPointerException: null
8:30:15 AM NullPointerException: null
8:30:53 AM Gradle sync started
8:30:58 AM Gradle sync failed: Cause: failed to find target with hash string 'android-23' in: C:\Users\mishr\AppData\Local\Android\sdk
           Consult IDE log for more details (Help | Show Log)
8:31:02 AM NullPointerException: null
8:31:04 AM NullPointerException: null
8:31:20 AM NullPointerException: null
8:31:23 AM NullPointerException: null
8:31:28 AM NullPointerException: null
8:31:28 AM NullPointerException: null
8:31:36 AM NullPointerException: null
8:31:53 AM NullPointerException: null
9:52:58 AM Gradle sync started
9:53:41 AM Gradle sync completed
9:54:03 AM Executing tasks: [:app:generateDebugSources, :app:generateDebugAndroidTestSources, :ParseUI-Widget:generateDebugSources, :ParseUI-Widget:generateDebugAndroidTestSources, :ParseUI-Login:generateDebugSources, :ParseUI-Login:generateDebugAndroidTestSources]
9:55:09 AM Gradle build finished with 1 warnings(s) in 1m 27s 539ms
10:09:57 AM Executing tasks: [clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :ParseUI-Widget:generateDebugSources, :ParseUI-Widget:generateDebugAndroidTestSources, :ParseUI-Login:generateDebugSources, :ParseUI-Login:generateDebugAndroidTestSources]
10:14:36 AM Gradle build finished with 1 warnings(s) in 4m 38s 465ms
10:14:48 AM Executing tasks: [clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :ParseUI-Widget:generateDebugSources, :ParseUI-Widget:generateDebugAndroidTestSources, :ParseUI-Login:generateDebugSources, :ParseUI-Login:generateDebugAndroidTestSources]
10:31:11 AM Gradle build finished with 1 warnings(s) in 2m 7s 456ms
10:31:41 AM Executing tasks: [clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :ParseUI-Widget:generateDebugSources, :ParseUI-Widget:generateDebugAndroidTestSources, :ParseUI-Login:generateDebugSources, :ParseUI-Login:generateDebugAndroidTestSources]
10:33:11 AM Gradle build finished with 1 warnings(s) in 1m 29s 506ms
5:20:07 PM Executing tasks: [:app:assembleDebug]
5:23:26 PM Gradle build finished with 1 error(s) in 3m 19s 901ms
5:29:53 PM Gradle sync started
5:30:41 PM Gradle sync completed
5:30:43 PM Executing tasks: [:app:assembleDebug]
5:32:44 PM Gradle build finished with 1 error(s) in 2m 1s 705ms
5:35:39 PM Executing tasks: [clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :ParseUI-Widget:generateDebugSources, :ParseUI-Widget:generateDebugAndroidTestSources, :ParseUI-Login:generateDebugSources, :ParseUI-Login:generateDebugAndroidTestSources]
5:37:14 PM Gradle build finished with 17 error(s) and 1 warning(s) in 1m 34s 636ms
5:37:14 PM Gradle sync started
5:37:30 PM Gradle sync completed
5:42:33 PM Gradle sync started
5:43:01 PM Gradle sync completed
5:43:03 PM Executing tasks: [:app:assembleDebug]
5:43:24 PM Gradle build finished with 1 error(s) in 21s 21ms
5:46:11 PM Executing tasks: [clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :ParseUI-Widget:generateDebugSources, :ParseUI-Widget:generateDebugAndroidTestSources, :ParseUI-Login:generateDebugSources, :ParseUI-Login:generateDebugAndroidTestSources]
5:46:39 PM Gradle sync started
5:46:54 PM Gradle build finished with 82 error(s) and 1 warning(s) in 42s 738ms
5:49:20 PM Gradle sync completed
5:49:25 PM Executing tasks: [:app:generateDebugSources, :app:generateDebugAndroidTestSources, :ParseUI-Widget:generateDebugSources, :ParseUI-Widget:generateDebugAndroidTestSources, :ParseUI-Login:generateDebugSources, :ParseUI-Login:generateDebugAndroidTestSources]
5:49:46 PM Gradle build finished with 82 error(s) and 1 warning(s) in 25s 917ms
5:50:07 PM Executing tasks: [:app:assembleDebug]
5:50:20 PM Gradle build finished with 1 error(s) in 13s 993ms
5:53:46 PM Gradle sync started
5:54:22 PM Gradle sync completed
5:54:26 PM Executing tasks: [:app:assembleDebug]
5:54:36 PM Gradle build finished with 1 error(s) in 10s 865ms
5:55:40 PM Gradle sync started
5:55:59 PM Gradle sync completed
5:56:01 PM Executing tasks: [:app:assembleDebug]
5:56:13 PM Gradle build finished with 1 error(s) in 12s 660ms
5:56:37 PM Executing tasks: [:app:assembleDebug]
5:58:12 PM Gradle build finished in 1m 23s 278ms
5:58:43 PM Session 'app': Launched on motorola-xt1064-ZX1D52PP8R
6:33:19 PM Branch handling_login was created (show balloon)
6:36:35 PM Executing tasks: [clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:compileDebugSources, :app:compileDebugAndroidTestSources, :ParseUI-Widget:generateDebugSources, :ParseUI-Widget:generateDebugAndroidTestSources, :ParseUI-Widget:compileDebugSources, :ParseUI-Widget:compileDebugAndroidTestSources, :ParseUI-Login:generateDebugSources, :ParseUI-Login:generateDebugAndroidTestSources, :ParseUI-Login:compileDebugSources, :ParseUI-Login:compileDebugAndroidTestSources]
6:39:32 PM Gradle build finished with 3 error(s) and 1 warning(s) in 2m 56s 747ms
6:51:44 PM Executing tasks: [clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:compileDebugSources, :app:compileDebugAndroidTestSources, :ParseUI-Widget:generateDebugSources, :ParseUI-Widget:generateDebugAndroidTestSources, :ParseUI-Widget:compileDebugSources, :ParseUI-Widget:compileDebugAndroidTestSources, :ParseUI-Login:generateDebugSources, :ParseUI-Login:generateDebugAndroidTestSources, :ParseUI-Login:compileDebugSources, :ParseUI-Login:compileDebugAndroidTestSources]
6:53:38 PM Gradle build finished with 2 error(s) and 1 warning(s) in 1m 54s 354ms

0 个答案:

没有答案