2014-06-18 246 views
-1

我試圖找到所有包含我提供的stacktrace中的包名稱的行。下面是我有:查找字符串中出現的所有子字符串

Log.d("result", result); 
     ArrayList<String> allOccurences = new ArrayList<String>(); 
     int lastIndex = 0; 
     int count = 0; 
     String packageName = context.getPackageName(); 

     while(lastIndex != -1) { 
      lastIndex = result.indexOf(packageName, lastIndex); 
      if(lastIndex != -1) { 
       String line = result.substring(lastIndex, packageName.length()); 
       allOccurences.add(line); 
       lastIndex += packageName.length(); 
      } 
     } 

     for (int i = 0; i < allOccurences.size(); i++) { 
      System.out.println(allOccurences.get(i)); 
     } 

如果我刪除

String line = result.substring(lastIndex, packageName.length()); 
allOccurences.add(line); 

它工作得很好。我似乎無法弄清楚爲什麼。 有什麼幫助嗎?

這裏就是我正在尋找的堆棧跟蹤:

06-18 20:43:59.455: D/result(1975): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.madqa/com.madqa.AndroidMADQAActivity}: java.lang.NullPointerException 
06-18 20:43:59.455: D/result(1975):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
06-18 20:43:59.455: D/result(1975):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
06-18 20:43:59.455: D/result(1975):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
06-18 20:43:59.455: D/result(1975):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
06-18 20:43:59.455: D/result(1975):  at android.os.Handler.dispatchMessage(Handler.java:99) 
06-18 20:43:59.455: D/result(1975):  at android.os.Looper.loop(Looper.java:137) 
06-18 20:43:59.455: D/result(1975):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
06-18 20:43:59.455: D/result(1975):  at java.lang.reflect.Method.invokeNative(Native Method) 
06-18 20:43:59.455: D/result(1975):  at java.lang.reflect.Method.invoke(Method.java:511) 
06-18 20:43:59.455: D/result(1975):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
06-18 20:43:59.455: D/result(1975):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
06-18 20:43:59.455: D/result(1975):  at dalvik.system.NativeStart.main(Native Method) 
06-18 20:43:59.455: D/result(1975): Caused by: java.lang.NullPointerException 
06-18 20:43:59.455: D/result(1975):  at com.madqa.AndroidMADQAActivity.onCreate(AndroidMADQAActivity.java:22) 
06-18 20:43:59.455: D/result(1975):  at android.app.Activity.performCreate(Activity.java:5104) 
06-18 20:43:59.455: D/result(1975):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
06-18 20:43:59.455: D/result(1975):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
06-18 20:43:59.455: D/result(1975):  ... 11 more 

包名是 「com.madqa」。堆棧跟蹤中有3次出現。我想找到它們並將它們中的每一個都打印出來。 (它應該打印com.madqa 3次)

+0

當它沒有工作時,它在做什麼? –

+0

它只打印出第一個出現 –

+0

樣本井幫助 –

回答

2

substring需要兩個索引,而不是索引和長度。您需要添加lastIndexpackageName.length(),像這樣:

String line = result.substring(lastIndex, lastIndex+packageName.length()); 

一旦你做出這種改變,你的代碼應該工作。

Demo on ideone.

+0

非常感謝!完美的作品!我怎麼看不到?這是一個漫長的一天哈哈 –

相關問題