Click on the FAB but do not go to the new Intent

1

I'm getting the click of a list and moving on to another Intent. But when I click on add it gives error.

Code;

 Button cadastrar = (Button) findViewById(R.id.fab);

    cadastrar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this, Cadastro.class);
            startActivity(intent);
        }
    });

Error:

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{NOMEDOPROJETO.MainActivity}: java.lang.ClassCastException: android.support.design.widget.FloatingActionButton cannot be cast to android.widget.Button
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
                                                                           at android.app.ActivityThread.access$600(ActivityThread.java:141)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                           at android.os.Looper.loop(Looper.java:137)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5041)
                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:511)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                                           at dalvik.system.NativeStart.main(Native Method)
                                                                        Caused by: java.lang.ClassCastException: android.support.design.widget.FloatingActionButton cannot be cast to android.widget.Button
                                                                           at br.com.aula.primeirobanco.MainActivity.onCreate(MainActivity.java:39)

By the error that appears I'm trying to pass a Button and it can not cast the FAB. Does anyone know how I can change?

    
asked by anonymous 28.10.2016 / 17:08

1 answer

1

Are you declaring in XML as FloatingActionButton?

Your Java should look like this:

FloatingActionButton cadastrar = (FloatingActionButton) findViewById(R.id.fab);

    cadastrar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this, Cadastro.class);
            startActivity(intent);
        }
    });
    
28.10.2016 / 17:10