unfortunately ___ has stopped , but logcat contains many errors

150 views Asked by At

i have developped an application android that contains two activities , the first activity contains two Edittext and a login button , i have to enter the value 'abcd' as a username and 'EFGH' as a password and validate with the button , the probleme is when i clicked login the application has crashed ,
the error message is : unfortunately ___ is stopped

logcat: errors 11-15 19:22:56.237 783-783/? E/Zygote: accessInfo : 0 11-15 19:24:41.057 783-783/info.adem.tplogin E/AndroidRuntime: FATAL EXCEPTION: main Process: info.adem.tplogin, PID: 783 java.lang.RuntimeException: Unable to start activity ComponentInfo{info.adem.tplogin/info.adem.tplogin.FriendActivity}: 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:3253) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349) at android.app.ActivityThread.access$1100(ActivityThread.java:221) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7224) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 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 info.adem.tplogin.FriendActivity.onCreate(FriendActivity.java:27) at android.app.Activity.performCreate(Activity.java:6876) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)  at android.app.ActivityThread.access$1100(ActivityThread.java:221)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:158)  at android.app.ActivityThread.main(ActivityThread.java:7224)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

1

There are 1 answers

0
Maicon Hellmann On
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object 

Before you use a reference from your button to add an event, you need to retrieve the reference from it.

Like this:

public class YourActivity extends AppCompatActivity{


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

        Button button = (Button)findViewById(R.id.your_button_id)
        //button.setOnClickListener........
    }

}