Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find symbol class Intent

I am new in android development and coding java and xml.

but I was following this tutorial:

http://www.steventrigg.com/activities-and-the-action-bar-create-an-alarm-clock-in-android-tutorial-part-1/#comment-296

then I had this error when using Intent. The word "Intent" under switch became red and there is an error "cannot find symbol class Intent"

Can someone explain to me what is going on and how to solve this?

This is the last part of my code under AlarmListActivity.java

@Override

public boolean onOptionsItemSelected(MenuItem item) {

   int id = item.getItemId();
   if (id == R.id.action_settings) {
       return true;
   }

   switch (item.getItemId()) {
       case R.id.action_add_new_alarm: {
           Intent intent = new Intent(this,
                   AlarmDetailsActivity.class);

            startActivity(intent);
            break;
        }
    }
    return super.onOptionsItemSelected(item);
}
like image 426
stanley santoso Avatar asked Oct 07 '14 23:10

stanley santoso


2 Answers

Look at your AlarmListActivity again and check the import statements at the top and make sure it includes the line:

import android.content.Intent;

If you intend to use any pre-existing classes that aren't part of the java.lang package, you generally have to import those classes. An Android Intent, for example, is a pre-built class, written by the Android development team, that allows for notification of other apps/activities. If you want to use Intent, you'd then have to import the package containing Intent.

When you write new Intent(), the compiler sees that you're requesting the construction of a new object, but because that object is not found in the java.lang package, it needs to know where to look for a blueprint to build that object. The import statement is the location of that blueprint.

I took a look at the tutorial and in the manner of experienced programmers, the author seems to have glossed over a few basic, but nonetheless, important things, such as the import statements that make his sample code work.

like image 79
MarsAtomic Avatar answered Nov 14 '22 06:11

MarsAtomic


I had a same problem which I just resolved, by understanding how Android Studio indexes files, As you know Building an Android App is quite complicated process. So Android studio has some internal references which it keeps getting updated on change of every file that you have created.

I arrived at this post while searching for the solution,

This is how I got this problem

I usually wont create an activity under the main project package, I create sub packages to organize files as per the design pattern that I use, for eg If my APP name is com.example.testingaravind then inside that I usually create packages such as activites, services, models, managers etc ... So today I just created an activity first and then moved that activity into activites package via Android Studio, I started facing the same issue what you have described, Below was my source code

    public class BootstrapActivity extends ActionBarActivity {

        private static final String TAG = "BootstrapActivity";

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

        public void startServiceOnClickHandler(View view) {

            Intent intent = new Intent(BootstrapActivity.this , AnalyzerService.class);
            startService(intent);
        }
}

In the method startServiceOnClickHandler it was showing an error saying, "Cannot resolve constructor Intent" I searched a lot in google and found that

When I move a file from one package to other package, my manifest file wont get updated, in the manifest we mention the activity name and its package path in my case it should be

   android:name=".activities.BootstrapActivity"

But it was

   android:name=".BootstrapActivity"

Because of this, Android studio was unaware that a class called BootstrapActivity exists inside the activities folder,

This seems to be a bug in the way how android studio works. Android Studio has to update manifestfile when I move the activity class file from one package to another package.

I am posting this to help others who might arrive at this post with the similar usecase.

like image 3
Aravind.HU Avatar answered Nov 14 '22 07:11

Aravind.HU