Mark Campo
Mark Campo

Reputation: 23

activity_display_message cannot be resolved or is not a field

I'm following the android basic tutorial and have been stuck on this one most of the day. I have highlighted the error line with '//'. I've tried all sorts of import lines .. previous searches seem to say it's an import defining type error.

package com.example.myapp3;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;

public class DisplayMessageActivity extends Activity {

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

    // Make sure we're running on Honeycomb or higher to use ActionBar APIs
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // Show the Up button in the action bar.`enter code here`
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

Upvotes: 2

Views: 5765

Answers (9)

Akatsuki Sky
Akatsuki Sky

Reputation: 1

i turn off my Emulator and restart the idea,it work

Upvotes: -1

Tara Hanratty
Tara Hanratty

Reputation: 121

I had this exact problem and I couldn't find the answer anywhere on stackoverflow. I tried cleaning, rebuilding etc. The help tip insisted that I should create the file but when I accepted the suggestion it said the file already existed. What ultimately fixed it for me was just restarting the IDE.

Upvotes: 2

Kingston
Kingston

Reputation: 41

Check your Manifest.xml. Make sure that the activity is referenced there along with the package name where it is been defined.

Upvotes: 0

Robby
Robby

Reputation: 1

My problem was: after creating the activity_display_message in my xml file, I didn't save the changes. After hours of research, I saved the file and my problem is gone! Hope this helps!

Upvotes: -1

Amit
Amit

Reputation: 1672

If you see import android.R; in the imports , remove that as well. it resolved my compilation error.

Upvotes: 0

Art
Art

Reputation: 1619

Even though this line: setContentView(R.layout.activity_display_message); Had and error message underlining the text "activity_display_message", it still ran for me.

But I hated seeing the underline telling me there was something wrong.

After a bit of searching and trial and error I realized that the problem was that the auto generated file "activity_display_message.xml" was missing the proper xml first line.

<?xml version="1.0" encoding="utf-8"?>

Once I added that as the first line in the "activity_display_message.xml" file the error underline went away.

Upvotes: 0

Bette Devine
Bette Devine

Reputation: 1194

With which IDE are you working?

For Eclipse, you need to import R file generated for the project automatically if your project does not contain any error. The R file generated contains reference to your resources that you will be creating.

So make sure

  1. You have a layout resource named activity_display_message in your layout folder which should have no errors.
  2. Import the R file of the project in class so that the resources of projects will be available for you to use in the class.

Upvotes: 0

Shubhank
Shubhank

Reputation: 21805

According to the guide you are following..it states

Note: If you are using an IDE other than Eclipse, your project does not contain the activity_display_message layout that's requested by setContentView(). That's OK because you will update this method later and won't be using that layout.

So just follow along.. your code will be updated.

On other note

setContentView(R.layout.activity_display_message);

here you are trying to set the view of your activity... and referencing the layout which does not exist.. if you will create a xml named activity_display_message.xml inside res->layout folder of your project.. error will resolve.

Upvotes: 4

Jim Rhodes
Jim Rhodes

Reputation: 5085

When you build an Android application, a file named R.java is created that contains identifiers for all of your resources. If the file R.java does not exist or does not contain an identifier for your layout, I can think of at least 4 possible reasons.

  1. You do not have a file named activity_display_message.xml in your layout folder
  2. You have a file with that name but it is not all lower case
  3. You have an error in one or more of your resource files and R.java is not being created
  4. Your development environment is not re-building R.java for some reason

In the case of number 4, try doing a "Clean"

Upvotes: 0

Related Questions