2016-11-29 1 views
-3

In meiner einfachen App habe ich eine TextView auf einem NavigationView und ich habe diese Ausnahme: java.lang.NullPointerException: Versuch, virtuelle Methode 'void android.widget.TextView.setVisibility (int) aufzurufen 'auf eine Nullobjektreferenz. Ich verstehe nicht, weil es mir sagt, dass Methode für ein Null-Objekt aufrufen. Code: android Nullzeiger Ausnahme auf Textansicht

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

Sie schreiben setContentView (R.layout.activity_main); Sie beziehen sich also auf das Layout activity_main.xml, aber der Name befindet sich in nav_header_main.xml, sodass er nicht gefunden werden kann und null zurückgibt. –

Antwort

1

Ich glaube, Sie könnten Ihre TextView vom NavigationView finden müssen.

so versuchen

info = (TextView) findViewById(R.id.name);

zu

info = (TextView) navigationView.findViewById(R.id.name);

+0

Danke für Sie antworten; Ich versuche, diese Zeile zu ändern, aber die Ausnahme ist immer noch vorhanden –

0

Die Ursache zu ändern ist, dass Sie nicht über TexView mit id name in Ihrem xml von R.layout.activity_main Bezug genommen wird, so ist dies finden schlägt fehl:

info = (TextView) findViewById(R.id.name); 
+0

Danke für Ihre Antwort; Ja, aber ich habe dies in der Datei nav_header_main, dass ich in activitymain –

+0

ich sehe, aber nicht in "activity_main.xml" aufrufen. Daher müssen Sie @raxelsson-Ratschläge implementieren. –