Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

java - Not able to dynamically set the setVisibility() parameter

I am trying to set the visibility for a button as follows:

public Bundle setActivityState(Bundle bundle){
    startBtn = (Button) findViewById(R.id.startSensorsBtn);

    startBtn.setVisibility(
            getVisibilityState(bundle, PersistanceConstants.START_BTN_STATE)
    );          

    return bundle;
}

public int getVisibilityState(Bundle bundle, String keyName){
    if (bundle.getInt(keyName) == View.VISIBLE){
        return View.VISIBLE;
    } else if (bundle.getInt(keyName) == View.INVISIBLE){
        return View.INVISIBLE;
    } else if (bundle.getInt(keyName) == View.GONE){
        return View.GONE;
    }

    return 0;
}

But I am getting the error:

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.

while calling

getVisibilityState(bundle, PersistanceConstants.START_BTN_STATE)

I don't know how to get around this. I understand that it's expecting a given set of values but all I know is to pass an int to it. What can be done here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When you know what you're doing, you can suppress this Android Studio inspection locally with

//noinspection ResourceType

For example,

//noinspection ResourceType
startBtn.setVisibility(bundle.getInt(PersistanceConstants.START_BTN_STATE));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...