[英]Facebook Profile Picture not showing up in Navigation Header?
I have a simple NavigationView
in which I try to show my profile picture.我有一个简单的
NavigationView
,我尝试在其中显示我的个人资料图片。 This is what I have so far:这是我到目前为止:
MainActivity.java:主活动.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: 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: 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>
I am very confused because I thought that I had to simply instantiate the profilePictureView
object and set its profile id.我很困惑,因为我认为我必须简单地实例化
profilePictureView
对象并设置其配置文件 ID。 I thought I was doing everything right because the first name matches with the user that is logged in, and I have made sure that the user ID is not null.我以为我做的一切都是正确的,因为名字与登录的用户匹配,并且我确保用户 ID 不为空。
Rather, the picture just looks like an empty Facebook profile picture when in reality I know I have a photo.相反,这张照片看起来就像一张空的 Facebook 个人资料照片,而实际上我知道我有一张照片。 If anyone could point me in the right direction, that would be great.
如果有人能指出我正确的方向,那就太好了。 Thanks!
谢谢!
EDIT: Added in edits to my files to mirror what Mattia Maestrini suggested:编辑:在我的文件中添加编辑以反映 Mattia Maestrini 的建议:
MainActivity.java:主活动.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: 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>
The NavigationView
has a bug in v23.1.0 of support library. NavigationView
在支持库的 v23.1.0 中有一个错误。 You can't access to the views contained in the HeaderView
in onCreate
method.您无法在
onCreate
方法中访问HeaderView
中包含的视图。
A workaround is to inflate the HeaderView
and add it programatically.一种解决方法是膨胀
HeaderView
并以编程方式添加它。 Remove app:headerLayout
from main_activity.xml
and then change onCreate
like this:从
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.");
}
}
}
EDIT : Since you already add the header with the include tag:编辑:由于您已经添加了包含标签的标题:
<include layout="@layout/nav_header_news_feed"
android:id="@+id/navigation_header"/>
You can remove the custom inflation of the header in MainActivity.java
:您可以在
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.");
}
}
}
And you don't need to specify the app:headerLayout
in main_activity.xml
:而且您不需要在
main_activity.xml
指定app:headerLayout
:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.