我正在使用Facebook登录我使用firebase的应用程序。当用户登录时,该应用程序应该从Facebook上获取诸如姓名、电子邮件、性别、位置、简历等信息。在我升级到AndroidStudio 2.2之前,它工作得很好。现在在更新之后,我得到了错误
尝试调用虚拟方法“java”。字符串组织。json。JSONObject。空对象引用上的getString(java.lang.String)”。
帐户保存到Firebase,但登录后应用程序崩溃。我不知道这是模拟器的茴香(Nexus 6 API 23),还是我的代码出了问题。
Logcat
10-27 08:23:07.965 15474-15474/com.rograb.workorderapp D/LOGIN DEBUG:facebook:onSuccess:com.facebook.login.LoginResult@47060b7
10-27 08:23:07.965 15474-15474/com.rograb.workorderapp D/LOGIN DEBUG: handleFacebookAccessToken:{AccessToken token:ACCESS_TOKEN_REMOVED permissions:[public_profile, contact_email, user_friends, user_likes, email, user_location]}
10-27 08:23:07.967 15474-15487/com.rograb.workorderapp W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
10-27 08:23:07.971 15474-16534/com.rograb.workorderapp V/FA: Activity resumed, time: 1301956
10-27 08:23:07.973 15474-15487/com.rograb.workorderapp W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
10-27 08:23:07.986 15474-15533/com.rograb.workorderapp E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb2c3e080
10-27 08:23:08.366 15474-15474/com.rograb.workorderapp D/AndroidRuntime: Shutting down VM
10-27 08:23:08.366 15474-15474/com.rograb.workorderapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.rograb.workorderapp, PID: 15474
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.getString(java.lang.String)' on a null object reference
at com.rograb.workorderapp.models.UserModel.fromJSON(UserModel.java:117)
at com.rograb.workorderapp.activities.LoginActivity$2$1.onCompleted(LoginActivity.java:106)
at com.facebook.GraphRequest$1.onCompleted(GraphRequest.java:304)
at com.facebook.GraphRequest$5.run(GraphRequest.java:1383)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-27 08:23:11.904 15474-15474/com.rograb.workorderapp I/Process: Sending signal. PID: 15474 SIG: 9
用户模型
public class UserModel {
private String id;
private String name;
private String email;
private String gender;
private String locationId;
private String locationName;
private String bio;
private String languages;
private ArrayList<TripModel> favorites = new ArrayList<>();
private HashMap<String, Object> timestampJoined;
public UserModel() {
}
public UserModel(String name, String email, HashMap<String, Object> timestampJoined) {
this.name = name;
this.email = email;
this.timestampJoined = timestampJoined;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getLocationId() {
return locationId;
}
public void setLocationId(String locationId) {
this.locationId = locationId;
}
public String getLocationName() {
return locationName;
}
public void setLocationName(String locationName) {
this.locationName = locationName;
}
public String getBio() {
return bio;
}
public void setBio(String bio) {
this.bio = bio;
}
public String getLanguages() {
return languages;
}
public void setLanguages(String languages) {
this.languages = languages;
}
public HashMap<String, Object> getTimestampJoined() {
return timestampJoined;
}
public void setTimestampJoined(HashMap<String, Object> timestampJoined) {
this.timestampJoined = timestampJoined;
}
public ArrayList<TripModel> getFavorites() {
return favorites;
}
public void setFavorites(ArrayList<TripModel> favorites) {
this.favorites = favorites;
}
public static UserModel fromJSON(JSONObject jsonObject){
UserModel user = new UserModel();
try {
user.id = jsonObject.getString("id");
user.name = jsonObject.getString("name");
user.email = jsonObject.getString("email");
user.gender = jsonObject.getString("gender");
user.locationId = jsonObject.getJSONObject("location").getString("id");
user.locationName = jsonObject.getJSONObject("location").getString("name");
user.languages = "";
} catch (JSONException e) {
e.printStackTrace();
}
return user;
}
}
登陆界面
public class LoginActivity extends AppCompatActivity {
private static final String TAG = "LOGIN DEBUG";
@BindView(R.id.btnFacebookLogin)
LoginButton btnFacebookLogin;
CallbackManager callbackManager;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
UserModel userModel = new UserModel();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
final WorkOrderApplication app = (WorkOrderApplication) getApplicationContext();
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
if (user.getEmail() == null || user.getEmail().equals(""))
user.updateEmail(userModel.getEmail());
if (user.getUid() != null) {
setAuthenticatedUserFacebook(firebaseAuth);
onLoginSuccess();
}
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
}
};
// Initialize Facebook Login button
callbackManager = CallbackManager.Factory.create();
btnFacebookLogin.setReadPermissions(Arrays.asList("public_profile", "email", "user_location", "user_friends", "user_likes"));
btnFacebookLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "facebook:onSuccess:" + loginResult);
handleFacebookAccessToken(loginResult.getAccessToken());
// App code
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
userModel = UserModel.fromJSON(object);
app.setUserInfo(userModel);
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,location,bio,languages");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
Log.d(TAG, "facebook:onCancel");
}
@Override
public void onError(FacebookException error) {
Log.d(TAG, "facebook:onError", error);
}
});
}
private void setAuthenticatedUserFacebook(FirebaseAuth firebaseAuth) {
final String userEncondedEmail = Utilities.encodeEmail(firebaseAuth.getCurrentUser().getEmail());
String userName = firebaseAuth.getCurrentUser().getDisplayName();
HashMap<String, Object> timestampJoined = new HashMap<>();
timestampJoined.put(Constants.FIREBASE_PROPERTY_TIMESTAMP, ServerValue.TIMESTAMP);
final WorkOrderApplication app = (WorkOrderApplication) getApplicationContext();
//Save user in firebase
final UserModel user = new UserModel(userName, userEncondedEmail, timestampJoined);
app.getUsersReference().child(userEncondedEmail).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue() == null) {
app.getUsersReference().child(userEncondedEmail).setValue(user);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
public void onLoginSuccess() {
Intent i = new Intent(this, JobListActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish();
}
private void handleFacebookAccessToken(AccessToken token) {
Log.d(TAG, "handleFacebookAccessToken:" + token);
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(LoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
} else {
onLoginSuccess();
}
}
});
}
}
建筑格拉德尔
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "com.rograb.workorderapp"
minSdkVersion 16
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.0.0'
testCompile 'junit:junit:4.12'
apt 'com.jakewharton:butterknife-compiler:8.2.1'
apt 'org.parceler:parceler:1.1.1'
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:design:24.2.0'
compile 'com.android.support:cardview-v7:24.2.0'
compile 'com.android.support:recyclerview-v7:24.2.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.google.firebase:firebase-auth:9.4.0'
compile 'com.facebook.android:facebook-android-sdk:[4,5)'
compile 'com.jakewharton:butterknife:8.2.1'
compile 'org.parceler:parceler-api:1.1.1'
compile 'com.google.firebase:firebase-database:9.4.0'
compile 'com.firebaseui:firebase-ui-database:0.5.1'
compile 'com.google.android.gms:play-services-location:9.4.0'
compile 'com.google.android.gms:play-services-maps:9.4.0'
compile 'com.google.android.gms:play-services-places:9.4.0'
compile 'com.firebase:geofire-android:2.1.0'
compile 'com.github.qiugang:EditTag:v1.2.3'
compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
compile 'com.android.support:support-v4:24.2.0'
}
apply plugin: 'com.google.gms.google-services'
配置文件
public class ProfileActivity extends AppCompatActivity implements OnMapReadyCallback {
WorkOrderApplication app;
private GoogleMap mMap;
@BindView(R.id.ivProfile)
ProfilePictureView ivProfile;
@BindView(R.id.tvBio)
TextView tvBio;
@BindView(R.id.tvEmail)
TextView tvEmail;
@BindView(R.id.app_bar)
AppBarLayout appBar;
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.tvUserName)
TextView tvUserName;
@BindView(R.id.tvLocationName)
TextView tvLocationName;
@BindView(R.id.toolbar_layout)
CollapsingToolbarLayout collapsingToolbarLayout;
@BindView(R.id.tvLanguages)
TextView tvLanguages;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
app = (WorkOrderApplication) getApplicationContext();
}
@Override
public void onBackPressed() {
finish();
overridePendingTransition(R.anim.left_in, R.anim.right_out);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
setUserInformation();
}
private void setUserInformation() {
UserModel user = app.getUserInfo();
ivProfile.setProfileId(user.getId());
tvUserName.setText(user.getName());
tvLocationName.setText(user.getLocationName());
tvBio.setText(user.getBio());
if (user.getBio().equals(Constants.EMPTY_STRING)) {
tvBio.setVisibility(View.GONE);
}
tvEmail.setText(user.getEmail());
//tvLanguages.setText(user.getLanguages());
//Set marker in User location
LatLng userLatLong = getUserLatLong(user.getLocationName());
mMap.addMarker(new MarkerOptions()
.position(userLatLong)
.icon(BitmapDescriptorFactory.fromBitmap(Utilities.writeTextOnDrawable(this, R.drawable.user_marker, "")))
.title(user.getLocationName()));
mMap.moveCamera(CameraUpdateFactory.newLatLng(userLatLong));
mMap.animateCamera(CameraUpdateFactory.zoomTo(5.0f));
//Collapsing Toolbar options
collapsingToolbarLayout.setExpandedTitleColor(Color.TRANSPARENT);
//collapsingToolbarLayout.setTitle(user.getName());
collapsingToolbarLayout.setTitle(" ");
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBar.getLayoutParams();
AppBarLayout.Behavior behavior = new AppBarLayout.Behavior();
behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
@Override
public boolean canDrag(@NonNull AppBarLayout appBarLayout) {
return false;
}
});
params.setBehavior(behavior);
}
private LatLng getUserLatLong(String locationName) {
LatLng userLatLong = null;
if (Geocoder.isPresent()) {
try {
Geocoder gc = new Geocoder(this);
List<Address> addresses = gc.getFromLocationName(locationName, 1); // get the found Address Objects
for (Address a : addresses) {
if (a.hasLatitude() && a.hasLongitude()) {
userLatLong = new LatLng(a.getLatitude(), a.getLongitude());
}
}
} catch (IOException e) {
Log.e("Geocoder Error", e.toString());
}
}
return userLatLong;
}
}
问题是Facebook更新了他们的权限,我被要求提交审查以获得user_location。这就是为什么它开始工作,然后突然停止。
异常表示您正试图取消对空值的引用。
根据代码判断,我认为您的JSON对象中没有“位置”,因此此行失败:
jsonObject.getJSONObject("location").getString("id");
stacktrace在这里非常有用。
at com.rograb.workorderapp.models.UserModel.fromJSON(UserModel.java:117)
这告诉你你的代码在第117行中断了。
大多数编辑器都有“跳转到行”功能,即行编号。在Eclipse中,可以使用Ctrl-L。
这里有一个有趣的提示:
在Eclipse中,您可以打开一个新的“Java堆栈跟踪”控制台。从日志中复制堆栈跟踪,并将其粘贴到Java堆栈跟踪控制台中。
然后,您只需单击stacktrace文本,它就会直接将您带到有问题的行!
问题内容: 我收到以下错误 尝试在空对象引用上调用虚拟方法’void android.widget.StackView.setAdapter(android.widget.Adapter)’ 在这条线上 完整的片段 EventsFragment.java 是 Stack_Adapter.java Stack_Items 问题答案: 您正在执行: 你是。返回。 你为什么用? 在哪里?您应该从正确的x
我的应用程序有问题,想解决它,但我无法访问解决方案,请帮帮我,,
问题内容: 我尝试使用片段来打开数据库,但是,当我单击按钮开始搜索时,程序意外终止,并显示如下错误: 主片段: DBManager类: 顺便说一句,我在MainAcitivity中使用了有关DBManager的代码,并且成功了。将代码复制到上面的片段后,它失败了,该怎么办? 问题答案: 之前和之后你都做不到。 因为,您正在执行片段实例化时。该方法将始终返回null。另外,尽量不要将参考文献保留在您
问题内容: 我正在尝试将播放器的名称保存为共享的首选项,并通过再次以共享的首选项获取它的名称来使其显示在另一个活动中,但是我的应用程序崩溃了。 致命异常:主要 代码: 播放器 PlayGame.java PlayerName.java 问题答案: 您的应用在以下位置崩溃: 因为。 您忘记在PlayGame活动中进行初始化。
我面临着这个错误: 主要活动: 谢谢你的帮助。
我尝试使用一个片段来打开一个数据库,但是,当我单击按钮开始搜索时,程序意外地终止,它显示这样的错误: 主要片段: 类DBManager: 顺便说一句,我在MainActivity中使用了关于DBManager的代码,它成功了。在我将代码复制到上面的片段后,它失败了。我该怎么办?