的Andr​​oid / Java的试图找出哪里把变量声明变量、声明、Andr、oid

2023-09-06 18:41:27 作者:妈妈说不让我谈恋爱

我是新来的Andr​​oid开发,到目前为止,我一直主要是搞清楚如何获得教程code在一个应用程序,我的建设工作。我奋力理解在哪里声明变量,为什么我复制和粘贴一些教程,结束了错误。

例如,我想学习如何使用ViewFlipper。从这个教程我有问题,与Eclipse的告诉我,VF是不是当我使用它里面的OnClick一个变量。所以,当我招行:

  ViewFlipper VF =(ViewFlipper)findViewById(R.id.viewFlipper1); 

要略高于我的onCreate方法,然后它的作品。为什么我看到这么多的教程,在OnCreate中声明的,为什么没有为我工作的变量?哪里是合适的地点申报呢?我理解封装和继承的基础知识,所以在OnCreate就像任何其他的方法,并宣布在那里是从其他方法分离的变量?我的按钮是什么,我要宣布我的那些类里,但外面的OnCreate?

解决方案   

为什么我看到的的OnCreate 声明,为什么没有为我工作中的变量很多的教程吗?

在方法中声明的变量,或者外面,真的可以归结为是什么样的 范围 要与变量关联。结果里面的方法,你要小心,你声明一个变量,你打算使用它。

  

哪里是适当的地方申报呢?

的得体的哪里来声明一个变量完全取决于在哪里你想用它。在类级别声明一个变量,当你只打算在方法中使用它是不明智的。和方法内声明变量,当班上许多其他方法要访问它,你最终传递变量每种方法 - 也是不明智的。所以现在,你可以考虑:

在声明类变量 - 当在班里很多方法需要访问它。在方法声明的变量 - 当只有方法使用的变量

我想补充一点, 1 和 2 是的不的可盲目适用普遍规则 - 但下车一开始,你可以跟着他们,直到你找出与变量范围,访问规范和寿命相关的更深层次的细微差别。

我不是在谈论访问说明这里,因为,它们值得更详细的了解,你可以得到的此处。 ,我也没有我谈到的实例与类变量的区别就在这里,因为你会提到像的这个。

  

我明白了封装和继承的基础知识,所以的onCreate 就像任何其他的方法,并宣布在那里是从其他方法分离的任何变量?

这是正确的。

  

那我的按钮,应该我宣布我的那些阶级内部,而是外部的的onCreate

在Android的UI组件,通常需要通过多种方法来访问。 的TextView 按钮,等等 - 通常这需要在不同的时间被改变,从不同的方法的状态 - 让你倒是被关在类级别声明他们更好的。

为什么要preFER类级别声明UI变量的另一个重要原因是,可以减少具有Android框架创建它们每一个方法调用的开销。只要你的活动实例是活的,该框架包含到UI组件/变量。

I'm new to Android development and so far I have been mostly figuring out how to get tutorial code to work in an app I'm building. I am struggling in understanding where to declare variables and why I copy and paste some tutorials that end up with errors.

For example, I am trying to learn how to use the ViewFlipper. From this tutorial I have issues with Eclipse telling me that vf is not a variable when I use it inside OnClick. So when I move the line:

ViewFlipper vf = (ViewFlipper)findViewById(R.id.viewFlipper1);

to just above my OnCreate method, then it works. Why do I see so many tutorials with the variables declared in OnCreate and why doesn't it work for me? Where is the appropriate place to declare them? I understand the basics of encapsulation and inheritance, so is OnCreate just like any other method and any variables declared in there are isolated from other methods? What about my buttons, should I declare those inside my class but outside the OnCreate?

解决方案

Why do I see so many tutorials with the variables declared in OnCreate and why doesn't it work for me?

Declaring variables within a method, or, outside, really boils down to what kind of scope you want to associate with the variable. Inside a method, you need to be careful that you're declaring a variable before you intend to use it.

Where is the appropriate place to declare them?

Appropriateness of where to declare a variable depends entirely on where all you want to use it. Declaring a variable at the class level, when you only intend to use it within a method is unwise. And declaring variables within methods, when a lot many other methods in the class want to access it, and you end up passing the variables to each method - is also unwise. So for now, you can just consider:

Declare variables at class - when many methods in the class need to access it. Declare variable in methods - when only the method has use for the variable.

I'd like to add, that 1 and 2 are not universal rules that can be applied blindly - but to get off to a start, you can follow them, until you figure out the deeper nuances associated with variable scoping, access specification and lifetime.

I'm not talking about access specifiers here, since, they merit a much detailed understanding, which you can get here. And neither have i talked about the difference of instance vs. class variables here, because you'd be better of referring to an official doc like this one.

I understand the basics of encapsulation and inheritance, so is onCreate just like any other method and any variables declared in there are isolated from other methods?

That's correct.

What about my buttons, should I declare those inside my class but outside the onCreate?

UI components in Android are typically required by multiple methods to access. TextView, Button, etc - usually have states which need to be altered at different times, from different methods - so you'd be better off declaring them at the class level.

Another important reason why you should prefer declaring UI variables at the class level is that you reduce the overhead of having the Android framework create them for every method invocation. As long as your Activity instance is alive, the framework holds onto the UI components/variables.