简体   繁体   中英

Vogella Android Tutorial, Compiler error in Android Studio

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

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 String s 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"); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM