2011-12-09 75 views
0

我無法弄清楚爲什麼我卡在這一點上,雖然沒有錯誤是我的編碼。我的問題實際上是,它不打印出我的EPUB,以及它只是掛在那裏,直到沒有響應的對話框出現。空白屏幕並只彈出「Not Responding」對話框。

我的代碼如下:

  package com.epub; 

    import java.io.BufferedReader; 
    import java.io.IOException; 
    import java.io.InputStream; 
    import java.io.InputStreamReader; 
    import java.util.List; 

    import nl.siegmann.epublib.domain.Book; 
    import nl.siegmann.epublib.domain.Resource; 
    import nl.siegmann.epublib.domain.Spine; 
    import nl.siegmann.epublib.domain.SpineReference; 
    import nl.siegmann.epublib.epub.EpubReader; 

    import android.app.Activity; 
    import android.content.res.AssetManager; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.webkit.WebView; 
    import android.widget.TextView; 

    public class EpubReaderActivity extends Activity { 
     WebView webView; 
     Book book; 
     TextView tv; 
     String line; 
     String linez; 
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 

      tv = new TextView(this); 
      webView = (WebView)findViewById(R.id.webView); 
      webView.getSettings().setJavaScriptEnabled(true); 
      AssetManager am = getAssets(); 
      try { 
       InputStream epubInputStream = am.open("testbook.epub"); 
       book = (new EpubReader()).readEpub(epubInputStream); 
      } catch (IOException e) { 
       Log.e("epublib", e.getMessage()); 
      } 
      Spine spine = book.getSpine(); 
      List<SpineReference> spineList = spine.getSpineReferences() ; 
      int count = spineList.size(); 
      tv.setText(Integer.toString(count)); 
      StringBuilder string = new StringBuilder(); 
      for (int i = 0; count > i; i++) { 
       Resource res = spine.getResource(i); 

       try { 
        InputStream is = res.getInputStream(); 
        BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
        try { 

         while ((line = reader.readLine()) != null) { 
          linez = string.append(line + "\n").toString(); 
         } 

        } catch (IOException e) {e.printStackTrace();} 

        //do something with stream 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

      } 
      webView.loadData(linez, "text/html", "utf-8"); 

     } 
    } 

介意幫我出這一點?

回答

0

浦這個代碼,因爲這需要時間來寫入數據,並加載Web視圖 線程是一個線程中執行的不同部分,你將不得不等待幾個sconds如果侑文件較大 你也可以把一個進度對話框,顯示的WebView

  new Thread(){ 
     public void run() { 
        for (int i = 0; count > i; i++) { 
        Resource res = spine.getResource(i); 

      try { 
       InputStream is = res.getInputStream(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
       try { 

        while ((line = reader.readLine()) != null) { 
         linez = string.append(line + "\n").toString(); 
        } 

       } catch (IOException e) {e.printStackTrace();} 

       //do something with stream 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } 

    }; 
    }.start(); 
+0

現在我用你的代碼,一個空的文本上來,而不是EPUB, – PudgeLover

+0

什麼是零均值。 – PudgeLover

+0

我logcat的與GC_FOR_MALLOC – PudgeLover

4

ANR(應用程序無響應)當你做一個沉重的工作通常會發生。在Android中,如果操作系統不能在大約5秒內執行一個代碼行,ANR對話框將彈出。從您的代碼中,您似乎正在閱讀並解析資產文件夾中的.epub文件。我對epub文件不熟悉,但我懷疑這是一個阻礙你的應用的繁重任務。

如何啓動一個線程或AsyncTask讀取該文件,以便在主線程不會受阻?

+0

+1準確信息的加載。更多信息,請在AsyncTask中執行後臺任務。 –

-1

更多的文件讀取代碼的形式onCreaate()onResume().由於數據將被讀取一次的UI展現給用戶