В Marshmallow все работает так, как должно. Однако, в Нугате, я получаю ошибку void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
. currentWorkoutDisplay
остается пустым, даже после того, как TextView currentWorkoutDisplay = (TextView) findViewById(R.id.currentWorkoutText);
findViewById возвращает null в Nougat, но не Marshmallow
Соответствующая часть моего макета:
<TextView
android:id="@+id/currentWorkoutText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView"
android:textAppearance="@android:style/TextAppearance.Material.Medium"
android:textColor="?android:attr/textColorPrimary"
android:textSize="16sp" />
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
... irrelevant code ...
// Get user preferences
SharedPreferences sharedpref = PreferenceManager.getDefaultSharedPreferences(this);
// Get the currentWorkoutDisplay TextView
TextView currentWorkoutDisplay = (TextView) findViewById(R.id.currentWorkoutText);
// Build the string for current workout display
String currentWorkoutText = currentProgram + currentCycleText + " - " + "Week " +
currentWeek + " " + "Day " + currentDay;
// Set the current workout text
currentWorkoutDisplay.setText(currentWorkoutText);
... continues ...
попробуйте использовать '@android: id/currentWorkoutText' в своем текстовом id – Roljhon