java 无法在 Android Studio 中解析符号 fab

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

Cannot resolve symbol fab in Android Studio

javaandroid-studio

提问by Ryan Scollard

I am developing an app but I am having an issue with a piece of code. The same piece of code is giving me the same error for a few different pages.

我正在开发一个应用程序,但我遇到了一段代码的问题。同一段代码在几个不同的页面上给了我同样的错误。

The code is as follows:

代码如下:

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

    buttonSignIn = (Button) findViewById(R.id.buttonSignIN);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show();

            buttonSignIn.setOnClickListener(this);
        }
    });
}}

It gives me an error on the line below saying it cannot resolve symbol fab FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

它在下面的行中给我一个错误,说它无法解析符号 fab FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

回答by Ravers

You probably deleted the fab in your activity_main:

你可能在你的activity_main中删除了fab:

This

这

If that's the case, and you really don't need the fab, you probably just need to delete this piece of code:

如果是这种情况,并且您真的不需要 fab,您可能只需要删除这段代码:

buttonSignIn = (Button) findViewById(R.id.buttonSignIN);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show();

        buttonSignIn.setOnClickListener(this);
    }
});

回答by Ridha Rezzag

add the support:design in build.gradle: module app to dependencies this will fix it

将 build.gradle:module app 中的 support:design 添加到依赖项中,这将修复它

compile 'com.android.support:design:26.+'

will look like this :

看起来像这样:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:26.+'
testCompile 'junit:junit:4.12'

}

}