繁体   English   中英

空指针异常:尝试从Facebook获取用户位置时

[英]Null Pointer Exception: when trying to get user location from Facebook

我正在尝试从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:-

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();
                }
            }
        });

我想我正面临这个问题,因为not given Location details on Facebook ,但是如果用户not provided to Facebook ,我想显示“地址未提及”以及其他详细信息。

分隔String strLocation = user.getLocation().toString(); 在此:

Object userLocation = user.getLocation();
String strLocation = (userLocation != null)? userLocation.toString() : "Address not mentioned";

我不知道Location对象的类型,但是如果user.getLocation()返回null ,则您的代码将带有java.lang.NullPointerException

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM