我试图在回调过程中进行回调。这对安卓来说可能吗?我知道它与主线程有关,但我不知道有什么好的解决方法,因为我是制作 Android 应用程序的初学者。
有没有办法将这些进程放在 Android 的不同线程中?
非常感谢您的帮助!
注:进口全部包括在内。
public class LoginFragment extends Fragment {
public static final String TAG = LoginFragment.class.getSimpleName();
@InjectView(R.id.emailField) EditText mEmailField;
@InjectView(R.id.passwordField) EditText mPassword;
@InjectView(R.id.loginButton) Button mLoginButton;
@InjectView(R.id.signupButton) Button mSignupButton;
//Facebook CallbackManager
private CallbackManager mCallbackManager;
//Facebook Callback
/* This is the first callback */
private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
if(profile != null) {
/* This is the second callback within the first callback */
ParseFacebookUtils.logInInBackground(accessToken, new LogInCallback() {
@Override
public void done(ParseUser parseUser, ParseException e) {
if (e == null) {
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
getActivity().startActivity(intent);
} else {
Log.d(TAG, e.getMessage());
}
}
});
}
}
@Override
public void onCancel() {
//Create a dialog to remind the user there was something wrong.
}
@Override
public void onError(FacebookException e) {
}
};
//Empty constructor, for fragments.
public LoginFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Facebook Callback Manager. See above.
mCallbackManager = CallbackManager.Factory.create();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.fragment_login, container, false);
ButterKnife.inject(this, myFragmentView);
//Facebook button
LoginButton fbLoginButton = (LoginButton) myFragmentView.findViewById(R.id.fb_login_button);
fbLoginButton.setReadPermissions(Arrays.asList("user_friends", "email"));
fbLoginButton.setFragment(this);
fbLoginButton.registerCallback(mCallbackManager, mCallback);
return myFragmentView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//Get Facebook Fragment activity result after callback.
mCallbackManager.onActivityResult(requestCode, resultCode, data);
ParseFacebookUtils.onActivityResult(requestCode, resultCode, data);
}
@OnClick(R.id.loginButton) void loginUser() {
String username = mEmailField.getText().toString().trim();
String password = mPassword.getText().toString().trim();
if(username.isEmpty() || password.isEmpty()) {
Toast.makeText(getActivity(), "Oops. Login details incorrect.", Toast.LENGTH_LONG).show();
}
else {
ParseUser.logInInBackground(username, password, new LogInCallback() {
@Override
public void done(ParseUser parseUser, ParseException e) {
if(e == null) {
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
else {
Toast.makeText(getActivity(), "There was an error trying to log in your account. Please try again later", Toast.LENGTH_LONG).show();
}
}
});
}
}
@OnClick(R.id.signupButton) void toSignup() {
Intent intent = new Intent(getActivity(), SignupActivity.class);
startActivity(intent);
}
并且错误代码如下:
07-15 12:14:47.128 26515-26566/com.vonderphil.doorbell E/DataScheduler﹕ isDataSchedulerEnabled():false
07-15 12:14:55.478 26515-26515/com.vonderphil.doorbell E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.vonderphil.doorbell, PID: 26515
java.lang.NoSuchMethodError: com.parse.Parse.callbackOnMainThreadAsync
at com.parse.ParseFacebookUtils.logInInBackground(ParseFacebookUtils.java:149)
at com.vonderphil.doorbell.fragments.LoginFragment$1.onSuccess(LoginFragment.java:77)
at com.vonderphil.doorbell.fragments.LoginFragment$1.onSuccess(LoginFragment.java:69)
at com.facebook.login.LoginManager.finishLogin(LoginManager.java:508)
at com.facebook.login.LoginManager.onActivityResult(LoginManager.java:192)
at com.facebook.login.LoginManager$1.onActivityResult(LoginManager.java:140)
at com.facebook.internal.CallbackManagerImpl.onActivityResult(CallbackManagerImpl.java:82)
at com.vonderphil.doorbell.fragments.LoginFragment.onActivityResult(LoginFragment.java:162)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:163)
at android.app.Activity.dispatchActivityResult(Activity.java:5467)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3378)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3425)
at android.app.ActivityThread.access$1300(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1248)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)