Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?