我使用backendless.com作为我申请的后端。我需要在片段中记录我的用户。我一直收到语法错误,指出无法识别backendless.com提供的异步登录方法。它在Activity中完美运行。有谁知道如何让它在片段中工作?以下是错误的屏幕截图: http://tools.android.com/tech-docs/configuration
以下是我的片段的代码:
public class LoginFragment extends Fragment implements View.OnClickListener {
private FragmentTransaction ft;
private Button registerButton, resetButton, loginButton;
EditText userName, password;
private boolean isPopUpOpen;
BackendlessUser userOne = new BackendlessUser();
private static final String PREFS_LOGGED_IN = "AreYouLoggedInFile";
public OnClickedListener listener;
public LogInInterface loggedInListener;
static interface OnClickedListener{
public void buttonClicked(View v);
}
static interface LogInInterface{
public void userLoggedIn(boolean loggedIn);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.listener = (OnClickedListener)activity;
this.loggedInListener = (LogInInterface)activity;
}
public LoginFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
isPopUpOpen = false;
if (savedInstanceState!=null){
if (savedInstanceState.getBoolean("isDialogOpen")){
resetPopUpWindow();
}
}
View view = inflater.inflate(R.layout.fragment_login, container, false);
registerButton = (Button)view.findViewById(R.id.register_button);
resetButton = (Button) view.findViewById(R.id.reset_button);
password = (EditText)view.findViewById(R.id.fragment_login_password);
userName = (EditText)view.findViewById(R.id.fragment_login_username);
loginButton = (Button)view.findViewById(R.id.fragment_login_loginButton);
registerButton.setOnClickListener(this);
resetButton.setOnClickListener(this);
loginButton.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.register_button:{
listener.buttonClicked(v);
break;
}
case R.id.reset_button:{
isPopUpOpen = true;
resetPopUpWindow();
break;
}
case R.id.fragment_login_loginButton:{
final ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Logging In...");
progressDialog.show();//FOLLOWING METHOD NOT WORKING...
Backendless.UserService.login("email", password, new AsyncCallback<BackendlessUser>() {
public void handleResponse(BackendlessUser user) {
Toast.makeText(getActivity(), "Logged In!", Toast.LENGTH_LONG).show();
SharedPreferences myPrefs = getActivity().getSharedPreferences(PREFS_LOGGED_IN, 0);
SharedPreferences.Editor editor = myPrefs.edit();
editor.putBoolean("isLoggedIn", true);
editor.commit();
}
public void handleFault(BackendlessFault fault) {
Toast.makeText(getActivity(), "No Name", Toast.LENGTH_LONG).show();
}
});
}
break;
}
答案 0 :(得分:3)
login()
的{{1}}方法需要Backendless
作为第二个参数。你传递的是String
。您必须从EditText
中提取值。将EditText
作为password.getText().toString()
方法的第二个参数。
并且,在实际发送值之前检查EditText中的空值是一个很好的做法。所以一定要做那些检查。
答案 1 :(得分:1)
正如这个website提到的,登录方法的语法是这样的:
public void Backendless.UserService.login( String login,
String password,
AsyncCallback<BackendlessUser> callback );
public void Backendless.UserService.login( String login,
String password,
boolean stayLoggedIn,
AsyncCallback<BackendlessUser> callback );
您传递的是EditText
而不是String
。所以替换这个:
Backendless.UserService.login("email", password, new AsyncCallback<BackendlessUser>() {
public void handleResponse(BackendlessUser user) {
Toast.makeText(getActivity(), "Logged In!", Toast.LENGTH_LONG).show();
SharedPreferences myPrefs = getActivity().getSharedPreferences(PREFS_LOGGED_IN, 0);
SharedPreferences.Editor editor = myPrefs.edit();
editor.putBoolean("isLoggedIn", true);
editor.commit();
}
public void handleFault(BackendlessFault fault) {
Toast.makeText(getActivity(), "No Name", Toast.LENGTH_LONG).show();
}
});
用这个:
Backendless.UserService.login("email", password.getText().toString(), new AsyncCallback<BackendlessUser>() {
public void handleResponse(BackendlessUser user) {
Toast.makeText(getActivity(), "Logged In!", Toast.LENGTH_LONG).show();
SharedPreferences myPrefs = getActivity().getSharedPreferences(PREFS_LOGGED_IN, 0);
SharedPreferences.Editor editor = myPrefs.edit();
editor.putBoolean("isLoggedIn", true);
editor.commit();
}
public void handleFault(BackendlessFault fault) {
Toast.makeText(getActivity(), "No Name", Toast.LENGTH_LONG).show();
}
});
希望它有所帮助!!!