无法在Android Studio中解析符号fab

问题描述 投票:0回答:2

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

代码如下:

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

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

java android-studio
2个回答
6
投票

您可能删除了activity_main中的fab:

如果是这种情况,并且你真的不需要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);
    }
});

0
投票

添加支持:在build.gradle:module app中设计依赖项,这将解决它

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

将如下所示:

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'

}

© www.soinside.com 2019 - 2024. All rights reserved.