我试图从登录按钮获取facebook测试用户的生日,然后将其传递给firestore文档。所以,我创建了一个UserInfo类,它将生日和id都保存为字符串,这样我就可以随时获取和设置它们的值。然而,当我从facebook JSON获取生日值并将其设置为userinfo时,它可以工作,但是当我尝试从getter获取此值时,我得到一个null对象。我的代码: 注意,我已经初始化了用户信息onStar。 这是我的代码:
about:blank
UserInfo.class
package com.example.saif.dammi;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.facebook.AccessToken;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.HttpMethod;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.firebase.auth.FirebaseAuth;
//import com.google.firebase.database.DatabaseReference;
//import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.firestore.FirebaseFirestore;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class SignupActivity extends AppCompatActivity {
private TextInputLayout mFullName;
private RadioGroup mRadioButton;
private Button mRegister;
private FirebaseAuth mAuth;
private UserInfo userInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
// Access a Cloud Firestore instance from your Activity
final FirebaseFirestore db = FirebaseFirestore.getInstance();
// final String[] bD = new String[1];
userInfo = new UserInfo();
mAuth = FirebaseAuth.getInstance();
mFullName = (TextInputLayout)findViewById(R.id.name_edittext);
mRadioButton = (RadioGroup) findViewById(R.id.bloodGroup);
mRegister = (Button)findViewById(R.id.register_btn);
mRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int selectedId = mRadioButton.getCheckedRadioButtonId();
final RadioButton mBloodGroup = (RadioButton)findViewById(selectedId);
if(mBloodGroup.getText() == null){
Toast.makeText(SignupActivity.this, "Please select your Blood Group!", Toast.LENGTH_SHORT).show();
return;
}
final String name = mFullName.getEditText().getText().toString();
if (mAuth.getCurrentUser().getUid()!=null) {
String userId = mAuth.getCurrentUser().getUid();
String userEmail = mAuth.getCurrentUser().getEmail();
Uri userPid = mAuth.getCurrentUser().getPhotoUrl();
/* make the API call */
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
public void onCompleted(JSONObject object,GraphResponse response) {
/* handle the result */
try{
userInfo.setBirthday(object.getString("birthday"));
// bD[0] = object.getString("birthday");
Log.d("Birthday", ""+ userInfo.getBirthday());
//
}catch (JSONException e){
e.printStackTrace();
};
}
}
);
Bundle parameters = new Bundle();
parameters.putString("fields","id,email,friends,birthday");
request.setParameters(parameters);
request.executeAsync();
Log.d("Birthday1", ""+ userInfo.getBirthday());
final Map<String, Object> dataToAdd = new HashMap<>();
// dataToAdd.put("Birthday", bD[0]);
dataToAdd.put("name", name);
dataToAdd.put("id", userId);
dataToAdd.put("Pid", userPid.toString());
dataToAdd.put("email", userEmail);
dataToAdd.put("bloodGroup", mBloodGroup.getText().toString());
dataToAdd.put("registered", true);
db.collection("Users").document(mAuth.getCurrentUser().getUid()).set(dataToAdd).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(SignupActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
// DatabaseReference currentUserId = FirebaseDatabase.getInstance().getReference().child("Users").child(mBloodGroup.getText().toString()).child(userId).child("Name").child("Registred");
// currentUserId.setValue(name);
// currentUserId.child("Registred").setValue(true);
Intent startIntent = new Intent(SignupActivity.this, MainActivity.class);
startActivity(startIntent);
finish();
}
}
});
}
}
日志: Logs
答案 0 :(得分:0)
首先,您需要将no-argument constructor
添加到模型类中,如下所示:
public UserInfo() {}
所有JavaBean类型对象都应该有一个,因此可以使用反射来创建它的实例,而不必猜测如何正确地将参数传递给它。
其次,您不能将UserInfo
类型的变量声明为全局变量,只需在onCompleted()
方法之外使用它。发生这种情况是因为此方法具有异步行为,这意味着甚至在从数据库获取数据之前就会调用此方法。快速解决方法是仅在UserInfo
方法中使用onCompleted()
对象,或者如果您想在外面使用它,请使用此 post的答案的最后一部分即可。您还可以查看 video 。