Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App with Java Basic Android Programming Accessing Views in Code: Declaring Variables

mario tawil
mario tawil
181 Points

I cant declare a private TextView Nor a private Button

In Fun Fact application cant declare a private TextView Nor a private Button

package mariotawil.funfacts

import android.support.v7.app.AppCompatActivity import android.os.Bundle

class FunFActsActivity : AppCompatActivity() { // Decalre our view varibale private TextView FactTextView private Button ShowFactButton

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState) 
    setContentView(R.layout.activity_fun_facts)
}

}

please help me I am using android studio 3.1 on windows 10

2 Answers

Seth Kroger
Seth Kroger
56,413 Points

You seem to be mixing up Java and Kotlin in your code. The course you're on is Java-based but your code is mostly Kotlin with some Java-ish bits in it. There is a Kotlin-based version of the course. Are you sure you didn't mix them up?

Joshua Walczak
Joshua Walczak
10,854 Points

I also ran into this same issue. I copied the java code and it worked. However, is there a way to make java the default in android studio instead of having kotlin defaulted? Not sure how this happened. Or is it a better idea to just switch courses and learn android with kotlin since I believe kotlin is replacing java for android?(correct me if i'm wrong about that one)

mario tawil
mario tawil
181 Points

Dear Seth,

Thank you for your reply , can you help me how to switch to switch to java the file extension is ".KT".

Seth Kroger
Seth Kroger
56,413 Points

I'd create a new Java file by right-clicking the package name in the left side Android/Project panel and selecting New > Java Class from the menu that comes up, name it FunFactsActivity as you've and remove the .kt file. Use the following code for that Java class and you should be able to carry on from here:

package mariotawil.funfacts;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class FunFactsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}