Facebook个人资料图片未显示在导航标题中?

时间:2015-11-03 08:07:23

标签: java android xml facebook

我有一个简单的NavigationView我尝试显示我的个人资料图片。这就是我到目前为止所做的:

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个人资料图片,而实际上我知道我有一张照片。如果有人能指出我正确的方向,那将是伟大的。谢谢!

编辑:添加了对我的文件的编辑,以反映Mattia 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>

1 个答案:

答案 0 :(得分:1)

NavigationView在v23.1.0的支持库中有一个bug。您无法访问HeaderView方法中onCreate中包含的观看次数。

解决方法是对HeaderView进行通知并以编程方式添加它。从app:headerLayout移除main_activity.xml,然后像这样更改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标记添加标题:

<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>