这是一个简单的应用程序,应该选择随机行从一个文本文件。我尝试安装android 8.0,因为我曾经得到的最后一个错误是,我需要26级或更高。我是新的android工作室,我不明白崩溃日志
package com.example.exampleapp
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.annotation.RequiresApi
import kotlin.random.Random
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import java.util.stream.Stream
class MainActivity : AppCompatActivity() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val rollButton = findViewById<Button>(R.id.rollButton)
val resultsTextView = findViewById<TextView>(R.id.textView)
rollButton.setOnClickListener {
val i = Random.nextInt(1, 9)
val line = Files.readAllLines(Paths.get("chal.txt")).get(i)
resultsTextView.text = line
}
}
}
启动应用程序后,它立即崩溃。以下是日志:
2022-12-05 13:01:22.217 8637-8637/com.example.exampleapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.exampleapp, PID: 8637
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:558)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Caused by: java.nio.file.NoSuchFileException: chal.txt
at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
at java.nio.file.Files.newByteChannel(Files.java:361)
at java.nio.file.Files.newByteChannel(Files.java:407)
at java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384)
at java.nio.file.Files.newInputStream(Files.java:152)
at java.nio.file.Files.newBufferedReader(Files.java:2784)
at java.nio.file.Files.readAllLines(Files.java:3202)
at java.nio.file.Files.readAllLines(Files.java:3242)
at com.example.exampleapp.MainActivity.onCreate$lambda$0(MainActivity.kt:28)
at com.example.exampleapp.MainActivity.$r8$lambda$qUvL8i7QyFzeRpgsUagxdHIDpUc(Unknown Source:0)
at com.example.exampleapp.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
at android.view.View.performClick(View.java:7506)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131)
at android.view.View.performClickInternal(View.java:7483)
at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
at android.view.View$PerformClick.run(View.java:29334)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7872)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
我真的不明白
1条答案
按热度按时间szqfcxe21#
你好:D崩溃日志有两个重要部分:
MainActivity.kt:28
和java.nio.file.NoSuchFileException: chal.txt
第二个错误是声明“chal.txt”实际上不存在,当它试图读取它时,实习生崩溃了应用程序。为了支持这一点,我相信
val line...
(它试图读取“chal.txt”)是第28行。让我知道这是否有帮助:)
编辑:
这是在回答“我不明白崩溃日志”。我不知道如何解决你的问题,但至少帮助你了解问题:)