所以我是Android Studio的新手,还在学习。我正在尝试创建一个允许用户与他的Facebook连接的应用程序,一旦他登录,他就可以转到个人资料页面。以下示例中的facebook UserID会在成功时传递给类配置文件。然而,在用facebook登录后,我的应用程序崩溃了..我做错了什么?
public class MainActivity extends AppCompatActivity {
LoginButton loginButton;
TextView textView;
String s;
CallbackManager callbackManager;
Realm realm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
loginButton = (LoginButton) findViewById(R.id.login_button);
textView = (TextView) findViewById(R.id.Textview);
callbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
//textView.setText("Login success "+loginResult.getAccessToken().getUserId() +"\n"+loginResult.getAccessToken().getToken());
s = loginResult.getAccessToken().getUserId();
Intent i= new Intent(MainActivity.this,profiles.class);
i.putExtra("Id",s);
startActivity(i);
}
@Override
public void onCancel() {
textView.setText("Login Failed");
}
@Override
public void onError(FacebookException error) {
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode,resultCode,data);
}
}
配置文件部分基本上由3个edittexts组成,允许用户输入他的信息并将其存储在领域数据库中
public class profiles extends AppCompatActivity {
EditText username,age,country;
Realm realm;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
Bundle bundle= getIntent().getExtras();
final String userid;
userid = bundle.getString("Id");
username= (EditText) findViewById(R.id.username);
age= (EditText) findViewById(R.id.age);
country= (EditText) findViewById(R.id.country);
Realm.init(this);
realm = Realm.getDefaultInstance();
findViewById(R.id.Continue).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
realm.beginTransaction();
Users obj= realm.createObject(Users.class);
obj.setUsername(username.getText().toString());
obj.setCountry(country.getText().toString());
obj.setAge(age.getText().toString());
obj.setUserId(userid);
realm.commitTransaction();
}
});
}
}
这里是使用的getter和setter的用户类,
public class Users extends RealmObject {
public void setUserId(String userId) {
UserId= userId;
}
public void setUsername(String username) {
Username = username;
}
public void setCountry(String country) {
Country = country;
}
String UserId;
String Username;
String Country;
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
String age;
public String getUsername() {
return Username;
}
public String getCountry() {
return Country;
}
public String getUserId() {
return UserId;
}
}