android error on tutorial cannot find symbol varia

2019-06-22 06:49发布

问题:

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'
}

回答1:

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



回答2:

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 :)



回答3:

I think:

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

Should be:

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


回答4:

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);


回答5:

I have solved the same issue by clean project and then Build the gradle, please try once.



回答6:

"@layout/activity_display_message"

It is a layout. Not an ID.

You correctly used it here as R.layout

setContentView(R.layout.activity_display_message);

But this is wrong as R.id

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

The corrected method should ideally look like this, but note that you might want some LayoutParams on the TextView, otherwise it might not display on the screen.

@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);
}


回答7:

The error comes from the MainActivity.java file.

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

Where I have (R.id.editText);.....the system put (R.id.edit_message);

edit_message is the function added to the text box but it is under the (EditText) "block" of code. New to Java, not sure what the block of code is called and not sure why the program put that in the public class. Hope this helps.

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


回答8:

I had the same error message: cannot find symbol variable activity_display_message

I solved it by adding android:id in 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>


回答9:

Check if you have code like this in MainActivity.java:

public class MainActivity extends AppCompatActivity {

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

"com.example.MyApplicationstart.MESSAGE", where "MyApplicationstart" is a name of your project.



回答10:

I've had this problem so many times trying to do this tutorial, and to add to the only answer here that helped me, which was deleting the MainActivity. from the MainActivity.EXTRA_MESSAGE portion...

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

import static android.provider.AlarmClock.EXTRA_MESSAGE;

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



回答11:

I had also the same problem. The error got rectified when I replaced the error line with:--

EditText editText = (EditText) findViewById(R.id.editText2);