0

In Android M, I need to set an ImageButton to a specific resource in the drawable folder. Which will which will work from the MainActivity where the view was declared.

I have a second Activity that needs to call a class with the method being to select the correct resource using a switch statement.

I cannot get the reference to the ImageButton correctly into the called Class.

I am using a constructor to set the context within the class and have made the ImageButton mRecordImageButton object public, but still have the unresolved reference to object error for mRecordImageButton. (unable to resolve symbol).

Here is the code in the class. in the MainActivity

public ImageButton mRecordImageButton;

in the ImageButtonProperties.java class file

 package twistlogic.com.miwidget;

    import android.app.Activity;
    import android.content.Context;
    import android.widget.ImageButton;

    public class ImageButtonProperties {
        Context context;

        public ImageButtonProperties() {
                this.context = context;                 // constructor
            }

        public void ImageButtonSym(EnumLists enumList) {

            switch (enumList) {
                case VIDEO_OFF:
                    (ImageButton) ((Activity)context).mRecordImageButton.setImageResource(R.drawable.ic_bluetooth_black_18dp);
                    break;
             default:
            }


        }

    }

Thank for any help.

//////////////////////////////////////////////////////////////////////////////

Thank you for the assistance, I am just learning so your help is appreciated. The constructor is being called now, when I check with the debugger, but I still have and unresolved reference to the button.

Call to the constructor as you suggested, I have it after setContentView

setContentView(twistlogic.com.miwidget.R.layout.activity_camera2_video_image); final ImageButtonProperties properties = new ImageButtonProperties((this));

In the main class I have private ImageButton mRecordImageButton; I also tried public, but that also didn't work

I call with: properties.ImageButtonSym(AppButtonStates.VIDEO_REC_ON);

the called class is

public class ImageButtonProperties { Context context;

public ImageButtonProperties(Context context) {
    this.context = context;          // constructor
}

public void ImageButtonSym(int appButtonStates) {
    switch (appButtonStates) {
        case AppButtonStates.VIDEO_REC_ON:
            (ImageButton) ((Activity)context).mRecordImageButton.setImageResource(twistlogic.com.miwidget.R.mipmap.btn_video_online);
            break;

    }
}

}

I was using as a guide, 'how to change text from another activity', but I still get the mRecordImageButton as an unresolved reference. Maybe I am not understanding the Context object. I thought that if I had the context, that all the objects in the view would be accessible. Is that an error in understanding?

Thank you,

1 Answers1

1

Your constructor is missing an argument, it should look this this:

 public ImageButtonProperties(Context context) {
        this.context = context; 
    }

and in the Activity call the constructor like this:

ImageButtonProperties properties = new ImageButtonProperties(this);

The way it is just now you're just assigning nothing to nothing. I'd be surprised if lint wasn't giving you a warning about it.

Dan
  • 321
  • 3
  • 11