屏幕变为空白,应用程序强制自行关闭

时间:2017-02-06 05:03:42

标签: java android

我用以下方法打开保存在原始文件夹中的记事本文件。

每当我打开此方法时,都会打开一个新的空白屏幕(全黑)并且app force自行关闭。

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我/艺术:   启动阻止GC Alloc 02-06 10:02:11.345   4026-4026 / com.example.krishna.festival我/艺术:开始阻止GC   Alloc 02-06 10:02:11.349 4026-4026 / com.example.krishna.festival我/艺术:   启动阻止GC Alloc 02-06 10:02:11.357   4026-4026 / com.example.krishna.festival我/艺术:Alloc部分并发   mark sweep GC释放了6个(144B)AllocSpace对象,1个(64MB)LOS对象,   2%免费,132MB / 136MB,暂停632us总计7.967ms 02-06 10:02:11.357   4026-4026 / com.example.krishna.festival我/艺术:开始阻止GC   Alloc 02-06 10:02:11.364 4026-4026 / com.example.krishna.festival我/艺术:   Alloc并发标记扫描GC释放3(72B)AllocSpace对象,0(0B)   LOS对象,2%免费,132MB / 136MB,暂停601us总计6.660ms 02-06   10:02:11.364 4026-4026 / com.example.krishna.festival我/艺术:强迫   用于256MB分配的SoftReferences集合02-06 10:02:11.364   4026-4026 / com.example.krishna.festival我/艺术:开始阻止GC   Alloc 02-06 10:02:11.371 4026-4026 / com.example.krishna.festival我/艺术:   Alloc并发标记扫描GC释放3(72B)AllocSpace对象,0(0B)   LOS对象,2%免费,132MB / 136MB,暂停599us总计6.876ms 02-06   10:02:11.371 4026-4026 / com.example.krishna.festival W / art:投掷   OutOfMemoryError"无法分配268435468字节分配   4194304个空闲字节和251MB直到OOM" 02-06 10:02:11.371   4026-4026 / com.example.krishna.festival我/艺术:开始阻止GC   Alloc 02-06 10:02:11.371 4026-4026 / com.example.krishna.festival我/艺术:   启动阻止GC Alloc 02-06 10:02:11.376   4026-4026 / com.example.krishna.festival我/艺术:开始阻止GC   Alloc 02-06 10:02:11.381 4026-4026 / com.example.krishna.festival I / art:   Alloc部分并发标记扫描GC释放6(144B)AllocSpace   对象,0(0B)LOS对象,2%免费,132MB / 136MB,暂停585us   5.443ms 02-06 10:02:11.382 4026-4026 / com.example.krishna.festival我/艺术:开始阻止GC Alloc 02-06 10:02:11.388   4026-4026 / com.example.krishna.festival我/艺术:Alloc并发标记   扫描GC释放3(72B)AllocSpace对象,0(0B)LOS对象,2%免费,   132MB / 136MB,暂停599us总计6.748ms 02-06 10:02:11.389   4026-4026 / com.example.krishna.festival I / art:强制收集   SoftReferences为256MB分配02-06 10:02:11.389   4026-4026 / com.example.krishna.festival我/艺术:开始阻止GC   Alloc 02-06 10:02:11.396 4026-4026 / com.example.krishna.festival I / art:   Alloc并发标记扫描GC释放3(72B)AllocSpace对象,0(0B)   LOS对象,2%免费,132MB / 136MB,暂停612us,总计7.157ms 02-06   10:02:11.396 4026-4026 / com.example.krishna.festival我/艺术:开始一个   阻止GC HomogeneousSpaceCompact 02-06 10:02:11.402   4026-4026 / com.example.krishna.festival I / art:HomogeneousSpaceCompact   marksweep + semispace GC释放0(0B)AllocSpace对象,0(0B)LOS   对象,2%免费,132MB / 136MB,暂停5.670ms总计5.670ms 02-06   10:02:11.402 4026-4026 / com.example.krishna.festival W / art:投掷   OutOfMemoryError"无法分配268435468字节分配   4194304个空闲字节和251MB直到OOM" 02-06 10:02:11.402   4026-4026 / com.example.krishna.festival D / AndroidRuntime:关闭   VM 02-06 10:02:11.402 4026-4026 / com.example.krishna.festival   E / AndroidRuntime:致命异常:主要                                                                               过程:com.example.krishna.festival,PID:4026                                                                               java.lang.OutOfMemoryError:无法分配268435468字节   分配4194304个空闲字节和251MB直到OOM                                                                                   在java.util.Arrays.copyOf(Arrays.java:3256)                                                                                   at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:113)                                                                                   在   java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)                                                                                   at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:122)                                                                                   在com.example.krishna.festival.Fest1.readTxt(Fest1.java:32)                                                                                   在com.example.krishna.festival.Fest1.onCreate(Fest1.java:21)                                                                                   在android.app.Activity.performCreate(Activity.java:6679)                                                                                   在   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)                                                                                   在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)                                                                                   在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)                                                                                   在android.app.ActivityThread.-wrap12(ActivityThread.java)                                                                                   在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477)                                                                                   在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                   在android.os.Looper.loop(Looper.java:154)                                                                                   在android.app.ActivityThread.main(ActivityThread.java:6119)                                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                                   在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)                                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

2 个答案:

答案 0 :(得分:1)

inputStream.read()在流结束时返回-1。您的while (i!=1)检查1而不是-1并进入无限循环。这会使byteArrayOutputStream增加到一个巨大的大小,最终会超出内存并导致崩溃。修复您的while声明。

答案 1 :(得分: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所指出的那样,这是因为你有一个无限循环会导致你的程序继续写入Stream,直到你的内存不足为止。