在两台计算机上测试Android App时结果截然不同 [英] Very Different Result When Testing Android App on Two Computers

查看:88
本文介绍了在两台计算机上测试Android App时结果截然不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用程序,并且当我不在家里和在家中使用Windows计算机时,我正在用Macbook笔记本电脑进行开发.我在两台机器上都具有相同的JDK版本(1.8.0_131),并且已经设置了正确的JDK路径,如下所示:

I'm working on an Android app, and I'm developing on my Macbook laptop when I'm away from home and my Windows computer at home. I have the same JDK version (1.8.0_131) on both machines, and I have set the correct JDK path like shown here: How to set the JDK in Android Studio.

当我在任何一台机器上调用方法Class.getDeclaredFields()时,我得到的结果都非常不同.在我的Mac上,该方法的行为类似于

When I call the method Class.getDeclaredFields() on either machine, I get very different results. On my Mac, the method behaves similarly to how the Java-Doc says it should, although it seems to only see public fields. When testing on my Windows computer, the method returns ALL fields, even inherited ones, which is explicitly what the Java-Doc says the method DOES NOT do. I need Class.getDeclaredFields() to be consistent because I'm using it to build JSON files with Gson and store them on the disk elsewhere in my program.

我猜想会有一个不同的Android运行时导致该程序的行为有所不同,但是我不知道在哪里可以找到该信息或如何纠正此问题,以便在部署时始终运行.

I'm guessing that there is a different Android Runtime which is causing the program to behave differently, but I have no idea where to find that information or how to correct this problem so that it runs consistently when deployed.

在两台计算机上,我都使用具有Android API 25(Android 7.1.1牛轧糖)的Nexus 5X虚拟设备和构建工具25.0.2.

On both machines I'm using a Nexus 5X virtual device with Android API 25 (Android 7.1.1 Nougat) and build tools 25.0.2.

这是我的具体实现方式

public abstract class Article {

    protected Context context;
    protected int id;//start at 0 and go up.
    private Texture texture;
    private Temperature idealTemp;
    private Formality formality;
    private String name;
    private int color;  //expressed as hex RGB
    private Bitmap image;
    JsonObject articleData;

    ...

    @Override
    public String toString() {
    //TODO: for efficiency, this should be StringBuilder
    String temp = "id: " + id + ", texture: " + texture + ", idealTemp: " + idealTemp
            + ", formality: " + formality + ", name: " + name + ", color: #" + color;

        Field[] fields = this.getClass().getDeclaredFields();
        for (Field f : fields) {
            try {
                temp = temp.concat(", " + f.getName() + ": " + f.get(this).toString());
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                continue;
            }
        }

        return temp;
    }
}//end class

继承Article的类是Top,它看起来像这样:

Where the class that inherits Article is Top which looks like this:

public abstract class Top extends Article {

    public Top(Context con, Texture tex, Temperature temp, Formality form, String n, int col, Bitmap img) {
        super(con, tex, temp, form, n, col, img);
    }

}

最后,使用Article.toString()方法的非抽象类是Shirt:

And finally the non-abstract class that uses the method Article.toString() is Shirt:

public class Shirt extends Top {

    //If this is not public, it is not seen by getDeclaredFields()
    //seems to be a bug in the Android Runtime on my Mac?
    public boolean longSleeves;

    public Shirt(Context con, Texture tex, Temperature temp, Formality form,
             String n, int col, Bitmap img, boolean longSleeve) {
        super(con, tex, temp, form, n, col, img);
        longSleeves = longSleeve;
    }
}

更新:我最初发布的代码使用了

UPDATE: The code I originally posted used

getFields()

代替

getDeclaredFields()

这已更改,我将包含一些图像以显示我在Android Studio中的两个调试器显示的内容: Mac:

This has been changed and I will include images to show what both of my debuggers in Android Studio show: Mac:

Windows:

推荐答案

我猜测您在Mac和PC上使用的代码不同.您提到了调用getDeclaredFields(),但是您发布的代码调用了getFields().如果您发布的代码来自您的PC,那么这就是为什么您看到继承的公共字段的原因.

I'm guessing you have different code on your Mac and PC. You mention calling getDeclaredFields(), but the code you posted calls getFields(). If the code you posted is from your PC, then that is why you're seeing inherited public fields.

您没有看到私有字段,因为您试图访问它们而不使它们无法访问.这将引发一个异常,您将其记录并忽略.检查您的日志,您应该看到每个非公共字段的异常.尝试仅将字段名称添加到字符串中,或​​在调用get之前调用每个字段的setAccessible(true).

You're not seeing private fields because you're trying to access them without making them accessible. This throws an exception, which you log and ignore. Check your logs and you should see exceptions for each non-public field. Try adding just the names of your fields to your string or call setAccessible(true) of each field before calling get.

更新:

我不知道changeserialVersionUID字段来自何处.我怀疑有一个IDE或编译器设置可以自动包含它们.由于它们是静态字段,因此您可以将其过滤掉.无论如何,您可能都想这样做.

I don't know where the change and serialVersionUID fields are coming from. I suspect there's an IDE or compiler setting to automatically include them. Since they are static fields you can filter them out. You would probably want to do this anyway.

for (Field f : getClass().getDeclaredFields()) {
    if (Modifier.isStatic(f.getModifiers()) continue;
    if (!f.isAccessible()) {
        f.setAccessible(true);
    }
    Object value = f.get(this);
    temp += ", " + f.getName() + ": " + String.valueOf(value);
}

这篇关于在两台计算机上测试Android App时结果截然不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆