简体   繁体   English

全局参考findViewById

[英]Global Reference findViewById

Is it possible to get a reference to an XML element such as a button using findViewById globally in the main java class? 是否可以在主java类中全局使用findViewById来获取对XML元素(例如按钮)的引用?

I have so many references that I need to keep on calling them all over again multiple times because I can't use the reference I called in the onCreate method. 我有太多的引用,因此我需要多次重复调用它们,因为我无法使用在onCreate方法中调用的引用。

private long mode;
private final Button playBtn = (Button)findViewById(R.id.playBtn);
private final TextView aboutTitle = (TextView)findViewById(R.id.aboutTitle);

@Override 
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playBtn.setTypeface(robotoThin);
aboutTitle.setText("hello world");
}

I know that to fix the error would be to reference the button and the textview after the setContentView but the problem is that I need to repeat all my references for each method in the same class. 我知道要解决该错误,将在setContentView之后引用按钮和textview,但是问题是我需要为同一类中的每个方法重复所有引用。

// try this way (here i'm declare your both view object as globaly for your class so it can be acccess any where in classs and is created onCreate() at first time and further it used directly)
private long mode;
private Button playBtn; 
private TextView aboutTitle;

@Override 
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
playBtn = (Button)findViewById(R.id.playBtn);
aboutTitle = (TextView)findViewById(R.id.aboutTitle);
playBtn.setTypeface(robotoThin);
aboutTitle.setText("hello world");
}

You have to create member variables for the Views you want to reference and you initialize them in onCreate() once. 您必须为要引用的Views创建成员变量,然后在onCreate()一次初始化。 Then you reference these variables where you need inside the Activity 然后在“ Activity需要的地方引用这些变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 空对象引用上的findViewById(int) - findViewById(int) on a null object reference 对空对象引用的View.findViewById(int)' - View.findViewById(int)' on a null object reference Android:使用findViewById时需要引用视图吗? - Android: Need to reference view when using findViewById? 尝试实现标签布局,在findViewByID上引用空对象 - Trying to implement tab layout, null object reference on findViewByID 我无法使用 findViewById 重新引用 TableRow - I can't re-reference a TableRow with findViewById 如何从静态上下文中引用非静态方法'findViewById'? - How to reference non-static method 'findViewById' from a static context? 在'setContentView()'之后无法使用'findViewById()'获得对EditText的引用 - Cannot get a reference to EditText using 'findViewById()' after 'setContentView()' 即使在 setContentView() 之后调用时, findViewById() 上的空对象引用错误也是如此 - Null object reference error on findViewById() even when invoked after setContentView() null object 参考上的 View.findViewById(int)' 存在问题 - Problem with View.findViewById(int)' on a null object reference 什么是'JNI全球参考' - What is 'JNI Global reference'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM