Java Android Studio 错误:无法解析符号“查看”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27779724/
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 05:01:02  来源:igfitidea点击:

Android Studio ERROR: Cannot resolve symbol 'View'

javaandroidandroid-studio

提问by Stefan

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.htmlAndroid Studio ends up with this error:

我正在尝试按照 google 的本教程使用 Android Studio 创建您自己的 android 应用程序。但是,当我按照此页面上的第 4 步进行操作时:http: //developer.android.com/training/basics/firstapp/starting-activity.htmlAndroid Studio 最终出现此错误:

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);
    }
}

Whats wrong in this code? I'm not experienced in java and after looking at other questions I still couldnt find the right solution.

这段代码有什么问题?我在 Java 方面没有经验,在查看了其他问题后,我仍然找不到正确的解决方案。

Thanks for helping!

感谢您的帮助!

采纳答案by BeingMIAkashs

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

我认为您忘记包含 View 的导入语句。在您的代码中添加以下导入

import android.view.View;

回答by Vincent

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;

Android Studio 将显示无法解析符号错误,因为此代码引用了未导入的类。您可以通过按 Alt + Enter(或 Mac 上的 Option + Return)使用 Android Studio 的“导入类”功能解决其中的一些问题。您的导入最终应如下所示:

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

*https://developer.android.com/training/basics/firstapp/starting-activity.html#BuildIntent

* https://developer.android.com/training/basics/firstapp/starting-activity.html#BuildIntent