2012-01-02 58 views
0

我正在制作一个应用程序,其中我陷入了这个异常 - 字符串索引超出了界限,我在下面一行出现错误。android:字符串索引出界

String netwrkk = mTelephonyMgr.getNetworkOperator(); 
System.out.println("hello"); 
if (netwrkk != null) { 
    System.out.println("hello2"); 
     // int mcc = Integer.parseInt(netwrkk.substring(0, 3)); // exception at this line 

    System.out.println("hello3"); 
     int mnc = Integer.parseInt(netwrkk.substring(3));// exception at this line 
     System.out.println("hello4"); 
    mccc1=(TextView)findViewById(R.id.mccc1); 
    System.out.println("netwrk:"+netwrkk); 
    mccc1.setText(Integer.toString(mcc)); 
    mncc1=(TextView)findViewById(R.id.mncc1); 
    System.out.println("netwrk:"+netwrkk); 
    mncc1.setText(Integer.toString(mnc)); 
    System.out.println("mccc: "+mcc); 
    try{ 
    System.out.println("bearing : "+locn.getBearing()); 
    }catch (Exception e) { 
     System.out.println(e); 
    }System.out.println("mnc"+mnc); 
         } 
+0

** netwrkk **字符串的长度是多少? – adatapost 2012-01-02 05:41:15

回答

2

您应该检查netwrkk的长度是否大于等于4。否则,substring(0, 3)将失败。

也让你自己熟悉Android中的Log类。你不应该使用System.out.println()

+0

log和System.out.println()中的主要区别是什么?你能详细解释一下吗? – Aditya1510 2012-01-02 07:43:45

+0

Log类是您应该用来记录的类。输出将在LogCat中(如System.out.println),但可以过滤输出,并且可以向其添加标记,以帮助识别哪个类生成输出。文档:http://developer.android.com/reference/android/util/Log.html – WarrenFaith 2012-01-02 08:16:05