我一直在寻找这一切,但不知道会发生什么......
当我回到以前的活动时,我的组件已正确初始化,但它们上面有一个新的......
我的意思是,在调试模式下,我遵循并执行 onResume 代码,编写文本并设置 onCLicklisteners,但是在好的组件之上显示一个空组件,不知道为什么
当然,当第一次创建应用程序时,它工作正常!
任何想法?
相关代码:组件
public class TopLayout extends RelativeLayout {
public static final int BTN_LEFT = 0;
public static final int BTN_RIGHT = 1;
public static final int LBL_TITLE = 2;
protected Button btnLeft;
protected Button btnRight;
protected TextView lblTitle;
public TopLayout(Context _context, AttributeSet _attrs) {
super(_context, _attrs);
}
public TopLayout(Context _context) {
super(_context);
}
public TopLayout(Context _context, AttributeSet _attrs, int _defStyle) {
super(_context, _attrs, _defStyle);
}
public void initialize(){
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater) getContext().getSystemService(infService);
li.inflate(R.layout.top_layout, this, true);
btnLeft = (Button)findViewById(R.id.btnLeft);
btnRight = (Button)findViewById(R.id.btnRight);
lblTitle = (TextView)findViewById(R.id.lblTitle);
}
public void setText(int _element, String _text){
switch (_element) {
case BTN_LEFT:
btnLeft.setText(_text);
break;
case BTN_RIGHT:
btnRight.setText(_text);
break;
case LBL_TITLE:
lblTitle.setText(_text);
break;
default:
throw new RuntimeException("Unknown element to set Text: " + _text);
}
}
public void hideButton(int _element){
switch (_element) {
case BTN_LEFT:
btnLeft.setVisibility(Button.GONE);
break;
case BTN_RIGHT:
btnRight.setVisibility(Button.GONE);
break;
default:
throw new RuntimeException("Unknown button to hide: " + _element);
}
}
public void setClickAction(int _element, OnClickListener _listener){
switch (_element) {
case BTN_LEFT:
btnLeft.setOnClickListener(_listener);
break;
case BTN_RIGHT:
btnRight.setOnClickListener(_listener);
break;
case LBL_TITLE:
lblTitle.setOnClickListener(_listener);
break;
default:
throw new RuntimeException("Unknown button to activate: " + _element);
}
}
}
我的活动的onResume:
topLyt = (TopLayout) findViewById(R.id.lytTop);
topLyt.initialize();
topLyt.setText(TopLayout.BTN_LEFT, res.getString(R.string.edit));
topLyt.setText(TopLayout.LBL_TITLE, res.getString(R.string.involved_people));
topLyt.setText(TopLayout.BTN_RIGHT, res.getString(R.string.add));
但问题是,它第一次工作时,第二次我得到了第一个上面的另一个未初始化的对象(它必须是另一个,因为调试时对象编号是相同的,如果我输入,我们可以看到前一个按钮的透明背景)
由于声誉原因无法发布图片,但请看这里: