2017-01-21 46 views
-2

在棉花糖中,一切都按原樣運作。然而,在牛軋糖,我得到一個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 ... 
+0

如果您知道答案,請在您的文本視圖ID中使用「@android:id/currentWorkoutText」 – Roljhon

回答

0

一定要檢查,以確保您的佈局「正常」和大型設備包含相同的名稱!

+0

。你爲什麼要問別人? :O –

+0

我在發佈後不久就想出了它,但爲了防止其他人也忽略它而決定放棄它。 –

+0

那麼,這不是關於「牛軋糖」或「棉花糖」的問題。它關於設備的屏幕尺寸。 –