Its a simple app that is supposed to choose random line from a text file.
I tried installing android 8.0 because last error I ever got was that I require level 26 or higher. I am new to android studio and I don’t understand the crash log
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
}
}
}
After launching the app it immediately crashes. Here’s the log:
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)Â
I don’ really understand it
>Solution :
It looks like your app is trying to read a file called "chal.txt" but the file doesn’t exist. This is causing a NoSuchFileException to be thrown, which is crashing your app.
To fix this, you’ll need to make sure that the "chal.txt" file exists in the correct location on your device. This is typically the app’s "assets" directory, which is a directory that contains files that are packaged with the app. You can access files in the assets directory by using the AssetManager, which is a class provided by Android that allows you to access an app’s assets.
Here’s an example of how you could use the AssetManager to read the "chal.txt" file and get a list of all the lines in the file:
val assetManager = this.assets
val inputStream = assetManager.open("chal.txt")
val lines = inputStream.bufferedReader().readLines()
Once you have the list of lines, you can use the Random class to choose a random line from the list and display it in your app.
Here’s an example of how you could do that:
val i = Random.nextInt(lines.size)
val line = lines[i]
resultsTextView.text = line
I hope this helps! Let me know if you have any other questions.