Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Simple Android App with Kotlin Basic Android Programming Initializing a Button

Initialize the showFactButton variable using the findViewById() method like the TextView above it.

The ID for the button is showFactButton, and don't forget to cast it to a Button with as Button!

However, it gets "Unable to access jarfile test.jar" error!!!

FunFactsActivity.kt
import android.app.Activity
import android.os.Bundle
import android.widget.TextView
import android.widget.Button

class FunFactsActivity : Activity() {
  override fun onCreate(savedInstanceState: Bundle) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_fun_facts)

    // Declare our View variables and assign them the Views from the layout file
    val factLabel = findViewById(R.id.factTextView) as TextView
    Button showFactButton = (Button) findViewById(R.id.showFactButton);
  }
}

2 Answers

Hi Hannah. It's just that you initialized your showFactButton variable in Java. In this course, you need to do it in Kotlin.

I won't give you the answer but just follow the example of the line right above, for the factLabel variable:

val factLabel = findViewById(R.id.factTextView) as TextView

I'm sure you'll get it right. But if you need further help, ask away :)

you are not helping, tried many things and wont solve the problem, be explicit we are trying to learn, and getting more confused will actually kill the enthusiasm

Farai Ted Mandoreba, I'm sorry you see it as not helping nor being explicit. However, bear in mind that this question refers to the "Build a Simple App with Kotlin" course. I believe the questions you posted on the forum (which I answered) were related to the "Build a Simple App with Java" course. Different languages, but I know it's easy to miss when running a search. So this post will definitely not help you I'm afraid.

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 90,705 Points

Sounds like it might be an issue that can be solved with simply a rebuild or a clean of your project in AS Studio. Try this and preview your project again and it should work. :-)