0

Android M에서는 드로어 블 폴더의 특정 리소스를 ImageButton으로 설정해야합니다. 보기가 선언 된 MainActivity에서 어느 것이 작동 할 것인가.클래스의 이미지 버튼 용 setImageResource

스위치 문을 사용하여 올바른 리소스를 선택하는 방법으로 클래스를 호출해야하는 second Activity이 있습니다.

ImageButton에 대한 참조를 호출 된 클래스에 올바르게 가져올 수 없습니다.

생성자를 사용하여 클래스 내의 컨텍스트를 설정하고 ImageButton mRecordImageButton 개체를 public으로 만들었지 만 mRecordImageButton에 대한 개체 오류에 대한 확인되지 않은 참조가 여전히 있습니다. (unable to resolve symbol).

다음은 클래스의 코드입니다. 어떤 도움을 ImageButtonProperties.java 클래스 파일

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: 
      } 


     } 

    } 

감사의 MainActivity

public ImageButton mRecordImageButton; 

에서 .

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

도움을 주셔서 감사합니다. 고맙습니다. 디버거를 검사 할 때 생성자가 호출 중입니다.하지만 여전히 단추에 대한 참조가 있고 해결되지 않았습니다. 당신이 제안 생성자에

전화, 내가 된 setContentView

된 setContentView (twistlogic.com.miwidget.R.layout.activity_camera2_video_image) 후 그것을 가지고; 최종 ImageButtonProperties 속성 = 새 ImageButtonProperties ((this)));

메인 클래스에는 private ImageButton mRecordImageButton이 있습니다. properties.ImageButtonSym (AppButtonStates.VIDEO_REC_ON) : 또한 공공 시도했지만 그 또한

내가 콜

를 작동하지 않았다

호출 된 클래스는

공용 클래스 ImageButtonProperties { 상황에 맞는 상황입니다; 나는 가이드로 사용되었다

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; 

    } 
} 

}

'어떻게 다른 활동에서 텍스트를 변경합니다',하지만 난 여전히 해결되지 않은 참조로 mRecordImageButton를 얻을. 어쩌면 내가 컨텍스트 개체를 이해하지 오전. 컨텍스트가 있다면 뷰의 모든 개체에 액세스 할 수 있다고 생각했습니다. 그것은 이해의 오류입니까?

답변

1

당신의 생성자는 인수 누락, 감사합니다, 이것이이 보일 것입니다 :

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

과 활동에

호출이 같은 생성자 :

ImageButtonProperties properties = new ImageButtonProperties(this); 

방법 지금은 아무 것도 할당하지 않고있는 것입니다. 린트가 당신에게 그것에 대해 경고하지 않았다면 놀랄 것입니다.