2015-08-21 20 views
6

我在其中一項活動中保存和恢復視圖可見性。我通過撥打mButton.getVisibility()並將其保存在Bundle中。在onRestore中,我得到int值時顯示錯誤。必須是以下其中一項:View.VISIBLE,View.INVISIBLE,View.GONE

Must be one of: View.VISIBLE, View.INVISIBLE, View.GONE less... (Ctrl+F1) 
Reports two types of problems: 
- Supplying the wrong type of resource identifier. For example, when calling Resources.getString(int id), you should be passing R.string.something, not R.drawable.something. 
- Passing the wrong constant to a method which expects one of a specific set of constants. For example, when calling View#setLayoutDirection, the parameter must be android.view.View.LAYOUT_DIRECTION_LTR or android.view.View.LAYOUT_DIRECTION_RTL. 

代碼編譯並運行沒有錯誤

代碼

@Override 
public void onSaveInstanceState(@NonNull Bundle savedInstanceState) { 
    savedInstanceState.putInt("BUTTON_VISIBILITY", mButton.getVisibility()); 

    super.onSaveInstanceState(savedInstanceState); 
} 

public void onRestoreInstanceState(@NonNull Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 

    mButton.setVisibility(savedInstanceState.getInt("BUTTON_VISIBILITY")); 
    // savedInstanceState.getInt("BUTTON_VISIBILITY") is underlined red 
} 
+2

您可以添加'@SuppressWarnings( 「的ResourceType」)' – BNK

+0

通常一個警告強調了黃色,這是紅色下劃線令我有些擔憂 – Eoin

+1

@SuppressWarnings( 「的ResourceType」)的工作!謝謝 – Eoin

回答

13

正如我剛纔評論,你可以添加@SuppressWarnings("ResourceType")。希望這可以幫助!

Alt-Enter組合 enter image description here

+1

是的,這是做的工作,謝謝 – Eoin

+0

我的經驗是:當AS發出警告,按Alt-Enter找到解決方案:) – BNK

+0

Alt-Enter沒有任何關於此:/ – Eoin

相關問題