1

Ich habe eine 2 Aktivität und einen Dienst, zuerst habe ich Navigation Schublade und 2 Textviews in seiner Kopfzeile, und ich muss ihren Text manchmal ändern, im Dienst gibt es einige Daten, die ich brauche
Auf den ersten Lauf ist alles perfekt, aber wenn ich von der Sekunde an zur ersten Aktivität gehe, habe ich in Textviews eine Null-Null-Wahrnehmung. Was verursacht es und wie kann es repariert werden?Navigation Schublade Header Textansicht nullpointerexception

//First 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    //Navigation drawer init 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarMain); 
    setSupportActionBar(toolbar); 
    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.addDrawerListener(toggle); 
    toggle.syncState(); 
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view_main); 
    navigationView.setNavigationItemSelectedListener(this); 

    intent = new Intent("playbackservice"); 
    sConn = new ServiceConnection() { 

     public void onServiceConnected(ComponentName name, IBinder binder) { 
      Log.d(LOG_TAG, "MainActivity onServiceConnected"); 
      service = ((PlaybackService.PlaybackServiceBinder) binder).getService(); 
      bound = true; 
      prepareLayout(); 
     } 

     public void onServiceDisconnected(ComponentName name) { 
      Log.d(LOG_TAG, "MainActivity onServiceDisconnected"); 
      bound = false; 
     } 
    }; 
} 

private void prepareLayout() { 
    Log.d(LOG_TAG, "prepareLayout: Running"); 
    ((TextView) findViewById(R.id.songNameNavhead)).setText(currentSong.getName()); 
    ((TextView) findViewById(R.id.artistNavhead)).setText(currentSong.getArtist()); 
} 

Und

//Layout 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:gravity="bottom" 
      android:orientation="vertical" 
      android:background="@color/colorPrimaryDark" 
      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"> 


<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="@string/default_song_name" 
    android:id="@+id/songNameNavhead"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/default_artist" 
    android:id="@+id/artistNavhead"/> 
</LinearLayout> 

Und

//activity_main.xml 
<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer_layout" 
    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: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_main" 
    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> 

Fehler

05-12 19:34:05.001 11086-11086/iskandarovlev.myitschool.ru.lyricplayer E/AndroidRuntime: FATAL EXCEPTION: main 
          Process: iskandarovlev.myitschool.ru.lyricplayer, PID: 11086 
          java.lang.NullPointerException 
          //MainActivity.java:302 is 
          //((TextView) header.findViewById(R.id.songNameNavhead)).setText(currentSong.getName()); 
          //this line 
          at iskandarovlev.myitschool.ru.lyricplayer.MainActivity.prepareLayout(MainActivity.java:302) 
          at iskandarovlev.myitschool.ru.lyricplayer.MainActivity.access$700(MainActivity.java:48) 
          at iskandarovlev.myitschool.ru.lyricplayer.MainActivity$3.onServiceConnected(MainActivity.java:355) 
          at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1140) 
          at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1157) 
          at android.os.Handler.handleCallback(Handler.java:808) 
          at android.os.Handler.dispatchMessage(Handler.java:103) 
          at android.os.Looper.loop(Looper.java:193) 
          at android.app.ActivityThread.main(ActivityThread.java:5542) 
          at java.lang.reflect.Method.invokeNative(Native Method) 
          at java.lang.reflect.Method.invoke(Method.java:515) 
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:657) 
          at dalvik.system.NativeStart.main(Native Method) 

Antwort

11

successfuly für Widget in Navigationsleiste per Kopf schauen, um, müssen Sie seine Kopfsicht erhalten, zuerst, die mit der Verwendung von getHeaderView() getan wird:

View header = ((NavigationView)findViewById(R.id.navigation_view)).getHeaderView(0); 

Dann mit header die auf der rechten ViewGroup können Sie regelmäßige findViewById() auf sie wie gewohnt verwenden:

TextView tv = header.findViewById(R.id.songNameNavhead); 
tv.setText(currentSong.getName()); 

Oder in kompakterer Weise:

((TextView)header.findViewById(R.id.songNameNavhead)) 
       .setText(currentSong.getName()); 
0
private void prepareLayout() { 
    Log.d(LOG_TAG, "prepareLayout: Running"); 
    ((TextView) findViewById(R.id.songNameNavhead)).setText(currentSong.getName()); 
    ((TextView) findViewById(R.id.artistNavhead)).setText(currentSong.getArtist()); 
} 

Diese müssen die navigationDrawer des vi verwenden ew und dann ist es Header, nicht Ansicht Ihre Tätigkeit des, so sollte es sein

View header = navigationView.getHeaderView(0) 

((TextView) navigationViewHeader.findViewById(R.id.songNameNavhead)).setText(currentSong.getName()); 
((TextView) navigationViewHeader.findViewById(R.id.artistNavhead)).setText(currentSong.getArtist()); 
+0

Nein, funktioniert nicht. Auch mein Code und dies funktioniert beim ersten Start, also kann es nicht falsch ID oder etwas anderes sein – Masafi

Verwandte Themen