Search code examples
androidandroid-popupwindow

Problems showing popupWindow


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.


Solution

  • 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.