user2827799
user2827799

Reputation: 53

Android First App Tutorial Issue

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!

Upvotes: 5

Views: 11386

Answers (5)

Naeem
Naeem

Reputation: 1

Add this to your activity_display_message.xml

android:id="@+id/activity_display_message"> 

Upvotes: -1

Chamath Jeevan
Chamath Jeevan

Reputation: 5172

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

Upvotes: 1

bcrochet
bcrochet

Reputation: 11

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

Upvotes: 1

Peter Tretiakov
Peter Tretiakov

Reputation: 3410

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.

Upvotes: 2

Juned Ahsan
Juned Ahsan

Reputation: 68715

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.

Upvotes: 1

Related Questions