在我的简单应用程序中,我在NavigationView上有一个textView,我有这个异常:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.TextView.setVisibility(int)'。我不明白,因为它告诉我在null对象上调用方法。代码:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, ReadRssListener {
RecyclerView recyclerView;
private PrefUtil prefUtil;
private ImageView profileImgView;
private TextView info;
AccessTokenTracker accessTokenTracker;
LoginButton loginButton;
SwipeRefreshLayout refreshLayout;
private CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
callbackManager = CallbackManager.Factory.create();
prefUtil = new PrefUtil(this);
profileImgView = (ImageView) findViewById(R.id.thumb_img);
info = (TextView) findViewById(R.id.name);
if(PrefUtils.getCurrentUser(MainActivity.this)==null)
{
info.setVisibility(View.INVISIBLE);
profileImgView.setBackgroundResource(R.drawable.wallpaper_for_facebook_profile_photo);
}
else
{
Log.e("sono loggato", "si");
info.setVisibility(View.VISIBLE);
profileImgView.setVisibility(View.VISIBLE);
User user= PrefUtils.getCurrentUser(MainActivity.this);
Log.e("urlImage", user.profileName);
Glide.with(MainActivity.this)
.load(user.imageUrl)
.into(profileImgView);
if (Profile.getCurrentProfile() != null) {
info.setText(Profile.getCurrentProfile().getName());}
}
mainactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<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_main"
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_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
nav_header_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
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">
<ImageView
android:id="@+id/thumb_img"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/wallpaper_for_facebook_profile_photo"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/name"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sports News"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="@+id/date_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
答案 0 :(得分:1)
我认为您可能需要从TextView
找到NavigationView
。
所以尝试改变
info = (TextView) findViewById(R.id.name);
到
info = (TextView) navigationView.findViewById(R.id.name);
答案 1 :(得分:0)
根本原因是您在name
引用的xml中没有标识为R.layout.activity_main
的TexView,因此此查找失败:
info = (TextView) findViewById(R.id.name);