Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android First App Tutorial Issue

Tags:

java

android

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!

like image 637
user2827799 Avatar asked Oct 02 '22 19:10

user2827799


2 Answers

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.

like image 94
Peter Tretyakov Avatar answered Oct 05 '22 09:10

Peter Tretyakov


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.

like image 35
Juned Ahsan Avatar answered Oct 05 '22 11:10

Juned Ahsan