2015-11-03 169 views
2

我有一個簡單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> 

+0

有你寫的這些'FacebookSdk.sdkInitialize(背景); callbackManager = CallbackManager.Factory.create();在獲得AccessToken之前在onCreate方法中? –

+0

@rushankshah是的,我有,但我已經在我的登錄活動其中,在用戶登錄後,進入到MainActivity.java這樣做。如果我沒有這樣做,AccessToken將爲空。我是否也需要在這個文件中調用這些函數? – user1871869

+0

是您的令牌無效或有登錄後的價值? –

回答

1

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:headerLayoutmain_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> 
+0

嗨,不幸的是,這種方法沒有奏效。我遵循了你的所有指示。我在上面的回答中添加了編輯,以顯示我做了什麼。我驗證了我的個人資料ID等不爲空。 – user1871869

+0

我剛纔注意到你在'NavigationView'裏面有'LinearLayout',請問爲什麼?通過這種方式,您有兩個標題和兩個'ProfilePictureView',這是導致錯誤的原因,您將圖像設置爲不可見的圖像。刪除'LinearLayout',它應該可以工作。 –

+0

如果我刪除第一個'LinearLayout',我無法得到我想要顯示的列表視圖,它位於我的導航標題下方。如果我刪除佈局的「include」,我無法顯示導航標題。 'nav_header_news_feed.xml'還需要一個'LinearLayout'用於'xmlns:facebook =「http://schemas.android.com/apk/res-auto」'命名空間來改變圖片的大小。 – user1871869

相關問題