0

I'm trying to make a random generator app for streamlining random quests in a tabletop game. The goal is to hit the generate button and have the results shown in the textview on activity page as a list. When I hit "Run" the code runs and I see the list of generated results in the console at the bottom of android studio, however it's not showing any text in the text view. I am extremely new to coding and I'm sure i'm missing something simple but i've been stuck for days and could use some help. I am not getting any errors, although i'm also not sure i'm using the main function right either. So far I have the main activity with a button that takes you to the second activity (where i want the random results to show). Any help is greatly appreciated.

fun main(args: Array<String>) {
val questGiver = arrayOf("John", "notJohn")
val random = Random()
val randomQuestGiver = 
questGiver[random.nextInt(questGiver.size)]
println("Quest Giver: $randomQuestGiver")
print(args.contentToString())
}
  • Android applications don't use `main` as the entry point. Where is your activity class? Where do you define the text view, and where do you try to use it? – Slaw May 25 '23 at 01:37
  • I thought Main was the entry point for it to read code? (very new i apologize) my class "secondActivity" is just above the overide fun oncreate, with the main fun just below it. in my xml i see the textid and the android:text="hello world" however i cant seem to get the text to display the list of results instead of whats in quotes. Is there a way to have the xml pull from a function? – jack kelley May 25 '23 at 01:43
  • In "normal" programs, that would be true. But Android is a framework and has its own way of doing things (there may be a `main` method behind the scenes, but you don't deal with it). In Android, you define an `Activity` and that functions as the entry point. You might want to check out [Build your first Android app](https://developer.android.com/training/basics/firstapp). Once you understand the fundamentals, _then_ you can start to build your own application. Also, just in case, note that Kotlin is a programming language _used_ to write Android applications (i.e., Kotlin != Android). – Slaw May 25 '23 at 01:48
  • Also, if you're going to use Kotlin instead of Java, I recommend learning [Jetpack Compose](https://developer.android.com/jetpack/compose). It's better than defining your UI in XML files. But if you use XML, then you need to get the `TextView` object in code and set its text. See e.g., https://stackoverflow.com/questions/13452991/change-textview-text – Slaw May 25 '23 at 01:50
  • Gotcha, so i'll need to ignore the 'fun main' and figure out how to define my activity as a text view, then make a new function that reads the results and shows them as text? (thank you for the links! I've got hello world mastered now lol) is there a way i can send you my project to see if i should scrap it and start new or if its a formatting issue with me? – jack kelley May 25 '23 at 02:06
  • "_figure out how to define my activity as a text view_" – Not "as a text view". Within the activity, you can _get_ the text view object (which was defined in the XML layout file and given an ID), and then call methods on it. But again, I do recommend Jetpack Compose. All that said, I know the temptation to just start coding your own application is strong, but there's really no substitute for _learning first_. That first link I gave has more than just a basic "Hello World" tutorial. E.g. check out the course under "Start the Android Basics course" header. It will take time, but it will help you – Slaw May 25 '23 at 02:32
  • The downside with a complete beginner trying to start with Compose is that all the basic official tutorials on all the other topics in Android don't use it yet, so it could be overwhelming and get you sidetracked. – Tenfour04 May 25 '23 at 13:06
  • Android is basically this huge framework of *stuff* that your code talks to, and that runs code you put in certain places. You really have to learn the Android way of doing things - if you follow some tutorials, you'll be creating Activities with TextViews and putting text in them in no time! But like anything else, it's extra stuff you have to learn beyond the basic Kotlin language – cactustictacs May 25 '23 at 14:39

0 Answers0