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 (2014) Basic Android Programming Making a Button Do Something

Josue Castaneda
Josue Castaneda
360 Points

Autocomplete not working

I'm typing in showFactButton.setO but autocomplete doesn't have the setOnClickListener method. It doesn´t give me any options, when I type in showFactButton.s it gives me only 2 options, sout and synchronized

3 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Sorry for the late reply here! Can you paste in the code you are trying to write? It sounds like your showFactButton variable might be the wrong data type.

Josue Castaneda
Josue Castaneda
360 Points

This is the code I have so far, but when I start to type setOn after the dot, the only options that autocomplete gives me is sout and synchonized.

Textview factLabel = (TextView) findViewById(R.id.factTextView);

Button showFactButton = (Button) findViewById(R.id.showFactButton);

showFactButton.s

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Can you paste in the code from your entire MainActivity.java file? There must be a wrong import or curly brace or some other syntax error causing you trouble.

Josue Castaneda
Josue Castaneda
360 Points

This is the code I have so far, but when I start to type setOn after the dot, the only options that autocomplete gives me is sout and synchonized.

Textview factLabel = (TextView) findViewById(R.id.factTextView);

Button showFactButton = (Button) findViewById(R.id.showFactButton);

showFactButton.s

I am having a similar issue. I manually entered "TextView" and "Button" (but starting with lower case). I got errors even after I changed them to upper case: "Cannot resolve the symbol 'TextView' ", and same for 'Button'. Below is the code prior to starting both lines over. They did not throw any errors prompting me to create the casts until I started over on both lines. I am concerned that I will be chasing bugs in correct code. Is there anything I can do (other than the obvious-attention to detail) that will get studio to recognize changes to the proper syntax?

package com.teamtreehouse.funfactsexercise;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class FunFactsActivity extends AppCompatActivity {

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

        //define our view variables and assign from the layout file
        TextView factLabel= findViewById(R.id.FactTextView);
        Button showFactButton= findViewById(R.id.showFactButton);


        showFactButton.setOnClickListener();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_fun_facts, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
Ben Jakuben
Ben Jakuben
Treehouse Teacher

You are missing the import statements for TextView and Button. They should get added automatically, but it's possible you need to enable that feature. It's been so long--I don't remember if that's a default or not!

Here's a description and gif from https://github.com/codepath/android_guides/wiki/Using-Android-Studio:

By default, Android Studio does not perform auto-imports. If you want to enable it, go to Preferences->Editor->Auto Import, click the checkbox "Add unambiguous imports on the fly" but make sure to exclude android.R auto imports since it can often cause conflicts with your own resource imports. You may also wish to Optimize Imports on the Fly, but you can also type Command-Option-O to do it manually:"

auto import

Thanks! It is not a default setting, but I have it on now.