Click on FAB but do not switch to new Intent

Asked

Viewed 36 times

1

I am taking the click of a list and passing to another Intent. However when I click on the 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 he can not do the cast for the FAB. Does anyone know how I can change ?

1 answer

1

You are 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);
        }
    });
  • Yes, bro. For the love of it. It was just having read a little. Of course a FAB is not equal to a Button. After I asked the question then I touched. It was bad. Thanks.

  • Sometimes ends up confusing even, after all the function of the two is practically the same...kkkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.