I have a Custom Action Bar that should do the following:
- Display the action bar.
- Display the User Name (from the firebase database).
- Display the User profile (from the firebase database).
I am able to get to the username and profile picture in other activities but i am not able to retrieve it on the action bar. The error I receive indicates that possibly the findview.r.id does not match the text view but I have double checked and it does.
The error points to the following line:
*"custom_action_bar_text_view_full_name.setText(userName);"*
When I commented this line out, the app no longer crashed, but the action bar still did not display.
If anyone has any ideas on how I can resolve, please let me know.
Error Message
Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference.
Chat Activity
public class ChatActivity extends AppCompatActivity implements View.OnClickListener {
Window window;
private ImageView image_view_send, image_view_attachment;
private ImageView image_view_profile_picture;
private TextView custom_action_bar_text_view_full_name;
private EditText edit_text_message;
private DatabaseReference mRootRef;
private FirebaseAuth firebaseAuth;
private String currentUserId, chatUserId;
private RecyclerView recyclerViewMessages;
private SwipeRefreshLayout srlMessages;
private MessagesAdapter messagesAdapter;
private List<MessageModel> messagesList;
private LinearLayout linearLayout_progress;
private int currentPage=1;
private static final int RECORD_PER_PAGE = 30; //this will show 30 records per page.
private static final int REQUEST_CODE_PICK_IMAGE=101;
private static final int REQUEST_CODE_CAPTURE_IMAGE=102;
private static final int REQUEST_CODE_PICK_VIDEO=103;
private DatabaseReference databaseReferenceMessages;
private ChildEventListener childEventListener;
private BottomSheetDialog bottomSheetDialog;
private String userName, photoName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
//Declaring our custom action bar.//
ActionBar actionBar = getSupportActionBar();
if(actionBar !=null) {
actionBar.setTitle("");
ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.custom_action_bar_chat, null);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setElevation(0);
actionBar.setCustomView(actionBarLayout);
actionBar.setDisplayOptions(actionBar.getDisplayOptions()|ActionBar.DISPLAY_SHOW_CUSTOM);
//Declaring our custom action bar.//
}
custom_action_bar_text_view_full_name = findViewById(R.id.custom_action_bar_chat_user_full_name); // from the custom action bar.
image_view_profile_picture = findViewById(R.id.custom_action_bar_chat_profile_image); //from thee custom action bar//
image_view_send = findViewById(R.id.image_view_send);
image_view_attachment = findViewById(R.id.imageView_attachment);
edit_text_message = findViewById(R.id.etMessage);
linearLayout_progress = findViewById(R.id.llProgress);
recyclerViewMessages = findViewById(R.id.recycler_view_Messages);
srlMessages = findViewById(R.id.swipe_refresh_layout);
messagesList = new ArrayList<>();
messagesAdapter = new MessagesAdapter(this, messagesList);
recyclerViewMessages.setLayoutManager(new LinearLayoutManager(this));
recyclerViewMessages.setAdapter(messagesAdapter);
image_view_send.setOnClickListener(this); //setting the onclick listner for the icon that lets the user send a message.
image_view_attachment.setOnClickListener(this); //setting the onclick listener for the icon that lets the user attach something to their chat.
firebaseAuth = FirebaseAuth.getInstance();
mRootRef = FirebaseDatabase.getInstance().getReference();
currentUserId = firebaseAuth.getCurrentUser().getUid();
if (Build.VERSION.SDK_INT > 21) {
window = this.getWindow();
window.setStatusBarColor(this.getResources().getColor(R.color.white));
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);// set status text dark
}
//pulling in user information for chat//
if (getIntent().hasExtra(Constants.USER_KEY))
{
chatUserId = getIntent().getStringExtra(Constants.USER_KEY);
photoName = chatUserId + "jpg";
}
//pulling in user information for chat//
if(getIntent().hasExtra(Constants.USER_FULL_NAME))
userName = getIntent().getStringExtra(Constants.USER_FULL_NAME);
if(getIntent().hasExtra(Constants.PROFILE_PHOTO_FILE_NAME))
photoName = getIntent().getStringExtra(Constants.PROFILE_PHOTO_FILE_NAME);
custom_action_bar_text_view_full_name.setText(userName);
if(!TextUtils.isEmpty(photoName)) { // <-- this is just incase someone does not have a profile image added.
StorageReference photoRef = FirebaseStorage.getInstance().getReference().child(Constants.IMAGES_FOLDER).child(photoName);
photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Glide.with(ChatActivity.this)
.load(uri)
.placeholder(R.drawable.small_grey_circle)
.error(R.drawable.small_grey_circle)
.into(image_view_profile_picture);
}
});
}
Chat Adapter
holder.linearLayout_chat_list.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Intent intent = new Intent(context, ChatActivity.class);
intent.putExtra(Constants.USER_KEY, chatListModel.getUserId());
intent.putExtra(Constants.USER_FULL_NAME, chatListModel.getUserName());
intent.putExtra(Constants.PROFILE_PHOTO_FILE_NAME, chatListModel.getPhotoName());
context.startActivity(intent);
}
});
Custom Action Bar xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_layout_custom_action_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="#EFEFEF"
android:gravity="center_vertical"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/custom_action_bar_chat_profile_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/small_grey_circle">
</de.hdodenhof.circleimageview.CircleImageView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp">
<TextView
android:id="@+id/custom_action_bar_chat_user_full_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:singleLine="true"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:textColor="#4B16E7"
android:textStyle="bold"
android:textSize="20dp"
android:fontFamily="@font/roboto_medium"
tools:text="User Name">
</TextView>
</LinearLayout>
</LinearLayout>