user2827799 user2827799 - 6 months ago 28
Android Question

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!

Answer

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.