Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vogella Android Tutorial, Compiler error in Android Studio

Tags:

java

android

I am new to Android development and following the Vogella Introduction to Android development with Android Studio - Tutorial located here:

http://www.vogella.com/tutorials/Android/article.html#androidstudio_starter

I started having a problem at step 19.4 and on. I have exact same code as shown in tutorial but Android Studio shows an error in MainActivity.java stating it cannot resolve symbol 'Constants' and build fails with compiler error. I would like to know what I am missing since every step has been followed and all code matches that listed in the tutorial.

MainActivity.java

package com.deluxaur.testapp;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import android.widget.EditText;


public class MainActivity extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (BuildConfig.DEBUG) {
        Log.d(Constants.LOG, "onCreated called");
    }
    setContentView(R.layout.activity_main);
}

public void onCLick(View view) {
    EditText input = (EditText) findViewById(R.id.main_input);
    String string = input.getText().toString();
    Toast.makeText(this, "Button 1 pressed", Toast.LENGTH_LONG).show();
}

activity_main.xml

<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=".MainActivity">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/main_input"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="true"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start"
    android:id="@+id/button"
    android:layout_alignLeft="@+id/main_input"
    android:layout_below="@+id/main_input"
    android:layout_marginTop="31dp"
    android:onClick="onClick"
    />

like image 638
deluxaur Avatar asked May 15 '26 15:05

deluxaur


1 Answers

It is just saying that Constants class is not recognized. There are many ways you can fix this.

  1. You may create Constants class with static LOG string variable in it

    public class Constants {
        public static final String LOG = "MyLogTag";
    }
    
  2. The following code is optional and not needed for the functionality of your program. It is there to help provide some extra logs in order for you to have a better understanding of what is happening in your application but is not necessary.

    //These three lines are optional
    if (BuildConfig.DEBUG) {
       Log.d(Constants.LOG, "onCreated called");
    }
    
  3. Or you can simply change the String key. The Log.d() method takes two Strings the first is used for the tag and the second is the message. If you just provide another valid String it will work fine.

    if (BuildConfig.DEBUG) {
       Log.d("A KEY", "onCreated called");
    }
    
like image 86
Sheychan Avatar answered May 18 '26 05:05

Sheychan