Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android error on tutorial cannot find symbol variable activity_display_message

I am trying to learn android app building through tutorials and Android Studio. Some of the comments regarding xml and imports were helpfult. I am down to one error. I get this error Error:(22, 57) error: cannot find symbol variable activity_display_message

The errors regarding imports I have fixed with some searching on stack flow. I am missing something

DisplayMessageActivity

package com.example.rpinto.myfirstapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.TextView;

public class DisplayMessageActivity extends AppCompatActivity {

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

        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
        layout.addView(textView);
    }
}

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.rpinto.myfirstapp.DisplayMessageActivity"
    tools:showIn="@layout/activity_display_message"
    android:id="@+id/content">
</RelativeLayout>

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "24.0.1"

    defaultConfig {
        applicationId "com.example.rpinto.myfirstapp"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
}
like image 309
ryan.pinto Avatar asked Aug 04 '16 04:08

ryan.pinto


4 Answers

I had this same problem. Under the "Build an Intent" section in the Android Studio instructions, it's easy to miss this line:

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

Make sure this is included above the @Override in MainActivity.java and you're all set :)

like image 159
Joey Avatar answered Oct 23 '22 00:10

Joey


Don't know if anyone still need the answer to this, but I figured it out like this:

I saw this in the tutorial:

Note: The XML layout generated by previous versions of Android Studio might not include the android:id attribute. The call findViewById() will fail if the layout does not have the android:id attribute. If this is the case, open activity_display_message.xml and add the attribute android:id="@+id/activity_display_message" to the layout element.

So, under Res -> layout -> activity_display_message.xml, I entered the line

android:id="@+id/activity_display_message"

Anywhere inside the RelativeLayout tags. In my case specifically, I randomly shoved it in-between the android:paddingTop and tools:context fields

Hope this helps :D

like image 33
AlgoRythm Avatar answered Oct 22 '22 23:10

AlgoRythm


I think:

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

Should be:

RelativeLayout layout = (RelativeLayout ) findViewById(R.id.content);
like image 2
Huy Avatar answered Oct 22 '22 22:10

Huy


Here is the updated solution for this.

Intent is a subclass of Main Activity so there's no need to specify explicitly. That means we can call EXTRA_MESSAGE, instead of MainActivity.

EXTRA_MESSAGE

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);

    Intent intent = getIntent();
    String message = intent.getStringExtra(EXTRA_MESSAGE);
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
    layout.addView(textView);
like image 1
Vinay Kumar P Avatar answered Oct 23 '22 00:10

Vinay Kumar P