在棉花糖中,一切都按原樣運作。然而,在牛軋糖,我得到一個void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
錯誤。該currentWorkoutDisplay
保持爲空,即使TextView currentWorkoutDisplay = (TextView) findViewById(R.id.currentWorkoutText);
findViewById在牛軋糖上返回null,但不是棉花糖
我佈局的相關部分:
<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 ...
如果您知道答案,請在您的文本視圖ID中使用「@android:id/currentWorkoutText」 – Roljhon