简体   繁体   English

为什么 findViewById() 方法只在活动 class 的方法中有效,而在 class 中直接无效?

[英]Why does the findViewById() method work only in the methods of the activity class but not in the class directly?

public class MainActivity extends AppCompatActivity {

       TextView name_header;

       //this results into an error
       name_header = (TextView) findViewById(R.id.name_header);

       @Override
       protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.menu_order);

       //whereas, this works perfectly
       name_header = (TextView) findViewById(R.id.name_header);
...
 }
}

The problem is that I am trying to initialize the TextView object globally using the findViewById() but it throws an error at the runtime and then if I use it in the onCreate() or in any other method the initialization works just fine.问题是我正在尝试使用 findViewById() 全局初始化 TextView object 但它在运行时引发错误,然后如果我在 onCreate() 或任何其他方法中使用它,初始化工作得很好。

I am developing for API level 22 and above我正在为 API 22 级及以上开发

Before the setContentView(...) function in onCreate(...) has been called, the layout(-file) has not been inflated/set yet.在调用 onCreate(...) 中的 setContentView(...) function 之前,layout(-file) 尚未膨胀/设置。 So there is nothing to look up an id in, yet.因此,还没有什么可以查找 id 的。

As the Android docs describe it:正如 Android 文档所描述的那样:

public T findViewById (int id)公共 T findViewById (int id)

Finds a view that was identified by the android:id XML attribute that was processed in onCreate(Bundle).查找由在 onCreate(Bundle) 中处理的 android:id XML 属性标识的视图。

But since you already declared your TextView 'globally', you can just initialise it inside the onCreate function and use it anywhere else in the MainActivity without an issue.但是,由于您已经将 TextView 声明为“全局”,因此您可以在 onCreate function 中对其进行初始化,然后在 MainActivity 的其他任何地方使用它而不会出现问题。

As you know, setContentView(R.layout.menu_order);如您所知, setContentView(R.layout.menu_order); defines the layout to be attached with this particular java class.定义要与此特定 java class 连接的布局。 So, if you initialize any views from that XML it can't find it anywhere.因此,如果您从该 XML 初始化任何视图,它将无法在任何地方找到它。 Because it has no clue where to look for id name_header .因为它不知道在哪里寻找 id name_header So, it shows you error.因此,它向您显示错误。

Now, I read your comment.现在,我读了你的评论。 And If I am correct you are initializing the view just after onCreate() .如果我是正确的,您将在onCreate()之后初始化视图。 That means something like this.这意味着这样的事情。

...
public class MainActivity extends AppCompatActivity {
       @Override
       protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
      setContentView(R.layout.menu_order);
 }
   name_header = (TextView) findViewById(R.id.name_header);
}
...

So, this is same as this.所以,这和这个是一样的。

...
public class MainActivity extends AppCompatActivity {

       name_header = (TextView) findViewById(R.id.name_header);

       @Override
       protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
      setContentView(R.layout.menu_order);
 }

}
...

So, it will give an error.所以,它会给出一个错误。 You have to initialize the view inside any method.您必须在任何方法中初始化视图。 Because at first java is going to initialize the variables which is outside the method.因为首先 java 将初始化方法之外的变量。 And then it's gonna go inside any of other methods.然后它会在任何其他方法中使用 go 。

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

相关问题 非活动类中的 FindViewById - FindViewById in a non activity class 同步是否适用于 class 中的所有方法或仅适用于 1? - Does synchronized work for all methods in a class or only 1? 如果从助手类中调用,则活动方法中的Android findViewById无法正常工作 - Android findViewById in activity method is not working if is called from helper class 在非活动类中使用findViewById - Using findViewById in a Non-Activity Class 为什么在实现接口的方法中分配实例变量在从同一个类的另一个本地方法调用它时不起作用? - why assigning instance variable inside method of implemented interface does not work while calling it from another local methods of the same class? 如果类A扩展了类B扩展了Activity,如何正确使用findViewById - How to correctly use findViewById if a class A extends class B extends Activity Java android findViewById在另一个类的静态方法中 - Java android findViewById in static method of another class 为什么内部类方法会隐藏所有具有相同名称的封闭类方法? - Why does an inner class method hide all the enclosing class methods with the same name? 如果方法名称与类不同,为什么这个java代码不起作用? - why does this java code not work if the method name is different from the class? 为什么我的方法在我的 API 中有效,但在测试 class 中无效? - Why does my method work in my API, but not in a test class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM