1

I installed Android Studio about two days ago, I also installed the most updated version of Java and JDK 1.8 (I think it was Java SE 8u101). I am able to edit code and I'm following a tutorial from the official Android Studio website. (Here: https://developer.android.com/training/basics/firstapp/building-ui.html)

However, upon hitting run I've gotten the two following errors:

  1. Error:(24, 57) error: cannot find symbol variable activity_display_message

    Code for this:

    ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);

  2. Error:(33, 25) error: cannot find symbol variable EXTRA_MESSAGE

    Code for this:

    intent.putExtra(EXTRA_MESSAGE, message);

What might I be doing wrong? The emulator pops up and I can get to other apps on the phone but I just can't get my app to load. Should I have installed Java SE 8u102 instead?

I also got a second error message:

Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details.

Any help is appreciated.

Below is my code for the first xml file, "activity_main.xml":

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="sendMessage" />
</LinearLayout>

Below is my code for "activity_display_message.xml"

<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.appno1.DisplayMessageActivity">

</RelativeLayout>
Jeff
  • 11
  • 1
  • 1
  • 4
  • Check what layout u inflated with setContentView(layoutId). Then go to the layout and check if there is a view with id of activity_display_message. Obviously not. – ugur Aug 14 '16 at 01:11
  • The second error can be ignored because it's a result of the first two. To figure out what's going on with the first two errors, can you post the actual xml of the layout? that's where the problem is with error 1 and that more than likely leads to # 2 error(33,25) – Hank Wilson Aug 14 '16 at 01:17
  • @UğurB I believe that should be included. I copied and pasted everything directly from the tutorial, since I'm using this as practice. I still have the same two errors. – Jeff Aug 14 '16 at 01:23

5 Answers5

5

Add this to your MainActivity:

public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

You might have missed this line, which can be found in the "Build an Intent" part.

Refer this for more, from the original page.

MJH
  • 2,301
  • 7
  • 18
  • 20
BluePie
  • 135
  • 3
  • 9
2

I think you might lose the last note in the tutorial. android note description

so you just add android:id attribute to the activity_display_message.xml file and it will be ok!oh actually you should add into the layout element which is the valid location.

Crabime
  • 644
  • 11
  • 27
0

My advice is, try to clean the project and build it again. If you want to access newly added resources you need to build the project. Cleaning will help to remove all previously autogenerated resources from project and building will help to create them again + new ones.

HPV121
  • 3
  • 2
0

You should add EXTRA_MESSAGE as static final, like:

private static final String EXTRA_MESSAGE = "message";
Sufian
  • 6,405
  • 16
  • 66
  • 120
  • This got rid of the first error, thank you! Any suggestions on the second part, "activity_display_message"? – Jeff Aug 14 '16 at 02:40
0

Regarding this error,

ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);

check that the file res > layout > activity_display_message.xml as the id specified as android:id="@+id/activity_display_message"

n00b
  • 1