android – 必须是以下之一:View.VISIBLE,View.INVISIBLE,View.GONE [复制]

前端之家收集整理的这篇文章主要介绍了android – 必须是以下之一:View.VISIBLE,View.INVISIBLE,View.GONE [复制]前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Not able to dynamically set the setVisibility() parameter2个
我正在保存并恢复我的一项活动中的视图可见性.我这样做是通过调用mButton.getVisibility()并将其保存在Bundle中.在onRestore中,我得到int值,它显示错误.
  1. Must be one of: View.VISIBLE,View.INVISIBLE,View.GONE less... (Ctrl+F1)
  2. Reports two types of problems:
  3. - 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.
  4. - 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.

代码编译并运行,没有错误

  1. @Override
  2. public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
  3. savedInstanceState.putInt("BUTTON_VISIBILITY",mButton.getVisibility());
  4.  
  5. super.onSaveInstanceState(savedInstanceState);
  6. }
  7.  
  8. public void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
  9. super.onRestoreInstanceState(savedInstanceState);
  10.  
  11. mButton.setVisibility(savedInstanceState.getInt("BUTTON_VISIBILITY"));
  12. // savedInstanceState.getInt("BUTTON_VISIBILITY") is underlined red
  13. }

解决方法

正如我刚刚评论的那样,您可以添加@SuppressWarnings(“ResourceType”).希望这可以帮助!

Alt-Enter组合

猜你在找的Android相关文章