Issue
I am new in android development and coding java and xml.
but I was following this tutorial:
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);
}
Solution
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.
Answered By - MarsAtomic
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.