简体   繁体   English

可以在Android Studio的onCreate方法中调用java文件中的其他方法吗?

[英]Can other methods from a java file be called in the onCreate method in Android Studio?

I'm trying to call a method that is defined later in my java file: 我正在尝试调用稍后在我的java文件中定义的方法:

public int moveHorizontal;
public int moveVertical;
public RelativeLayout relative1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    relative1 = (RelativeLayout) findViewById(R.id.relative1);
    moveHorizontal = width(1f/12f);
    moveVertical = height(1f/12f);
}

... ...

public int width(float fraction) {
    float relativeWidth = relative1.getWidth();
    return Math.round(fraction*relativeWidth);
}

However, when I run my app in debug mode I see that moveHorizontal and moveVertical are both set to 0 . 但是,当我在调试模式下运行我的应用程序时,我看到moveHorizontalmoveVertical都设置为0 I know the method works fine as I've tried using it in the rest of the code to find width(1f/12f) and it returns 90 . 我知道该方法工作正常,因为我尝试在其余代码中使用它来查找width(1f/12f)并返回90 Does this mean that I can't call the width method inside the onCreate method? 这是否意味着我无法在onCreate方法中调用width方法? And is there a way around this? 有没有办法解决这个问题?

Does this mean that I can't call the width method inside the onCreate method? 这是否意味着我无法在onCreate方法中调用width方法?

You can call width() . 你可以调用width() However, relative1.getWidth() will be 0 at this point, because the layout has not been measured and laid out yet. 但是, relative1.getWidth()将为0,因为尚未测量和布局布局。

And is there a way around this? 有没有办法解决这个问题?

First, see if there is a better solution to whatever problem you are trying to solve, that involves getting the size of relative1 . 首先,看看是否有更好的解决方案来解决您要解决的任何问题,包括获取relative1的大小。 For example, ConstraintLayout and LinearLayout both support sizing things based upon fractions, without you having to do the calculations yourself. 例如, ConstraintLayoutLinearLayout都支持基于分数的大小调整,而无需您自己进行计算。

If you are sure that you need the width and height, wait to try using it until the widgets have been sized . 如果您确定需要宽度和高度,请等待尝试使用它,直到小部件的大小

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

相关问题 我们可以调用在 MainActivity 中的同一类中声明的方法,而不是在 onCreate 或 android studio 中的其他方法中吗? - Can we call a method declared in same class in MainActivity not in onCreate or other methods in android studio? 创建一个可以读取文件的void方法,该方法可以由该类中的其他方法调用吗? - Creating a void method that reads a file, that can be called by other methods in the class? Android Studio的多个onCreate方法 - Android Studio multiple onCreate methods Android Studio MainActivity.java 中的多个 onCreate 方法 - Android Studio Multiple onCreate methods in MainActivity.java 如何从OnCreate方法外部更改窗口小部件可见性? Java Android Studio - How to change widget visibility from outside of OnCreate method? Java Android Studio 其他方法未调用onCreate - Other Method isn't called onCreate Android Activity onCreate方法调用的同步方法 - Synchronized method called by Android Activity onCreate method 即使在 oncreate 方法中没有调用它们,为什么 android 也会运行像“onItemSelected”这样的方法? - Why do android run methods like "onItemSelected" even if they are not called in the oncreate Method? 在android的oncreate方法中调用多个方法 - calling multiple methods in oncreate method in android Libgdx Android:onCreate()之后未调用方法onStart() - Libgdx Android: method onStart() not called after onCreate()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM