【问题标题】:How can I access a TextView in the onCreate function?如何在 onCreate 函数中访问 TextView?
【发布时间】:2021-03-06 15:12:44
【问题描述】:

您好,一直在使用片段,并且有一个抽屉标题,它将显示当前登录的用户。

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.drawerlayout.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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="false"
        android:background="?android:attr/colorBackground"
        tools:context=".MainActivity">
    
        <include
            layout="@layout/drawer_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <include
            layout="@layout/content_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <com.google.android.material.navigation.NavigationView
            android:id="@+id/navigationView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/drawer_header"
            app:menu="@menu/drawer_menu" />
    
    
    </androidx.drawerlayout.widget.DrawerLayout>

这是创建布局并将标题和菜单添加到导航视图的代码。我正在尝试使用 id 访问存储在抽屉标题中的文本。

我遇到的问题是,当它尝试将变量 usernameHeader 设置为 id“用户”时,它会抛出一个错误,指出存在空对象引用。我的理解是,当它说“actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close);”时,抽屉是在onCreate期间创建的我可能是错的,非常感谢任何帮助。提前致谢

更新:

我已经重写了最小的示例来显示它中断的确切部分。 我添加了包含 ID 为“用户”的 TextView 的抽屉头文件。我还添加了 MainActivity 的最小版本。

drawer_header.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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="150dp"
        android:background="@color/teal_700">
    
        <TextView
            android:id="@+id/user"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="25dp"
            android:text="@string/user"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>


MainActivity.java
    
    package com.secureit.drawerfragment;
    
    import android.os.Bundle;
    import android.view.MenuItem;
    import android.widget.TextView;
    
    import androidx.annotation.NonNull;
    import androidx.appcompat.app.ActionBarDrawerToggle;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.appcompat.widget.Toolbar;
    import androidx.core.view.GravityCompat;
    import androidx.drawerlayout.widget.DrawerLayout;
    
    import com.google.android.material.navigation.NavigationView;
    
    public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
        DrawerLayout drawerLayout;
        ActionBarDrawerToggle actionBarDrawerToggle;
        Toolbar toolbar;
        NavigationView navigationView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            toolbar = findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            drawerLayout = findViewById(R.id.drawer);
            navigationView = findViewById(R.id.navigationView);
            navigationView.setNavigationItemSelectedListener(this);
    
            actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close);
            drawerLayout.addDrawerListener(actionBarDrawerToggle);
            actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
            actionBarDrawerToggle.syncState();
    
            TextView textView = findViewById(R.id.user); // This is where the error shows "Null object reference"
            textView.setText("user");
        }
    
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            drawerLayout.closeDrawer(GravityCompat.START);
            return false;
        }
    }

【问题讨论】:

  • 我在您提供的 xml 中看不到任何 ID 为“用户”的视图。因此,既然您获得了 NPE,我想在 activity_main.xml 或 &lt;include&gt; 标签中引用的布局文件之一中没有 ID 为“user”的视图。
  • 我建议您尝试将代码中的问题缩小到特定部分,然后向我们提供minimal reproducible example。这将帮助您获得更多答案,希望我们能帮助您!

标签: java android


【解决方案1】:

问题是,您正在以与您的活动不同的视图访问 TextView。您应该先膨胀您的标题视图,然后使用它来访问您的 TextView。将此添加到您的 onCreate 方法中。

View headerView = navigationView.getHeaderView(0);
TextView txtUser= headerView.findViewById(R.id.user);
txtUser.setText("user");

【讨论】:

  • 非常感谢,这解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多