'app keeps crashing when accessing profile tab in my app
so my app keeps crashing, im developing a chat app and when i run the app and try to access the profile tab it crashes the logcat says this.
when it runs the app its fine but as soon as i click profile tab it crashes it was fine at first, but once i added more code to the profile activity it kept crashing however i need that code or a varient of it
im fairly new to android studio all help is welcome.
Thank you
this is the logcat error:
2022-01-25 17:49:08.570 2928-2928/com.example.ttt E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ttt, PID: 2928
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ttt/com.example.ttt.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:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
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.example.ttt.ProfileActivity.onCreate(ProfileActivity.java:71)
at android.app.Activity.performCreate(Activity.java:7994)
at android.app.Activity.performCreate(Activity.java:7978)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
HERE IS THE ProfileActivity:
package com.example.ttt;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.squareup.picasso.Picasso;
public class ProfileActivity extends AppCompatActivity {
EditText mviewusername;
FirebaseAuth firebaseAuth;
FirebaseDatabase firebaseDatabase;
TextView mmovetoupdateprofile;
FirebaseFirestore firebaseFirestore;
ImageView mviewuserimageinimageview;
StorageReference storageReference;
private String ImageURIacessToken;
androidx.appcompat.widget.Toolbar mtoolbarofviewprofile;
ImageButton mbackbuttonofviewprofile;
FirebaseStorage firebaseStorage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
mviewuserimageinimageview=findViewById(R.id.viewuserimageinview);
mviewusername=findViewById(R.id.viewusername);
mmovetoupdateprofile=findViewById(R.id.movetoupdateprofile);
firebaseFirestore=FirebaseFirestore.getInstance();
mtoolbarofviewprofile=findViewById(R.id.toolbarofviewprofile);
mbackbuttonofviewprofile=findViewById(R.id.backbuttonofviewprofile);
firebaseDatabase=FirebaseDatabase.getInstance();
firebaseAuth=FirebaseAuth.getInstance();
firebaseStorage=FirebaseStorage.getInstance();
setSupportActionBar(mtoolbarofviewprofile);
mbackbuttonofviewprofile.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
finish();
}
});
storageReference=firebaseStorage.getReference();
storageReference.child("Images").child(firebaseAuth.getUid()).child("Profile Pic").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
ImageURIacessToken=uri.toString();
Picasso.get().load(uri).into(mviewuserimageinimageview);
}
});
DatabaseReference databaseReference=firebaseDatabase.getReference(firebaseAuth.getUid());
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
UserProfile muserprofile=snapshot.getValue(UserProfile.class);
mviewusername.setText(muserprofile.getUsername());
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(getApplicationContext(),"Failed To Fetch",Toast.LENGTH_SHORT).show();
}
});
mmovetoupdateprofile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(ProfileActivity.this,UpdateProfile.class);
intent.putExtra("nameofuser",mviewusername.getText().toString());
startActivity(intent);
}
});
}
@Override
protected void onStop() {
super.onStop();
DocumentReference documentReference=firebaseFirestore.collection("Users").document(firebaseAuth.getUid());
documentReference.update("status","Offline").addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(),"Now User is Offline",Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onStart() {
super.onStart();
DocumentReference documentReference=firebaseFirestore.collection("Users").document(firebaseAuth.getUid());
documentReference.update("status","Online").addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(getApplicationContext(),"Now User is Online",Toast.LENGTH_SHORT).show();
}
});
}
}
Solution 1:[1]
Here is your problem
TextView mmovetoupdateprofile;
your mmovetoupdateprofile is an ImageButton in your xml and not a Textview
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Exron |
