New Activity from FloatingActionButton

i would launch an activity with a click in a floating action button, but when i click this, I get "Unfortunately, ... has stopped.". please help me!

questo è il codice del bottone di default, and this works

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

When change the code of a snackbar with the code of a new activity, this does not work

fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent launchactivity=new Intent (getApplicationContext(),add.class);
            startActivity(launchactivity);

        }
    });

This is my logcat

09-24 21:22:29.009 6353-6353/com.example.fra31.tradebooks E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.fra31.tradebooks, PID: 6353 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fra31.tradebooks/com.example.fra31.tradebooks.add}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2356) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418) at android.app.ActivityThread.access$900(ActivityThread.java:154) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5289) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.fra31.tradebooks.add.onCreate(add.java:47) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2309) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2418)  at android.app.ActivityThread.access$900(ActivityThread.java:154)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5289)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)  at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115) 

Upvotes: 0

Views: 2514

Answers (2)

Omar Mardini
Omar Mardini

Reputation: 41

Intent launchactivity=new Intent (MainActivity.this,add.class);
            startActivity(launchactivity);

and you should go to androidmanifest.xml and define your activity as this :

<activity android:name=".MainActivity">
       </activity>

Upvotes: 0

MD Ashikur Rahman
MD Ashikur Rahman

Reputation: 11

you should try this.

Intent launchactivity = new  Intent(MainActivity.this, add.class);
                    startActivity(launchactivity);

Upvotes: 1

Related Questions