2017-10-02 4 views
0

Ich habe eine DrawerLayout in meiner Android-App, die eine NavigationView hat, die ein nav_header_main2-Layout verwendet. In der nav_header_main2-Layoutdatei befinden sich ein ImageView und 2 TextViews. In meiner MainActivity versuche ich, diese TextView-Elemente zu erfassen, sodass ich den Inhalt dynamisch mit dem Benutzernamen und der E-Mail-Adresse ausfüllen kann um auf sie zuzugreifen. Hier ist die Haupt Layout-Datei ...TextView kann nicht von MainActivity gefunden werden

<?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_main2" 
     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_main2" 
     app:menu="@menu/activity_main2_drawer"/> 

</android.support.v4.widget.DrawerLayout> 

Wie Sie sehen können, die NavigationView wird mit "nav_header_main2", ist hier die Datei ...

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    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"> 

    <de.hdodenhof.circleimageview.CircleImageView 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/menu_user_photo" 
     android:layout_width="64dp" 
     android:layout_height="64dp" 
     android:layout_gravity="center_horizontal" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:src="@drawable/ic_person_white" 
     app:border_color="@color/colorTransparent" 
     android:layout_marginStart="10dp" 
     android:layout_marginLeft="10dp" 
     android:layout_marginTop="3dp" 
     android:layout_marginBottom="0dp" 
     app:border_width="1dp"/> 

    <!--<ImageView--> 
     <!--android:id="@+id/imageView"--> 
     <!--android:layout_width="wrap_content"--> 
     <!--android:layout_height="wrap_content"--> 
     <!--android:paddingTop="@dimen/nav_header_vertical_spacing"--> 
     <!--app:srcCompat="@android:drawable/sym_def_app_icon"/>--> 

    <TextView 
     android:id="@+id/menu_username" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/nav_header_vertical_spacing" 
     android:layout_gravity="center_horizontal" 
     android:gravity="center_horizontal" 
     android:text="Android Studio" 
     android:textAppearance="@style/TextAppearance.AppCompat.Body1"/> 

    <TextView 
     android:id="@+id/textView" 
     android:gravity="center_horizontal" 
     android:layout_gravity="center_horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="[email protected]"/> 

</LinearLayout> 

Da ist in meinem MainActivity, ich habe mehrere Möglichkeiten des Zugriffs auf das Element "menu_username" versucht, aber alle geben eine Nullreferenz zurück. Hier ist meine Hauptklasse jetzt ...

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { 

    public static final String EXTRA_MESSAGE = "text.notreal.justatest.MESSAGE"; 
    public static final int TAG_KEY = 0; 
    public static final int TAG_VALUE = 0; 
    public List<Post> posts = new ArrayList<>(); 

    public FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

     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(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
     //THIS IS WHAT IS FAILING 
     //I HAVE ALSO TRIED JUST USING findViewById(R.id.menu_username); NULL STILL 
     TextView menuUserName = (TextView) navigationView.findViewById(R.id.menu_username); 
     if(menuUserName == null) { 
      Log.d("MAIN ACTIVITY", "Somehow the menu_username doesn't exist"); 
     } 
     if(findViewById(R.id.frameLayout) != null) { 

      if(savedInstanceState != null) { 
       return; 
      } 
      AllPostsFragment fragment = new AllPostsFragment(); 
      fragment.setArguments(getIntent().getExtras()); 
      getSupportFragmentManager().beginTransaction().add(R.id.frameLayout, fragment).commit(); 
      } 
     } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch(item.getItemId()) { 
      case android.R.id.home: 
       this.finish(); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

     @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_camera) { 
      // Handle the camera action 
     } else if (id == R.id.nav_gallery) { 

     } else if (id == R.id.nav_slideshow) { 

     } else if (id == R.id.nav_manage) { 

     } else if (id == R.id.nav_share) { 

     } else if (id == R.id.nav_send) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 
} 

Wie Sie sehen, ich versuche, es zu packen das NavigationView-Objekt, das nicht funktioniert, aber ich habe auch versucht, nur mit

TextView menuUsername = (TextView) findViewById(R.id.menu_username); 

, die auch eine Nullreferenz zurückgibt. Wie kann ich die Felder für Benutzername und E-Mail in meiner Hauptaktivität abrufen, damit ich den Inhalt dynamisch ausfüllen kann? Danke

Antwort

4

Sie werden es nicht so finden. Der richtige Weg ist zuerst Ihre NavigationView zu finden, dann bekommen seine HeaderViewgetHeaderView() und anschließend Ihre Widgets in diesem HeaverView suchen:

NavigationView nv = findViewById(R.id.nav_view); 
TextView menuUsername = nv.getHeaderView(0).findViewById(R.id.menu_username); 

oder kompakter Weise

TextView menuUsername = findViewById(R.id.nav_view) 
         .getHeaderView(0).findViewById(R.id.menu_username); 
+1

Perfect !! ich danke dir sehr! –

Verwandte Themen