Java Facebook个人资料图片未显示在导航标题中?
我有一个简单的Java Facebook个人资料图片未显示在导航标题中?,java,android,xml,facebook,Java,Android,Xml,Facebook,我有一个简单的导航视图,在其中我尝试显示我的个人资料图片。这就是我到目前为止所做的: MainActivity.java: public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { protected void onCreate(Bundle savedInstanceState) { super.onC
导航视图
,在其中我尝试显示我的个人资料图片。这就是我到目前为止所做的:
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.");
}
}
}
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:
<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>
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>
导航视图
在v23.1.0中有一个支持库。您无法访问onCreate
方法中的HeaderView
中包含的视图
解决方法是将HeaderView
充气,然后按编程方式添加。从main\u 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"/>
您不需要在main\u 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>
您是否编写了这些FacebookSdk.sdkinInitialize(上下文);callbackManager=callbackManager.Factory.create()
在获取AccessToken之前是否在onCreate方法中?@rushankshah是的,但我在登录活动中这样做了,用户登录后,登录活动将转到MainActivity.java。如果我不这样做,AccessToken将为null。我是否也需要调用此文件中的这些函数?您的令牌是否为null或在登录后具有值?@rushankshah it具有值。我通过打印出存在的访问令牌用户id来确认它。如果它有令牌和用户id,那么它必须显示用户的图像。嗨,不幸的是,这个方法不起作用。我听从了你的指示。我在上面的答案中添加了编辑,以显示我所做的。我验证了我的配置文件id和其他id不为空。我刚刚注意到您在导航视图
中有一个线性布局
,我可以问一下原因吗?这样您就有了两个标题,其中有两个ProfilePictureView
,这就是导致错误的原因,您将图像设置为不可见的。删除LinearLayout
,它应该可以工作。如果删除第一个LinearLayout
,我将无法获取我想要显示的位于导航标题下方的列表视图。如果删除布局的包含
,则无法显示导航标题nav_header\u news\u feed.xml
还需要一个LinearLayout
用于xmlns:facebook=”http://schemas.android.com/apk/res-auto“
namespace来更改图片的大小。实际上我已经修复了它。谢谢你的帮助。由于您在上面指定的标题下方有一个Listview
,如何使Listview
显示在标题下方?我刚刚更新了答案。使用此方法,标题下方将有一个列表视图
。让我知道它是否适合你。
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.");
}
}
}
<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>