文章目录
一、报错信息
二、解决方案
一、报错信息
2021-05-07 13:24:24.672 29512-29512/kim.hsl.a7_zip E/AndroidRuntime: FATAL EXCEPTION: main Process: kim.hsl.a7_zip, PID: 29512 java.lang.RuntimeException: Unable to start activity ComponentInfo{kim.hsl.a7_zip/kim.hsl.a7_zip.MainActivity}: java.lang.IllegalThreadStateException: process hasn't exited at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2951) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6718) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.IllegalThreadStateException: process hasn't exited at java.lang.UNIXProcess.exitValue(UNIXProcess.java:215) at kim.hsl.a7_zip.MainActivity.compress7z(MainActivity.kt:115) at kim.hsl.a7_zip.MainActivity.onCreate(MainActivity.kt:24) at android.app.Activity.performCreate(Activity.java:7144) at android.app.Activity.performCreate(Activity.java:7135) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2931) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6718) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
问题描述 : 使用 Runtime.getRuntime().exec(cmd) 执行命令 , 如果实时打印出命令执行的命令行信息 , 使用如下代码 :
var process: Process = Runtime.getRuntime().exec(cmd) // 读取命令执行过程数据 var reader = BufferedReader(InputStreamReader(process.inputStream)) while (true) { val line = reader.readLine() if (line != null) { Log.i(TAG, "$line") }else{ break } }
执行完毕后 , 调用如下代码查询执行结果 , 报上述错误 ;
process.exitValue()
二、解决方案
出现问题原因 : 读取命令执行过程的 BufferedReader 没有关闭 ;
正确用法 :
var process: Process = Runtime.getRuntime().exec(cmd) // 读取命令执行过程数据 var reader = BufferedReader(InputStreamReader(process.inputStream)) while (true) { val line = reader.readLine() if (line != null) { Log.i(TAG, "$line") }else{ break } } reader.close() val exitValue = process.exitValue()
使用 BufferedReader 读取命令行执行过程 , 读取完毕后 , 及时关闭 BufferedReader ;