-1

我无法在导航视图标题中将文本设置为 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> 

编辑文本

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 回答