【问题标题】:It gives me a white screen when I run this code and the cause of this white screen is the first line in the try block当我运行此代码时,它给了我一个白屏,这个白屏的原因是 try 块中的第一行
【发布时间】:2021-01-21 09:27:46
【问题描述】:

当我运行这段代码时它给了我一个白屏,这个白屏的原因是 try 块中的第一行:

try {
    result = task.execute("https://www.hollywoodreporter.com/lists/international-womens-day-2017-feminist-quotes-25-celebrities-983702").get();
    
    String[] splitResult= result.split("</div><figcaption class");
    
    Pattern p =Pattern.compile(" src=\" (.*?) \"/> ");
    Matcher m= p.matcher(splitResult[1]);
    while (m.find()) { clecbUrls.add(m.group(1)); }
    
    p =Pattern.compile("content=\" (.*?) \"/> ");
    m= p.matcher(splitResult[1]);
    while (m.find()) { clecbNames.add(m.group(1)); }
                 
    Random random =new Random();
    chosenCeleb=random.nextInt( clecbUrls.size());
 } 
 catch (ExecutionException e) {
     e.printStackTrace();
     System.out.println("it crashed");
 }
 catch (InterruptedException e) {
     e.printStackTrace();
     System.out.println("crashed");
 }

【问题讨论】:

  • 不要用正则表达式解析html。
  • 不要在异步任务上使用 .get() 而是在 onPostExecute 中处理 doInBackground 的结果。

标签: java android split try-catch execute


【解决方案1】:

首先你应该修正你的样式,使第一行在代码块内。

其次,它不会崩溃,因为您应该从 catch 块中得到一个错误。这导致您的 task.execute 返回一个空字符串的想法。它将解释为什么既没有错误也没有内容。如果您想了解更多信息,您需要向我们展示 doInBackground 的代码。另外我不明白你为什么在执行结果上调用 get 。您不能使用 doInBackground 来返回您想要的任何内容吗?

【讨论】:

    猜你喜欢
    • 2020-02-07
    • 1970-01-01
    • 2016-02-14
    相关资源
    最近更新 更多