Reputation: 27
My app is crashing while switching from the after login activity to the in app´s profile activity. Build is complete without errors.
Any ideas why? (Thankful for every Help)
////////////////////////////////////////////////////////////////////////// ProfileActivity ---->
package com.smarthelp.smarthelp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import com.google.firebase.auth.FirebaseAuth;
public class ProfileActivity extends AppCompatActivity {
private FirebaseAuth firebaseAuth;
private Button logout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
firebaseAuth= FirebaseAuth.getInstance();
logout = (Button)findViewById(R.id.btnLogout);
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Logout();
}
});
configureibtnHelp();
}
private void Logout(){
firebaseAuth.signOut();
finish();
startActivity(new Intent(ProfileActivity.this, LoginActivity.class));
}
private void configureibtnHelp() {
ImageButton ibtnHelp = (ImageButton) findViewById(R.id.ibtnHelp);
ibtnHelp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.profile_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.logoutMenu:{
Logout();
}
}
return super.onOptionsItemSelected(item);
}
}
/////////////////////////////////////////////////////////////////////// HelpActivity ---->
package com.smarthelp.smarthelp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import com.google.firebase.auth.FirebaseAuth;
public class HelpActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help);
configureibtnProfile();
}
private void configureibtnProfile(){
ImageButton ibtnProfile = (ImageButton) findViewById(R.id.ibtnProfile);
ibtnProfile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(HelpActivity.this, ProfileActivity.class));
}
});
}
}
////////////////////////////////////////////////////////////////////////////// activity_help.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HelpActivity">
<ImageButton
android:id="@+id/ibtnProfile"
android:layout_width="51dp"
android:layout_height="52dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="ProfilButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.025"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.983"
app:srcCompat="@drawable/profil" />
<ImageButton
android:id="@+id/ibtnOrder"
android:layout_width="52dp"
android:layout_height="52dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="OrderButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.983"
app:srcCompat="@drawable/order" />
<ImageButton
android:id="@+id/ibtnHelp"
android:layout_width="51dp"
android:layout_height="53dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="236dp"
android:layout_marginRight="8dp"
android:layout_marginStart="236dp"
android:layout_marginTop="8dp"
android:contentDescription="HelpButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.875"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.983"
app:srcCompat="@drawable/help" />
</android.support.constraint.ConstraintLayout>
/////////////////////////////////////////////////////////////////////////////// ------->Logcat
08-13 19:13:15.273 21068-21068/com.smarthelp.smarthelp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.smarthelp.smarthelp, PID: 21068
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smarthelp.smarthelp/com.smarthelp.smarthelp.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.smarthelp.smarthelp.ProfileActivity.configureibtnHelp(ProfileActivity.java:48)
at com.smarthelp.smarthelp.ProfileActivity.onCreate(ProfileActivity.java:35)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
Upvotes: 0
Views: 70
Reputation: 128
From the crash log it seems
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
there is an NPE on the button for which you are trying to set onclick listener. In your ProfileActivity set the button to one of the ImageButtons of XML Assuming you are trying to do for first of your image buttons
just add the following line before setting onClickListener
logout=(Button) findViewById(R.id.ibtnProfile);
Upvotes: 2
Reputation: 2917
It's pretty much said in the stacktrace: trying to call addOnClickListener
on a null object at ProfileActivity
on line 27
You haven't initialized logout
button with any value. Probably forgot to findViewById
it.
Upvotes: 0
Reputation: 445
Change
firebaseAuth.getInstance();
to
FirebaseAuth.getInstance();
at line 14 in ProfileActivity.java
Upvotes: 0