Java 找不到符号类意图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26246839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:04:51  来源:igfitidea点击:

Cannot find symbol class Intent

javaandroidandroid-intent

提问by stanley santoso

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

我是 android 开发和编码 java 和 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

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"

然后我在使用 Intent 时遇到了这个错误。switch 下的“Intent”字样变红,出现“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

这是我在 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);
}

采纳答案by MarsAtomic

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

再次查看您的 AlarmListActivity 并检查顶部的导入语句并确保它包含以下行:

import android.content.Intent;

If you intend to use any pre-existing classes that aren't part of the java.langpackage, 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.

如果您打算使用任何不属于java.lang包的预先存在的类,您通常必须导入这些类。Intent例如,Android是由 Android 开发团队编写的预构建类,它允许通知其他应用程序/活动。如果要使用Intent,则必须导入包含 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.langpackage, it needs to know where to look for a blueprint to build that object. The import statement is the location of that blueprint.

当您编写 时new Intent(),编译器会看到您正在请求构建一个新对象,但由于在java.lang包中找不到该对象,因此它需要知道到哪里寻找构建该对象的蓝图。导入语句是该蓝图的位置。

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.

我查看了教程,以有经验的程序员的方式,作者似乎掩盖了一些基本但仍然重要的事情,例如使他的示例代码工作的导入语句。

回答by aravind.udayashankara

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.

通过了解 Android Studio 如何索引文件,我刚刚解决了同样的问题,如您所知,构建 Android 应用程序是一个非常复杂的过程。因此,Android Studio 有一些内部引用,它会随着您创建的每个文件的更改而不断更新。

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

我通常不会在主项目包下创建活动,我会根据我使用的设计模式创建子包来组织文件,例如如果我的 APP 名称是 com.example.testingaravind 那么我通常会在其中创建诸如活动之类的包,服务,模型,管理器等......所以今天我首先创建了一个活动,然后通过Android Studio将该活动移动到活动包中,我开始面临您所描述的相同问题,下面是我的源代码

    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

在 startServiceOnClickHandler 方法中,它显示一个错误,说“无法解析构造函数意图”我在谷歌中搜索了很多,发现

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,

正因为如此,Android Studio 不知道活动文件夹中存在一个名为 BootstrapActivity 的类,

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.

这似乎是 android studio 工作方式的一个错误。当我将活动类文件从一个包移动到另一个包时,Android Studio 必须更新清单文件。

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

我发布此信息是为了帮助可能使用类似用例到达此帖子的其他人。