1

I am developing a chat application for android.

I have get stucked showing the contacts in a popupWindow. What I pretend to do is to show them in the right side of the screen, like google+ shows the menus. The problem is I have searched for many structures to show them and finally the one I have seen which is not deprecated is popupWindow. Please if this is wrong i will appreciate your help.

When I try to exec the code I got this error(I have tried a lot of things and this is actually what I have): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at osmuogar.letter.interfaz.conversacion.Sala.showContacts(Sala.java:100)

Java code---

public void showContacts(MenuItem item){
    LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    final ViewGroup popupView = (ViewGroup)layoutInflater.inflate(R.layout.lista_amigos, null);
    PopupWindow popupWindow = new PopupWindow(popupView, RadioGroup.LayoutParams.WRAP_CONTENT,
            RadioGroup.LayoutParams.WRAP_CONTENT);

    final LinearLayout layout = (LinearLayout) popupView.findViewById(R.id.linearLayoutAmigos);

    layout.inflate(popupView.getContext(),R.layout.contact,null);
    TextView contactName = (TextView) layout.findViewById(R.id.contactname);
    contactName.setText("contacto1");
    popupView.addView(layout);

    popupWindow.setFocusable(true);
    popupWindow.update();
    popupWindow.showAtLocation(findViewById(R.id.mainLayout), Gravity.RIGHT,0,0);

My layouts--- --lista_amigos

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/scrollViewAmigos">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/linearLayoutAmigos">
        </LinearLayout>
    </ScrollView>

</LinearLayout>

--contact

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:background="#ffffff">
    <TextView
        android:id="@+id/contactname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/imageView"
        android:text="contact"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@android:color/black"
        android:textSize="24sp" />
</RelativeLayout>

Thank you for your help.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

1 Answers1

0

You are inflaing R.layout.contact layout layout.inflate(popupView.getContext(),R.layout.contact,null); and you want to find TextView with id contactname but you are finding contantname view from other inflated view. So you should do like this

View contactView = (View)layout.inflate(popupView.getContext(),R.layout.contact,null);
TextView contactName = (TextView) contactView.findViewById(R.id.contactname);
    contactName.setText("contacto1");
    popupView.addView(contactView);

Hope this helps.

Mustansar Saeed
  • 2,730
  • 2
  • 22
  • 46
  • Thanks for your message. It solved my actual problem but i am getting another error when the line "popupView.addView(contactView)" is executed: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. –  Jan 26 '16 at 15:48
  • I solved all problems, but still doesn't work fine. I added more info to my question. Thank you so much –  Jan 26 '16 at 16:10