2015-10-04 61 views
0

我有一個奇怪的問題。我有這個android應用程序應該從一個導致文本文件的URL中獲取一些數據。它有18行,每行一個項目,看起來是這樣的:掛在按鈕上按

萬事達

(這是一個任務,所有的數據顯然是假的)

我有這樣的:

public class CreditCard { 

    //set up method to get and return data as arraylist 
    public ArrayList<String> ccArray(){ 
     ArrayList<String> ccInfo = new ArrayList<String>(); 

     //array set up. time to get data 
     try { 
      URL urlDat = new URL("url"); 
      Scanner urlScn = new Scanner(urlDat.openStream()); 
      while (urlScn.hasNext()){ 
       ccArray().add(urlScn.nextLine() + "\n"); 
      } 

     }catch (MalformedURLException URLe){ 
      //handle exception 

     } 
     catch (IOException IOe){ 
      //handle exception 
     } 
     return ccInfo; 
    } 
} 

這樣我就可以通過陣列的主要方法(或類的另一種方法,不知道然而)。我有一個按鈕,運行中的主要方法displayCreditCards:

public void displayCreditCards(View view) throws IOException { 
     TextView tv1; 

     tv1 = (TextView) findViewById(R.id.text_main); 
     tv1.setText("Data will appear here"); 

     //create objects to det data and make profiles 
     CreditCard ccObj = new CreditCard(); 


     tv1.setText(ccObj.ccArray().toString()); 
} 

然而,這會導致應用程序沒有響應,並從我的Android得等待/關閉對話框。我不知道如何解釋logcat:

10-04 18:27:01.204 9036-9036/? I/art﹕ Not late-enabling -Xcheck:jni (already on) 
10-04 18:27:01.204 9036-9036/? I/art﹕ Late-enabling JIT 
10-04 18:27:01.206 9036-9036/? I/art﹕ JIT created with code_cache_capacity=2MB compile_threshold=1000 
10-04 18:27:01.257 9036-9036/? W/System﹕ ClassLoader referenced unknown path: /data/app/com.example.pat.displaycc-2/lib/x86 
10-04 18:27:01.396 9036-9058/com.example.pat.displaycc D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true 
10-04 18:27:01.400 9036-9036/com.example.pat.displaycc D/﹕ HostConnection::get() New Host Connection established 0xad7ad9a0, tid 9036 
10-04 18:27:01.468 9036-9058/com.example.pat.displaycc D/﹕ HostConnection::get() New Host Connection established 0xad7ade40, tid 9058 
10-04 18:27:01.471 9036-9058/com.example.pat.displaycc I/OpenGLRenderer﹕ Initialized EGL, version 1.4 
10-04 18:27:01.520 9036-9058/com.example.pat.displaycc W/EGL_emulation﹕ eglSurfaceAttrib not implemented 
10-04 18:27:01.521 9036-9058/com.example.pat.displaycc W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xad7917c0, error=EGL_SUCCESS 
10-04 18:27:04.841 9036-9036/com.example.pat.displaycc W/art﹕ Before Android 4.1, method int android.support.v7.internal.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView 
10-04 18:27:04.874 9036-9058/com.example.pat.displaycc W/EGL_emulation﹕ eglSurfaceAttrib not implemented 
10-04 18:27:04.874 9036-9058/com.example.pat.displaycc W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xad791c20, error=EGL_SUCCESS 
10-04 18:27:06.138 9036-9058/com.example.pat.displaycc E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab80f0d0 
10-04 18:27:11.564 9036-9058/com.example.pat.displaycc W/EGL_emulation﹕ eglSurfaceAttrib not implemented 
10-04 18:27:11.564 9036-9058/com.example.pat.displaycc W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xad791c00, error=EGL_SUCCESS 
10-04 18:27:13.108 9036-9058/com.example.pat.displaycc E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xab80f060 

我不知道爲什麼它掛在我身上,有什麼想法?

+0

網絡調用只能在Main(UI)以外的線程中完成。從線程調用這個'ccArray()'(像Handle,AsyncTask,Thread)。 –

回答

0

我認爲問題在於使用後不要關閉掃描儀。你應該總是在try-catch之後添加一個finally {}塊,你應該調用

urlScan.close() and 
urlDat.close() 
+0

當我添加一個finally {}時,我收到了無法解析的符號。當我在while循環之後添加它時沒有錯誤,但現在它崩潰並出現內存不足錯誤。 – royalflush5

+0

哎呀!你也應該關閉urlDat輸入流。 urlDat.close() – Theodore