-1

Navigation View ヘッダーの 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)

4

1 に答える 1

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

else ケースに {} を追加してみてください。

于 2015-10-23T21:34:45.840 に答える