0

I've been working through an Android Application tutorial on the developer android site for quite a while, but I can't get it to work perfectly for the life of me. My DisplayMessageActivity.java is filled with errors (i.e. getting undefined methods and unresolved types) Here are my files...

MainActivity.java

package com.miller.lab3;

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

public class MainActivity extends Activity {

    public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    /** Called when the user clicks the Send button */
    public void sendMessage(View view) {
        // Do something in response to button
        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);
    }
}

DisplayMessageActivity.java

package com.miller.lab3;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class DisplayMessageActivity extends ActionBarActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        // Set the text view as the activity layout
        setContentView(textView);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() { }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState) {
              View rootView = inflater.inflate(R.layout.fragment_display_message,
                      container, false);
              return rootView;
        }
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="sendMessage" />

    <EditText
        android:id="@+id/edit_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />


</RelativeLayout>

strings.xml

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

    <string name="app_name">Lab3</string>
    <string name="hello_world">Hello world!</string>
    <string name="title_activity_display_message">My Message</string>
    <string name="edit_message">Enter a message</string>
    <string name="button_send">Send</string>


</resources>

I'm wondering if anyone sees anything out of the ordinary. It would be greatly appreciated if you could point it out. I heard that this may or may not be an outdated version of the tutorial and the fact that I must use eclipse to do it doesn't exactly help. Thanks in advance.

If anyone doesn't know what I'm talking about here is the tutorial link http://developer.android.com/training/basics/firstapp/starting-activity.html

YoungMogul
  • 101
  • 6
  • 15
  • could you post a screenshot of your IDE or provide some of the errors – Yash Sampat Feb 18 '15 at 18:11
  • `getting undefined methods and unresolved types` which method not resolved? – ρяσѕρєя K Feb 18 '15 at 18:12
  • ActionBarActivity cannot be resolved to a type, The method getIntent() is undefined for the type DisplayMessageActivity, The constructor TextView(DisplayMessageActivity) is undefined, The method setContentView(TextView) is undefined for the type DisplayMessageActivity, The method getSupportFragmentManager() is undefined for the type DisplayMessageActivity, The method onOptionsItemSelected(MenuItem) of type DisplayMessageActivity must override or implement a supertype method – YoungMogul Feb 18 '15 at 18:18
  • Try adding `import com.miller.lab3.R` as an import – Marcus Feb 18 '15 at 20:05
  • I only see MainActivity twice, can you post your DisplayMessageActivity? – Manuel Ramírez Feb 18 '15 at 20:05
  • Fixed. Looks like I rushed this post a bit. – YoungMogul Feb 19 '15 at 01:11

2 Answers2

0

You code seems correct. Try cleaning the project. Go to Project>Clean>Clean selected project.If this doesn't work as happens in some cases, try creating a new project. However, if this is a big project, this method is useless. In such cases, there is a much better solution to this. Make use of git which is a version control system that keeps your project super safe and clean.

Nagabhushan Baddi
  • 1,164
  • 10
  • 18
0

You are not importing ActionBarActivity

Add to your imports in DisplayMessageActivity.java:

import android.support.v7.app.ActionBarActivity;

or click on ActionBarActivity and press ctrl+1, and click Import 'ActionBarActivity'

SilverCorvus
  • 2,956
  • 1
  • 15
  • 26
  • All that says is: The import android.support.v7 cannot be resolved. Shouldn't CTRL + SHIFT + O add my missing imports? Because I always make sure to do that first. – YoungMogul Feb 19 '15 at 16:40
  • @YoungMogul Not if the support library is not added to eclipse correctly. Please add it (or re-add it) by using this guide: https://developer.android.com/tools/support-library/setup.html . make sure to add with resources. – SilverCorvus Feb 19 '15 at 17:15
  • I have three errors still remaining though. The container, action_settings and fragment_display_message portion of the code cannot be resolved. – YoungMogul Feb 23 '15 at 20:47