0

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.

Shachi
  • 1,858
  • 3
  • 23
  • 41
  • you need to set setSupportActionbar(toobar) first – user3040153 Nov 20 '17 at 10:26
  • Where is outside onCreate? If it is in any method called before onCreate in the lifecycle, the view isn't created yet and you can't load the action bar – Zoe Nov 20 '17 at 10:26
  • @Zoe: But, in the same activity, it is already created before. So, onClick, why does it show as null.... – Shachi Nov 20 '17 at 10:33
  • @user3040153: where should I set the ActionBar? in onCreate or in the method? – Shachi Nov 20 '17 at 10:34
  • @Shachi not if it's called before onCreate in the lifecycle. onCreate inflates the view, any calls before that will give you NPE's because the views don't exist in the active content view – Zoe Nov 20 '17 at 10:34
  • Do u have another method onSubjectChange ? your app crashes on onSubjectChange method not on onContactChange method. – Imene Noomene Nov 20 '17 at 10:35
  • 1
    onCretae after setcontentview – user3040153 Nov 20 '17 at 10:35
  • 4
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zoe Nov 20 '17 at 10:39
  • Nothing is working. It is not a duplicate of NullPointerException. I have already referred it all.. And asking for help only after I can't find an answer.. If your solution doesn't work, atleast don't negative vote it... Someone can be in genuine need of help. I myself am stating that it is a nullpointer exception. I am not able to decipher even after the reason behind it. Check the links in the question that can be the possible reason of NullPointer and help if you know thew answer. – Shachi Nov 20 '17 at 10:48
  • 1
    @Shachi maybe if you added the information requested this wouldn't be a duplicate. WHen you don't have enough information and just some tiny bit of code and an NPE stacktrace, that makes this a duplicate. Add more code, add what your actual code was when the crash happened instead of saying "it doesn't work when I place it in a method". **Which method??** How is it called? When and how matters because of order and threads. – Zoe Nov 20 '17 at 11:03

1 Answers1

0

My suggestion is to use setSupportActionBar() for whole activity. Here in your layout.

 <android.support.v7.widget.Toolbar
        android:id="@+id/home_activity_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways" />


//OnCreate

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

    toolbar = (Toolbar) findViewById(R.id.home_activity_toolbar);
    configureHomeToolBar();
}


private void configureHomeToolBar() {

    toolbar.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary));
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setIcon(R.drawable.my_logo);
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}

Then you will be able to use it by calling getSupportActionBar() anywhere in your activity such as:

getSupportActionBar().setDisplayUseLogoEnabled(false);
getSupportActionBar().setTitle(titlesArray[someIndex]);
Baki Kocak
  • 389
  • 1
  • 9
  • For that you compulsorily need to extend AppCompatActivity instead of Activity, right? – Shachi Nov 20 '17 at 10:58
  • "Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on null object Reference" This gives error as above. – Shachi Nov 20 '17 at 13:57