BrianGogle Maimone
BrianGogle Maimone

Reputation: 17

Android Studio doesn't show any errors in my code but gradle does. What to try

Using android studio. I went through my project 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. I tried closing and reopening android studio. No luck. 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 get Error running app: Gradle project sync failed. Please fix your project and try again. Would appreciate any help. Code below.

Java file:

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);
}
}

Xml file:

<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"/>

<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>

Upvotes: 0

Views: 4331

Answers (3)

Priya Singhal
Priya Singhal

Reputation: 1291

Check gradle console to know why gradle sync failed. It's at bottom right side.

Did u imported the project from eclipse.?

Check your SDK manger to know if you have build tool and android support installed with same version that you are using.

Upvotes: 1

Charaf Eddine Mechalikh
Charaf Eddine Mechalikh

Reputation: 1248

try this : open your sdk path and open the build-tools folder this folder must contains 1 or more folder that the name is like xx.x.x where 'x' is a number , example

\android-sdk\build-tools\19.0.0

and then open platforms folder (in sdk folder) and you will see folders that names are "android-xx" ,example

\android-sdk\platforms\android-19

in your build.gradle

compileSdkVersion 19<-same as the folder (android-19)
buildToolsVersion "19.0.0"<- same as the folder (19.0.0)

enter image description here

Upvotes: 0

gkondati
gkondati

Reputation: 526

There is no problem with your android code, you need clean the project first.

And then delete build files in you project(Include lib build files).

And close the Studio and restart again. Build the project . I hope it will help you.

Upvotes: 0

Related Questions