2017-02-06 95 views
-4

下面的方法我用來打開一個記事本文件,我保存在我的原始文件夾中。屏幕變空白,應用程序強制關閉自己

每當我打開這個方法,一個新的空白屏幕打開(全黑),並且app強制自己關閉。

public class Fest1 extends AppCompatActivity { 

TextView text; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_fest1); 
    text=(TextView)findViewById(R.id.textV); 
    text.setText(readTxt()); 
} 
private String readTxt(){ 

    InputStream inputStream=getResources().openRawResource(R.raw.text); 
    ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream(); 
    int i; 
    try{ 
     i=inputStream.read(); 
     while (i!=1) 
     { 
      byteArrayOutputStream.write(i); 
      i = inputStream.read(); 
     } 
     inputStream.close(); 
     }catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 

    return byteArrayOutputStream.toString(); 
    } 

    } 

日誌:

02-06 10:02:11.345 4026-4026/com.example.krishna.festival I/art: Starting a blocking GC Alloc 02-06 10:02:11.345 4026-4026/com.example.krishna.festival I/art: Starting a blocking GC Alloc 02-06 10:02:11.349 4026-4026/com.example.krishna.festival I/art: Starting a blocking GC Alloc 02-06 10:02:11.357 4026-4026/com.example.krishna.festival I/art: Alloc partial concurrent mark sweep GC freed 6(144B) AllocSpace objects, 1(64MB) LOS objects, 2% free, 132MB/136MB, paused 632us total 7.967ms 02-06 10:02:11.357 4026-4026/com.example.krishna.festival I/art: Starting a blocking GC Alloc 02-06 10:02:11.364 4026-4026/com.example.krishna.festival I/art: Alloc concurrent mark sweep GC freed 3(72B) AllocSpace objects, 0(0B) LOS objects, 2% free, 132MB/136MB, paused 601us total 6.660ms 02-06 10:02:11.364 4026-4026/com.example.krishna.festival I/art: Forcing collection of SoftReferences for 256MB allocation 02-06 10:02:11.364 4026-4026/com.example.krishna.festival I/art: Starting a blocking GC Alloc 02-06 10:02:11.371 4026-4026/com.example.krishna.festival I/art: Alloc concurrent mark sweep GC freed 3(72B) AllocSpace objects, 0(0B) LOS objects, 2% free, 132MB/136MB, paused 599us total 6.876ms 02-06 10:02:11.371 4026-4026/com.example.krishna.festival W/art: Throwing OutOfMemoryError "Failed to allocate a 268435468 byte allocation with 4194304 free bytes and 251MB until OOM" 02-06 10:02:11.371 4026-4026/com.example.krishna.festival I/art: Starting a blocking GC Alloc 02-06 10:02:11.371 4026-4026/com.example.krishna.festival I/art: Starting a blocking GC Alloc 02-06 10:02:11.376 4026-4026/com.example.krishna.festival I/art: Starting a blocking GC Alloc 02-06 10:02:11.381 4026-4026/com.example.krishna.festival I/art: Alloc partial concurrent mark sweep GC freed 6(144B) AllocSpace objects, 0(0B) LOS objects, 2% free, 132MB/136MB, paused 585us total 5.443ms 02-06 10:02:11.382 4026-4026/com.example.krishna.festival I/art: Starting a blocking GC Alloc 02-06 10:02:11.388 4026-4026/com.example.krishna.festival I/art: Alloc concurrent mark sweep GC freed 3(72B) AllocSpace objects, 0(0B) LOS objects, 2% free, 132MB/136MB, paused 599us total 6.748ms 02-06 10:02:11.389 4026-4026/com.example.krishna.festival I/art: Forcing collection of SoftReferences for 256MB allocation 02-06 10:02:11.389 4026-4026/com.example.krishna.festival I/art: Starting a blocking GC Alloc 02-06 10:02:11.396 4026-4026/com.example.krishna.festival I/art: Alloc concurrent mark sweep GC freed 3(72B) AllocSpace objects, 0(0B) LOS objects, 2% free, 132MB/136MB, paused 612us total 7.157ms 02-06 10:02:11.396 4026-4026/com.example.krishna.festival I/art: Starting a blocking GC HomogeneousSpaceCompact 02-06 10:02:11.402 4026-4026/com.example.krishna.festival I/art: HomogeneousSpaceCompact marksweep + semispace GC freed 0(0B) AllocSpace objects, 0(0B) LOS objects, 2% free, 132MB/136MB, paused 5.670ms total 5.670ms 02-06 10:02:11.402 4026-4026/com.example.krishna.festival W/art: Throwing OutOfMemoryError "Failed to allocate a 268435468 byte allocation with 4194304 free bytes and 251MB until OOM" 02-06 10:02:11.402 4026-4026/com.example.krishna.festival D/AndroidRuntime: Shutting down VM 02-06 10:02:11.402 4026-4026/com.example.krishna.festival E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.krishna.festival, PID: 4026 java.lang.OutOfMemoryError: Failed to allocate a 268435468 byte allocation with 4194304 free bytes and 251MB until OOM at java.util.Arrays.copyOf(Arrays.java:3256) at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:113) at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93) at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:122) at com.example.krishna.festival.Fest1.readTxt(Fest1.java:32) at com.example.krishna.festival.Fest1.onCreate(Fest1.java:21) at android.app.Activity.performCreate(Activity.java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

+1

請分享相關日誌,以便我們可以看到異常和堆棧跟蹤 – yakobom

+1

請添加您的logcat – AbhayBohra

+1

'while(i!= 1)' - surely'while(i!= - 1)'? –

回答

1

inputStream.read()返回-1在流的結尾。你的while (i!=1)檢查1而不是-1並進入無限循環。這增加了byteArrayOutputStream到一個巨大的大小,最終超過內存並導致崩潰。修復您的while聲明。

+0

謝謝。有效 ! –

0

仔細看日誌。相關的線路有:

com.example.krishna.festival, PID: 4026 java.lang.OutOfMemoryError: Failed to allocate a 268435468 byte allocation with 4194304 free bytes and 251MB until OOM at 

java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:122) at 

所以,基本上,你的字節數組輸出流寫入時引起內存溢出異常。

正如@degs所示,這是因爲你有一個無限循環,會導致你的程序繼續寫入流,直到內存不足。