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 (2014) Basic Android Programming Adding the onClick() Method

Brian Maimone
PLUS
Brian Maimone
Courses Plus Student 1,644 Points

After copying funfacts project files to new desktop I run the app and get Error running app: Gradle project sync failed

Gradle 'FunFacts' project refresh failed, Error: Read timed out. I also see Button showFactButton = (Button) findViewById(R.id.showFactButton); and the variable showFactButton in parenthesis is red. When I click on it, it says cannot resolve symbol showFactButton. Would appreciate help.

Ben Junya
Ben Junya
12,365 Points

Can you be a little more specific about your problem? It looks like a gradle problem, but I don't exactly know what is going on.

So you moved all your project files to a new desktop, is Android studio updated on your new desktop and do you have all the API's your using installed in Android Studio?

10 Answers

Ben Junya
Ben Junya
12,365 Points

Hey Brian,

Then it's definitely a gradle problem if what you're telling me is true. It's at the point where we need to isolate the problem.

Here's what I feel would be best for you:

  1. Start a new project, call it whatever you'd like
  2. Make sure the app itself in your new project can run on its own. Before adding any code, run the standard empty "Hello World!" App on your phone or emulator
  3. If that works, then copy all your code into the new project, that's XML files, drawables, etc. Don't copy the file into your project, just copy and paste the code itself into brand new Java Classes and new XML files. If it goes well, this should work.

If you can't complete step 2, hop back on the forum and shoot a comment in this thread. I won't be around as much as I was yesterday, but I'll be happy to help when I'm at my workstation.

Brian Maimone
PLUS
Brian Maimone
Courses Plus Student 1,644 Points

I downloaded android studio from the link on the video page so I think everything is the same from that point of view. Also, I was able to successfully run an earlier version of FunFacts with no errors. Here is my code:

package com.teamtreehouse.funfacts;

import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView;

public class FunFactsActivity extends Activity {

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

    //Declare our view variables and assign them the views from the layout file
    final TextView factlabel = (TextView) findViewById(R.id.factTextView);
    Button Showfactbutton = (Button) findViewById(R.id.Showfactbutton);
    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // The button was clicked so update the fact label with a new fact
            String fact = "Ostriches can run faster than horses";
            factlabel.setText(fact);

        }
    }
    Showfactbutton.setOnClickListener(listener)
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

Thanx

Ben Junya
Ben Junya
12,365 Points

Can you show me your gradle.build file for the app module?

Brian Maimone
PLUS
Brian Maimone
Courses Plus Student 1,644 Points

This is my build.gradle file. Is this the one you need?

apply plugin: 'com.android.application'

android { compileSdkVersion 19 buildToolsVersion "20.0.0"

defaultConfig {
    applicationId "com.teamtreehouse.funfacts"
    minSdkVersion 14
    targetSdkVersion 19
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}

dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) }

Ben Junya
Ben Junya
12,365 Points

Hmmm. Everything looks good so far...

Can you send a screenshot of what's going on in your MainActivity? If you hover over the red text and show me what's going on, that helps a ton too.

Let's solve this problem together :D

Brian Maimone
PLUS
Brian Maimone
Courses Plus Student 1,644 Points

Is this the file?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".FunFactsActivity" android:background="#ff51b46d">

<TextView
    android:text="Did you know!"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="24sp"
    android:textColor="#80ffffff" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Ants stretch when they wake up in the morning"
    android:id="@+id/factTextView"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="24sp"
    android:   />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Show another fun fact"
    android:id="@+id/Showfactbutton"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="@android:color/white" />

</RelativeLayout>

In the second textview section I get an error on the last statement where it just shows android: /> and the error says '=' expected. Thats the only error for this file.

Ben Junya
Ben Junya
12,365 Points

Ahhh! I found the problem!

You're missing parameters on that 2nd TextView!

Where it says: "android: />"

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Ants stretch when they wake up in the morning"
    android:id="@+id/factTextView"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="24sp" />
 <!-- I got rid of that last line and moved the "/>" up to this line -->

You may need to look at your previous code on your other machine to see what exactly was there.

Brian Maimone
PLUS
Brian Maimone
Courses Plus Student 1,644 Points

Strange, now I have no code errors but I'm still getting the gradle project sync failed message?

Ben Junya
Ben Junya
12,365 Points

Can you post the project sync failed message? Is it the same?

Are there any other errors in your activity?

Brian Maimone
PLUS
Brian Maimone
Courses Plus Student 1,644 Points

Error running app: Gradle project sync failed. Please fix your project and try again. 5:59:59 PM Update Info: Android Studio is ready to update. 6:00:53 PM Error running app: Gradle project sync failed. Please fix your project and try again. 6:02:20 PM Error running app: Gradle project sync failed. Please fix your project and try again. 6:03:26 PM Error running app: Gradle project sync failed. Please fix your project and try again.

Gradle 'FunFacts' project refresh failed

Error:Read timed out

No other errors

Ben Junya
Ben Junya
12,365 Points

There's a few things we can try here:

First on the top menu bar, click on Build > Clean Project

Second, click Build > Rebuild Project

If you are still getting errors, you need to comb through your whole project and make sure there aren't any errors. The reason your project isn't building is because there are errors in your code. Check on the left side if there are any red squigglies underneath any of your java files or XML layout files. If there's a red squiggly, then that means there's a code error in there, and it needs to be fixed.

I just noticed this too in your code above:

This line:

Showfactbutton.setOnClickListener(listener)

You're missing a semicolon

Showfactbutton.setOnClickListener(listener);

That should fix one error. Try it again and see what happens.

Remember, computers are only as smart as the people who program them. They follow instructions exactly as they are told.

Brian Maimone
PLUS
Brian Maimone
Courses Plus Student 1,644 Points

Morning Ben,

I went through the code line by line and there are no red highlights or squiggly underlines anymore (in both the .xml, .java files). In fact none of the project files have red underlines. The code also matches what is shown in the videos. I closed and reopened android studio. When I clean the project I get the error message: Failed to complete gradle execution - Cause: Read timed out. Secondly when I run the project I still get Error running app: Gradle project sync failed. Please fix your project and try again. Aghhh. Would appreciate any new ideas.

Brian Maimone
PLUS
Brian Maimone
Courses Plus Student 1,644 Points

Ben,

What I decided to do was take an earlier version of my project which worked and added the incremental code to it. That worked so I guess gradle is ok, but there must have been something in the java and or xml files that gradle didn't like. Many thanks.