Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

'Mass' "Cannot resolve symbol/method" - Android Studio.

In Android Studio, throughout both activities, I'm getting an error all over, saying "Cannot resolve symbol" or "Cannot resolve method". I've tried cleaning and rebuilding, I've tried invalidating caches and restarting. Nothing seems to work.

For example: public class MainActivity extends Activity {

    private EditText mNameField;
    private Button mStartButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interactive_story);

Half of this, such as "MainActivity", "EditText", "mNameField" and others are highlighted in red, with the error.

Does anyone know a fix for this? Thanks.

Can you include your import statements?

1 Answer

Hi Ernest, I'm doing the Interactive Story tutorial, this is the complete code I have.

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class MainActivity extends Activity {

    private EditText mNameField;
    private Button mStartButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interactive_story);

        mNameField = (EditText)findViewById(R.id.nameEditText);
        mStartButton = (Button)findViewById(R.id.startButton);

        mStartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = mNameField.getText().toString();
                Toast.makeText(MainActivity.this, name, Toast.LENGTH_LONG).show();
                startStory(name);
            }
        });
    }
    private void startStory(String name) {
        Intent intent = new Intent(this, StoryActivity.class);
        intent.putExtra("name", name);
        startActivity(intent);
    }

}

That's for "MainActivity.java". This is "StoryActivity.java"

import android.app.Activity;

import android.content.Intent;

import android.nfc.Tag;

import android.os.Bundle;

import android.util.Log;

import android.view.Menu;

import android.view.MenuItem;

import com.alex.interactivestory.R;
public class StoryActivity extends Activity {

    public static final String TAG = StoryActivity.class.getSimpleName();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_story);

        Intent intent = getIntent();
        intent.getStringExtra("name");

        if (name == null) {
            name = "Friend";
        }
        Log.d(TAG, name);
    }
}

In your question you mentioned MainActivity is "highlighted in red". Does your java file have a package name declared at the top? To make sure nothing else is causing errors, can you comment out all of your methods in your MainActivity?

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.myapplication.R;

public class MainActivity extends Activity {

    private EditText mNameField;
    private Button mStartButton;

//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_interactive_story);
//
//        mNameField = (EditText)findViewById(R.id.nameEditText);
//        mStartButton = (Button)findViewById(R.id.startButton);
//
//        mStartButton.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                String name = mNameField.getText().toString();
//                Toast.makeText(MainActivity.this, name, Toast.LENGTH_LONG).show();
//                startStory(name);
//            }
//        });
//    }
//    private void startStory(String name) {
//        Intent intent = new Intent(this, StoryActivity.class);
//        intent.putExtra("name", name);
//        startActivity(intent);
//    }

}

Then add the proper package name at the top of the file in this format: package com.example.myapplication;

At this point you should have no errors (red lines). Can you try to reproduce my steps?