Android error in tutorial cannot find symbol variable activity_display_message

I am trying to learn how to create Android applications through tutorials and Android Studio. Some comments regarding xml and import were helpfult. I reached one mistake. I get this error Error: (22, 57) error: cannot find character variable activity_display_message

Import errors that I fixed with some searches in the stack thread. I'm 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'
}
+4
source share
11 answers

I don’t know if anyone else needs an answer to this question, but I understood it as follows:

:

. XML, Android Studio, android: id. findViewById() , android: id. , activity_display_message.xml android: id = "@+ id/activity_display_message" .

, Res → layout → activity_display_message.xml

android:id="@+id/activity_display_message"

RelativeLayout. , , : paddingTop tools:

, : D

+4

. " " Android Studio :

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

, @Override MainActivity.java, :)

+3

:

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

:

RelativeLayout layout = (RelativeLayout ) findViewById(R.id.content);
+2

.

Intent - Main Activity, . , EXTRA_MESSAGE 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);
+1

, gradle, .

0
"@layout/activity_display_message"

. .

R.layout

setContentView(R.layout.activity_display_message);

, R.id

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

, , LayoutParams TextView, .

@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.content);
    layout.addView(textView);
}
0

MainActivity.java.

TextView tv = (TextView) findViewById(R.id.editText);

(R.id.editText);..... ​​(R.id.edit_message);

edit_message - , , (EditText) . Java, , , , . .

package com.example.josh.myfirstapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

    // Example of a call to a native method
    TextView tv = (TextView) findViewById(R.id.editText);
    tv.setText(stringFromJNI());
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }
}
0

: activity_display_message

, android: id activity_display_message.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.galo.myapplication.DisplayMessageActivity"
    android:id="@+id/activity_display_message">

</android.support.constraint.ConstraintLayout>
0

, MainActivity.java:

public class MainActivity extends AppCompatActivity {

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

"com.example.MyApplicationstart.MESSAGE", "MyApplicationstart" - .

0

I had this problem so many times trying to make this tutorial, and add to this the only answer that helped me, which removed MainActivity.from the part MainActivity.EXTRA_MESSAGE...

I also had to import the following into DisplayMessageActivity.java:

import static android.provider.AlarmClock.EXTRA_MESSAGE;

This completely solved all my mistakes and allowed me to continue the tutorial.

0
source

I had the same problem. The error was fixed when I replaced the error string: -

EditText editText = (EditText) findViewById(R.id.editText2);
-1
source

Source: https://habr.com/ru/post/1650109/


All Articles