导航视图中的null对象引用

时间:2015-10-23 21:26:14

标签: android android-layout android-design-library navigationview android-navigationview

我无法在导航视图标题中将文本设置为TextView。

出了什么问题?你能帮帮我吗?

来自我的java类的

代码

public class MainActivity extends AppCompatActivity {

DialogFragment AboutDialog;
DialogFragment ScheduleDialog;
DialogFragment StartDialog;

private Toolbar toolbar;

private TextView drawerName, drawerNumber;

private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;

NavigationView navigationView;

String gn, studName, studNumber, prfTheme;
int versionCode = BuildConfig.VERSION_CODE;
int prfVersionCode;

SharedPreferences sPref;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // если новая версия, то показываем диалог-предупреждение
 //   StartDialog();

    // устанавливаем нашу тему перед появлением лэйаута
    SetTheme();

    setContentView(R.layout.activity_main);

    initViews();

    gn = PreferenceManager.getDefaultSharedPreferences(this).getString("groups_list", "22492");
    studName = PreferenceManager.getDefaultSharedPreferences(this).getString("student_name", "");
    studNumber = PreferenceManager.getDefaultSharedPreferences(this).getString("student_number", "");

    toolbar.setSubtitle(gn);

    if (toolbar != null) {
        toolbar.setTitle(R.string.app_name);
        setSupportActionBar(toolbar);
    }

    if (!isTablet(this)) {
        initDrawer();
    }

    if (studName.equals("")) {
        drawerName.setText("");
    } else 
    if (!studName.equals(""))
    {
        drawerName.setText(studName + ", гр. " + gn);
    } 

    drawerNumber.setText(studNumber);


    Fragment fragment = new MainFragment();
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

}

public void initViews() {
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerName = (TextView) findViewById(R.id.drawer_name);
    drawerNumber = (TextView) findViewById(R.id.drawer_number);

    navigationView = (NavigationView) findViewById(R.id.nav_view);
    if (navigationView != null) {
        setupDrawerContent(navigationView);
    }

} ....

和来自 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="180dp" 
    android:background="@drawable/pic" 
    android:padding="16dp" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark" 
    android:orientation="vertical" 
    android:gravity="bottom"> 

    <TextView 
        android:id="@+id/drawer_name" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:textColor="#fff" 
        android:fontFamily="sans-serif-light" 
        android:textStyle="bold" 
        android:text="Вася Пупкин" 
        android:textSize="18sp" 
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"/> 

    <TextView 
        android:id="@+id/drawer_number" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:textColor="#fff" 
        android:fontFamily="sans-serif-light" 
        android:textStyle="normal" 
        android:text="№ группы" 
        android:textSize="16sp" 
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"/> 

</LinearLayout> 

文本进行编辑(sggfgdjfgdfgdfggfgdjfgdfgdfggfgdjfgdfgdfggfgdjfgdfgdfggfgdjfgdfgdfggfgdjfgdfgdfggfgdjfgdfgdfggfgdjfgdfgdfggfgdjfgdfgdfggfgdjfgdfgdfg)

1 个答案:

答案 0 :(得分:0)

if (studName.equals("")) { 
   drawerName.setText(""); 
} else {
  if (!studName.equals("")) 
  { 
     drawerName.setText(studName + ", гр. " + gn); 
  } 
    drawerNumber.setText(studNumber);
}

尝试在其他情况下添加{}。