Situation: I am making a chat app with Skype like UI. The contacts recycler view is on the left side. I have the custom ActionBar based theme.
I need to set the title in the ActionBar onClick.
So, basically, the onClick method is in the Adapter. OnClick of the contacts, the method is passed to the Activity with ActionBar and the name of the contact should come in the title.
The getActionBar() runs perfectly and the Title is set in onCreate method. But, app crashes when I do the same in method outside onCreate. I referred links here and here but I couldn't solve my issue.
Please guide me regarding the same.
Example:
ChatActivity extends Activity {
//..onCreate here
if(getActionBar() != null) {
String title = " Chat: ";
if(userName != null) {
title = title + userName;
}
getActionBar().setTitle(title);
}
// onCreate finishes
// onContactChange
public void onContactChange(int position, ContactsVO addContact) {
userName = addContact.getName().toString();
String url = addContact.getDP();
if(getActionBar() != null) { //App crashes here
String title =" Chat: ";
if(userName != null)
title = title + userTo;
getActionBar().setTitle(title);
}
}
}
Async Task is called, webservice returns the data which is set in the Adapter.
Now, in Adapter,
ChatActivity c1 = new ChatActivity();
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
ContactsVO contactsvo = data.get(position);
holder.tv.setText(contactsvo.getName());
String url = contactsvo.getDP();
Glide.with(getContext())
.load(url)
.crossFade()
.into(holder.img);
holder.row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
c1.onContactChange(position,contactsvo); //method called here.
}
});
}
Stack Trace
E/ACRA: ACRA caught a NullPointerException exception for com.chat Building report. 11-20 15:51:23.278 12797-12941/? E/ACRA: com.chat fatal error : Attempt to invoke virtual method 'android.view.View android.view.Window.getDecorView()' on a null object reference java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.getDecorView()' on a null object reference at android.app.Activity.initWindowDecorActionBar(Activity.java:2397) at android.app.Activity.getActionBar(Activity.java:2339) at com.chat.activities.ChatActivity.onContactChange(ChatActivity.java:276) at com.chat.utilities.adapters.ChatCustomAdapter$1.onClick(ChatCustomAdapter.java:74) at android.view.View.performClick(View.java:5678) at android.view.View$PerformClick.run(View.java:22667) at android.os.Handler.handleCallback(Handler.java:836) at android.os.Handler.dispatchMessage(Handler.java:103) at android.os.Looper.loop(Looper.java:203) at android.app.ActivityThread.main(ActivityThread.java:6293) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1065) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:926)
Please guide me to solve the solution for the same.