我有一个片段,它是一个“定时器”,我可以在任何地方添加。在片段中,我以编程方式更改textView,并且它运行得非常漂亮。我的问题是当涉及到从构造函数膨胀的布局中使用视图(?不知道这是否是正确的术语)在它下面的另一种方法。在片段后添加方法
public class Timer_fragment extends android.support.v4.app.Fragment {
int testpins;
String testedpin;
TextView text;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.timer_frag, container, false);
TextView text = (TextView) v.findViewById(R.id.pwd_status);
text.setText("Setting text in fragment not main");
/* set the TextView's text, click listeners, etc. */
updateStatus();
return v;
}
所有代码的工作,没有错误,但是当我尝试添加这个方法:
private void updateStatus() {
TextView text = (TextView) findViewById(R.id.pwd_status);
testPin();
text.setText(testedpin);
}
我下findViewById说The method findViewById(int) is undefined for the type Timer_fragment
得到一条红线。
我想过在我的所有方法中夸大视图,而不是返回它们,但肯定会影响性能莫名其妙吗?
只是在使用视图之前试图膨胀布局,但我得到一个单词inflater
和container
错误,说他们无法解决。
我是否正确地处理这个问题?
是的,我的问题只是访问视图。 – EGHDK 2012-07-30 16:11:55