5

I've been trying to get this to work, even copying and pasting the code exactly as the tutorial says it, but it doesn't seem to work. I know the issue is in MainActivity or DisplayMessageActivity, but I can't see what's wrong. I also have the DisplayMessageActivity in the same folder as MainActivity.

I get the following errors.

DisplayMessageActivity
Gradle: error: cannot find symbol class SuppressLint
Gradle: error: package R does not exist
Gradle: error: cannot find symbol variable NavUtils

MainActivity
Gradle: error: cannot find symbol class DisplayMessageActivity

I have been fiddling with this for awhile, and cannot figure out what I am doing wrong. Any help is much appreciated.

What I have,

AndroidManifest.xml

~snip~
        <activity
        android:name="com.example.myfirstapp.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.myfirstapp.MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />
    </activity>
~snip~

DisplayMessageActivity

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
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);

        // 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.
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

MainActivity

package com.example.myfirstapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }

}

UPDATE

Juned and Peter were correct. The only reason it wasn't working right away was because I had messed something else up. Thanks guys!

user2827799
  • 53
  • 1
  • 1
  • 4

5 Answers5

2

I had the same problem yesterday (: You need to add to your imports in DisplayMessageActivity

import android.annotation.SuppressLint;
import android.support.v4.app.NavUtils;

Also, you need to add to your build.gradle file in dependencies section:

compile 'com.android.support:support-v4:18.0.+'

About Support Libraries you can red here.

Peter Tretyakov
  • 3,380
  • 6
  • 38
  • 54
  • That worked, thank you! Now my only problem is Gradle: error: cannot find symbol class DisplayMessageActivity It's in the right folder, is a class and is spelled correctly. For some reason it isn't being acknowledged. It's acting like it isn't in the manifest, but it is? – user2827799 Sep 29 '13 at 10:06
  • What IDE do you use? I had a problem with Android Studio, which solved by restarting the IDE. Android Studio isn't stable yet. Also, show your full manifest file. – Peter Tretyakov Sep 29 '13 at 11:06
  • I am having the same problem, but i cant seem to find this `build.gradle` file, i am using IntelliJ IDEA 13.1.1 – Shairyar May 04 '14 at 18:55
1

I don't see the imports for SuppressLint in your DisplayMessageActivity class. Add the correct imports.

Also not that SuppressLint annotation was added in API level 16. Make sure that you are using build SDK to 16 or higher.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • I am using build SDK 17+. The import for SupressLint did make that error go away! I still get; package R does not exist, cannot find symbol variable NavUtils and cannot find symbol class DisplayMessageActivity (from MainActivity). I tried Googling this stuff before making this topic but couldn't find anything that worked for me. – user2827799 Sep 29 '13 at 07:02
1

The last part of your issue is that you don't have 'package com.example.firstapp;' at the top of DisplayMessageActivity.java.

bcrochet
  • 11
  • 2
1

As for the Android first app documentiaont they have clearly mentioned as below. The Note near the ***

Build an Intent topic , Step 1


Note: The reference to DisplayMessageActivity will raise an error if you’re using an IDE such as Android Studio because the class doesn’t exist yet. Ignore the error for now; you’ll create the class soon.

Therefore, if you scroll down more in the documentation you can find the topic Create the Second Activity that create the new DisplayMessageActivity.

Android First App Tutorial By Google

Chamath Jeevan
  • 5,072
  • 1
  • 24
  • 27
-1

Add this to your activity_display_message.xml

android:id="@+id/activity_display_message"> 
Laurel
  • 5,965
  • 14
  • 31
  • 57
Naeem
  • 1