[英]Null Pointer Exception: when trying to get user location from Facebook
I am trying to get user's location
from Facebook, but getting Null Pointer Exception
(NPE) and log
looks like this: 我正在尝试从Facebook获取
user's location
,但获取Null Pointer Exception
(NPE)和log
如下所示:
D/dalvikvm(3116): GC_FOR_ALLOC freed 413K, 15% free 2863K/3336K, paused 32ms, total 32ms
W/System.err(3116): java.lang.NullPointerException
W/System.err(3116): at com.example.loginfb.MainActivity$2.onUserInfoFetched(MainActivity.java:50)
W/System.err(3116): at com.facebook.widget.LoginButton$2.onCompleted(LoginButton.java:770)
W/System.err(3116): at com.facebook.Request$1.onCompleted(Request.java:280)
W/System.err(3116): at com.facebook.Request$4.run(Request.java:1658)
W/System.err(3116): at android.os.Handler.handleCallback(Handler.java:730)
W/System.err(3116): at android.os.Handler.dispatchMessage(Handler.java:92)
W/System.err(3116): at android.os.Looper.loop(Looper.java:137)
W/System.err(3116): at android.app.ActivityThread.main(ActivityThread.java:5103)
W/System.err(3116): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(3116): at java.lang.reflect.Method.invoke(Method.java:525)
W/System.err(3116): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
W/System.err(3116): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
W/System.err(3116): at dalvik.system.NativeStart.main(Native Method)
MainActivity.java:- MainActivity.java:-
public void onUserInfoFetched(GraphUser user) {
try {
if (user != null) {
String strName = user.getFirstName();
String strLocation = user.getLocation().toString();
textUserName.setText("Welcome, " + user.getName()) ;
textUserInformation.setText
(
"Name: " + strName + "\n"
+
"Location: " + strLocation
);
} else {
textUserName.setText("You are not Logged In");
textUserInformation.setText("");
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
I guess i am facing this issue, because has not given Location details on Facebook
, but i want to show "Address not mentioned" in case user not provided to Facebook
, along with other details. 我想我正面临这个问题,因为
not given Location details on Facebook
,但是如果用户not provided to Facebook
,我想显示“地址未提及”以及其他详细信息。
Divide String strLocation = user.getLocation().toString();
分隔
String strLocation = user.getLocation().toString();
in this: 在此:
Object userLocation = user.getLocation();
String strLocation = (userLocation != null)? userLocation.toString() : "Address not mentioned";
I don't know the type of the Location object but if user.getLocation()
returns null
, you will have a java.lang.NullPointerException
with your code. 我不知道Location对象的类型,但是如果
user.getLocation()
返回null
,则您的代码将带有java.lang.NullPointerException
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.