简体   繁体   English

Android出现奇怪问题,程序可以编译但无法正常工作

[英]Strange Problem with Android, Program compiles but does not work

In my android project, I have a simple Java file which gets webpage content, does something with the text and returns text as a String . 在我的android项目中,我有一个简单的Java文件,该文件获取网页内容,对文本进行处理并以String返回文本。 This text I display in a ScrollView in another activity on Android. 我在Android上的另一个活动中的ScrollView中显示了此文本。

It works fine, but the problem comes when I try manipulating the text in this Java file. 它工作正常,但是当我尝试操作此Java文件中的文本时出现问题。 I tried with arrays, it didn't work then switched back to String , still nothing. 我尝试使用数组,但没有用,然后又切换回String ,还是一无所有。 It is just a particular method which fails to provide any output but the program compiles fine. 这只是一种无法提供任何输出但程序可以正常编译的特殊方法。

However, the same method when tried in a Java project in Eclipse works perfectly fine. 但是,当在Eclipse中的Java项目中尝试使用相同的方法时,效果很好。

Here's my code: 这是我的代码:

// This method will return selected stripped text extracted from rawData
public static String FillMenus(String rawData){
    String resulT = "";
    int c1, c2;
    for(int i=0; i<11; i++){ 
        c1 = rawData.indexOf("\" width=\"50px\" />") + 17;
        c2 = rawData.indexOf(" €</td>") + 2;

        if (c1==16 || c2==1) break;
        if (c1<=c2){
            resulT = resulT+"\n"+ rawData.substring(c1, c2); 
            rawData = rawData.substring(c2);
        }
        if (c1>c2){ 
            resulT = resulT+"\n"+StripTag((rawData.substring(0, c2))); 
            rawData = rawData.substring(c2);
        }
    } 
    return resulT;
}

If there is nothing wrong with this method, then why does it not provide any output? 如果此方法没有问题,那么为什么不提供任何输出呢? If I return rawData in Android, the phone displays everything properly. 如果我在Android中返回rawData ,则手机会正确显示所有内容。 If I return the output result String , it is all blank in the phone. 如果我返回输出结果String ,那么电话中的所有内容都是空白。

If I call this method (say created in class ABC ) in another test class (test.java) inside the same Android project say with String a = ABC.FillMenus(String b) there is something strange. 如果我在同一Android项目中的另一个测试类(test.java)中调用此方法(例如,在类ABC创建),则说String a = ABC.FillMenus(String b)表示有些奇怪。 I get a compiler error, while it shows blank in Android phone. 我收到一个编译器错误,但在Android手机中显示为空白。 And the same thing called from within a Java project works absolutely fine. 从Java项目中调用的同一事物绝对可以正常工作。

Is there something I'm missing? 有什么我想念的吗?

A few things 一些东西

1) what is you for loop for ? 1)您的for循环是做什么用的?

2) if (c1<=c2) and if (c1>c2) are exclusive so I would recommend to change if (c1>c2) to else (helps in the future when you want to change your code) 2)if(c1 <= c2)和if(c1> c2)是互斥的,因此我建议将if(c1> c2)更改为else(以后在您想更改代码时会有所帮助)

3) you should init result to null more than "", it will help you test at the exit of FillMenu to know when your code was skipped or when there is no values. 3)您应该在init结果中将null设置为大于“”,这将帮助您在FillMenu的出口进行测试,以了解何时跳过了代码或何时没有值。

now my guess is that for some reason if (c1==16 || c2==1) break; 现在我的猜测是由于某种原因如果(c1 == 16 || c2 == 1)中断; is triggered and therefore resultT is never changed hence exists empty. 被触发,因此resultT永远不会改变,因此存在为空。

you should run this code in debug and put a break point in it, monitor particularly the values of c1 c2 您应该在调试中运行此代码并在其中放置一个断点,尤其要监视c1 c2的值

a part form that I don't think there is enough info to help you more 我认为没有足够的信息来帮助您的零件

Jason 杰森

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM