Проблемы с отображением всплывающего окна

Я разрабатываю приложение для чата для Android.

Я застрял, показывая контакты во всплывающем окне. Я притворяюсь, что показываю их в правой части экрана, как Google+ показывает меню. Проблема в том, что я искал много структур, чтобы показать их, и, наконец, тот, который я видел, который не устарел, - это popupWindow. Пожалуйста, если это неправильно, я буду признателен за вашу помощь.

Когда я пытаюсь выполнить код, я получаю эту ошибку (я пробовал много вещей, и на самом деле это то, что у меня есть): java.lang.NullPointerException: попытка вызвать виртуальный метод 'void android.widget.TextView.setText (java .lang.CharSequence)» для ссылки на нулевой объект в osmuogar.letter.interfaz.conversacion.Sala.showContacts(Sala.java:100)

Код Java---

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);

Мои макеты --- --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>

--контакт

<?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>

Спасибо за помощь.


person Community    schedule 25.01.2016    source источник
comment
удалить android:layout_toRightOf=@+id/imageView в вашем текстовом представлении   -  person sasikumar    schedule 25.01.2016
comment
это фрагмент или активность?   -  person sasikumar    schedule 25.01.2016
comment
Это часть деятельности, но не сама деятельность. Он выполняется, когда кто-то нажимает кнопку. Танки вам в помощь   -  person    schedule 26.01.2016


Ответы (1)


Вы надуваете R.layout.contact макет layout.inflate(popupView.getContext(),R.layout.contact,null); и хотите найти TextView с идентификатором contactname, но находите contantname вид из другого надутого вида. Так что вы должны сделать так

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);

Надеюсь это поможет.

person Mustansar Saeed    schedule 25.01.2016
comment
Спасибо за ваше сообщение. Это решило мою настоящую проблему, но я получаю другую ошибку, когда выполняется строка popupView.addView(contactView): java.lang.IllegalStateException: указанный дочерний элемент уже имеет родителя. Сначала вы должны вызвать removeView() для родителя дочернего элемента. - person ; 26.01.2016
comment
Я решил все проблемы, но все еще не работает нормально. Я добавил больше информации к моему вопросу. Большое спасибо - person ; 26.01.2016