Facebook个人资料图片未显示在导航标题中?
我有一个简单NavigationView
中,我试图表明我的资料图片。这是我到目前为止有:Facebook个人资料图片未显示在导航标题中?
MainActivity.java:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Profile profile = Profile.getCurrentProfile();
System.out.println("Profile Id: " + profile.getId());
ProfilePictureView profilePictureView = (ProfilePictureView) findViewById(R.id.profile_image);
if(profilePictureView != null) {
System.out.println("Not null.");
System.out.println("Profile Name: " + profile.getFirstName());
profilePictureView.setProfileId(profile.getId());
}
else {
System.out.println("Null.");
}
}
}
main_activity.xml:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" tools:openDrawer="start">
<include layout="@layout/app_bar_news_feed" android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView android:id="@+id/nav_view"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_gravity="start" android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_news_feed">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/nav_header_news_feed"
android:id="@+id/navigation_header"/>
<ListView
android:id="@+id/friends_list"
android:layout_weight="7"
android:layout_width="match_parent"
android:layout_height="0dp"></ListView>
</LinearLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
nav_header_news_feed.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:background="@drawable/side_nav_bar"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
android:orientation="vertical"
android:gravity="bottom">
<com.facebook.login.widget.ProfilePictureView
android:id="@+id/profile_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
facebook:com_facebook_preset_size="small"/>
</LinearLayout>
我很困惑,因为我认为我只需实例化profilePictureView
对象并设置其配置文件ID。我以为我做的一切都正确,因为名字与登录的用户相匹配,并且我确保用户ID不为空。
相反,画面只是看起来像一个空的Facebook档案照片时,在现实中,我知道我有一张照片。如果有人能指出我的方向正确,那会很棒。谢谢!
编辑:添加在编辑我的文件,以反映什么马蒂亚MAESTRINI建议:
MainActivity.java:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private NavigationView navigationView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View header = LayoutInflater.from(this).inflate(R.layout.nav_header_news_feed, null);
navigationView.addHeaderView(header);
Profile profile = Profile.getCurrentProfile();
System.out.println("Profile Id: " + profile.getId());
ProfilePictureView profilePictureView = (ProfilePictureView) header.findViewById(R.id.profile_image);
if(profilePictureView != null) {
System.out.println("Not null.");
System.out.println("Profile Name: " + profile.getFirstName());
profilePictureView.setProfileId(profile.getId());
}
else {
System.out.println("Null.");
}
}
}
main_activity.xml:
<include layout="@layout/app_bar_news_feed" android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView android:id="@+id/nav_view"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_gravity="start" android:fitsSystemWindows="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/nav_header_news_feed"
android:id="@+id/navigation_header"/>
<ListView
android:id="@+id/friends_list"
android:layout_weight="7"
android:layout_width="match_parent"
android:layout_height="0dp"></ListView>
</LinearLayout>
</android.support.design.widget.NavigationView>
的NavigationView
在支持库v23.1.0一个bug。您无法访问到onCreate
方法包含在HeaderView
的意见。
一种解决方法是吹大HeaderView
和编程添加它。从main_activity.xml
删除app:headerLayout
,然后更改onCreate
这样的:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Profile profile = Profile.getCurrentProfile();
System.out.println("Profile Id: " + profile.getId());
View header = LayoutInflater.from(this).inflate(R.layout.nav_header_news_feed, null);
navigationView.addHeaderView(header);
ProfilePictureView profilePictureView = (ProfilePictureView) header.findViewById(R.id.profile_image);
if(profilePictureView != null) {
System.out.println("Not null.");
System.out.println("Profile Name: " + profile.getFirstName());
profilePictureView.setProfileId(profile.getId());
}
else {
System.out.println("Null.");
}
}
}
编辑:既然你已经添加页眉与包括标签:
<include layout="@layout/nav_header_news_feed"
android:id="@+id/navigation_header"/>
可以删除头的习俗通胀MainActivity.java
:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Profile profile = Profile.getCurrentProfile();
System.out.println("Profile Id: " + profile.getId());
ProfilePictureView profilePictureView = (ProfilePictureView) findViewById(R.id.profile_image);
if(profilePictureView != null) {
System.out.println("Not null.");
System.out.println("Profile Name: " + profile.getFirstName());
profilePictureView.setProfileId(profile.getId());
}
else {
System.out.println("Null.");
}
}
}
而且你也不需要指定app:headerLayout
在main_activity.xml
:
<include
layout="@layout/app_bar_news_feed"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/nav_header_news_feed"
android:id="@+id/navigation_header"/>
<ListView
android:id="@+id/friends_list"
android:layout_weight="7"
android:layout_width="match_parent"
android:layout_height="0dp"></ListView>
</LinearLayout>
</android.support.design.widget.NavigationView>
嗨,不幸的是,这种方法没有奏效。我遵循了你的所有指示。我在上面的回答中添加了编辑,以显示我做了什么。我验证了我的个人资料ID等不为空。 – user1871869
我刚才注意到你在'NavigationView'里面有'LinearLayout',请问为什么?通过这种方式,您有两个标题和两个'ProfilePictureView',这是导致错误的原因,您将图像设置为不可见的图像。删除'LinearLayout',它应该可以工作。 –
如果我删除第一个'LinearLayout',我无法得到我想要显示的列表视图,它位于我的导航标题下方。如果我删除布局的“include”,我无法显示导航标题。 'nav_header_news_feed.xml'还需要一个'LinearLayout'用于'xmlns:facebook =“http://schemas.android.com/apk/res-auto”'命名空间来改变图片的大小。 – user1871869
有你写的这些'FacebookSdk.sdkInitialize(背景); callbackManager = CallbackManager.Factory.create();在获得AccessToken之前在onCreate方法中? –
@rushankshah是的,我有,但我已经在我的登录活动其中,在用户登录后,进入到MainActivity.java这样做。如果我没有这样做,AccessToken将为空。我是否也需要在这个文件中调用这些函数? – user1871869
是您的令牌无效或有登录后的价值? –