24

I'm trying to follow this tutorial from Google to create your own android app with Android Studio. But when I follow the 4th step on this page: http://developer.android.com/training/basics/firstapp/starting-activity.html Android Studio ends up with this error:

Cannot resolve symbol 'View'

This is what my code looks like at the moment:

public class MainActivity extends ActionBarActivity {
    /** Called when the user clicks the Send button */
    public void sendMessage(View view) { <--- (This line ends up with the error)
        // Do something in response to button
    }

    @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.menu_main, menu);
        return true;
    }

    @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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

What's wrong with this code? I'm not experienced in java and after looking at other questions I still couldn't find the right solution.

Thanks for helping!

Federico Navarrete
  • 3,069
  • 5
  • 41
  • 76
Stefan
  • 1,905
  • 7
  • 24
  • 38
  • Please show your imports – 2Dee Jan 05 '15 at 12:41
  • Clean your project and see if it works. – Gaurav Dave Jan 05 '15 at 12:42
  • @2dee How do I show my imports? (New to this program/java) – Stefan Jan 05 '15 at 12:43
  • imports are what you have before the class declaration in a java file. For example import android.view.View; – 2Dee Jan 05 '15 at 12:45
  • @2Dee thanks that solved my problem But now I end up with 7 new errors like this: Error:(6, 35) error: cannot find symbol class ActionBarActivity – Stefan Jan 05 '15 at 12:56
  • Alt+Enter should help you sort out the imports. Maybe you could also read about the basics of Java, to avoid getting lost between the intricacies of Java and those of Android. – 2Dee Jan 05 '15 at 13:01

3 Answers3

58

I think you forget to include the import statement for View. Add the following import in your code

import android.view.View;
BeingMIAkashs
  • 1,375
  • 11
  • 18
  • Thanks the error message is gone but when I try to run it now I end up with 7 new errors in my console: Error:(6, 35) error: cannot find symbol class ActionBarActivity Error:(13, 29) error: cannot find symbol class Bundle Error:(20, 40) error: cannot find symbol class Menu Error:(27, 42) error: cannot find symbol class MenuItem Error:(14, 9) error: cannot find symbol variable super Error:(15, 9) error: cannot find symbol method setContentView(int) Error:(12, 5) error: method does not override or implement a method from a supertype – Stefan Jan 05 '15 at 12:48
  • just import the necessary Classes. For android studio press Ctrl + Alt + O to import class. – BeingMIAkashs Jan 05 '15 at 12:51
  • Ctrl + Alt + O gives me a popup with 2 file directorys to select with a button "run" It doesnt look like its doing something when I press the run button? – Stefan Jan 05 '15 at 12:55
  • see this http://stackoverflow.com/questions/22272524/how-to-auto-import-the-necessary-classes-in-android-studio-with-shortcut – BeingMIAkashs Jan 05 '15 at 12:57
  • Btw, I think this is a mistake in the tutorial, they should have mentioned that you have to add this import. – Konstantin Schubert Feb 22 '15 at 15:39
  • I've added import but the code is still red. before everything was fine until suddenly my java class files were corrupted one by one, the code was lost in half, the code became random and could not be saved anymore. Since this some imports seem unreadable but can still be run. Is there any solution? – Sasmita Feb 05 '22 at 06:28
3

I am doing the same tutorial and ran into the same problem (that's why I found this question).

I see they explain this issue in the next paragraph named "Build an Intent":

Android Studio will display Cannot resolve symbol errors because this code references classes that are not imported. You can solve some of these with Android Studio's "import class" functionality by pressing Alt + Enter (or Option + Return on Mac). Your imports should end up as the following:

import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText;

Federico Navarrete
  • 3,069
  • 5
  • 41
  • 76
Vincent
  • 31
  • 2
1

This problem can be resolved easily either by pressing alt + enter on the error to import android.view.View or by cross-checking that your method is outside protected void onCreate(Bundle savedInstanceState) and in the class parenthesis.

Federico Navarrete
  • 3,069
  • 5
  • 41
  • 76
Ssehgal
  • 11
  • 1