-1

I'd like to create a TextView with the name of the person you've just tapped, but when I tap a name, it prints an error message to the console.

      listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView parentView, View childView, int position, long id) {
          System.err.println("clicked " + position);
          System.err.println("clicked " + friendsIO.getPresentationText(position));
          try {
              JSONObject friendInformations = new JSONObject(friendsIO.getPresentationText(position));


              String friendNames = friendInformations.getString("name");
              System.err.println(friendNames);
              TextView peopleName = (TextView) findViewById(R.id.peoplePerson);
              peopleName.setText(friendNames);


              String friendEmails = friendInformations.getString("email");
              System.err.println(friendEmails);
              String friendNumbers = friendInformations.getString("phone number");
              System.err.println(friendNumbers);
              String friendEmailsNumbers = friendNames + "\n" + friendEmails;
//                  TextView peopleInformations = (TextView) findViewById(R.id.peopleInfo);
//                  peopleInformations.setText(friendEmailsNumbers);
          } catch (Exception e) {
              e.printStackTrace();
          }
          final LayoutInflater peopleInflater = LayoutInflater.from(MainActivity.this);
          final View peopleView = peopleInflater.inflate(R.layout.popup2, null);
          final PopupWindow peoplePopup = new PopupWindow(peopleView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
          peoplePopup.showAtLocation(peopleView, Gravity.CENTER, 0, 0);
          final Button peopleBack = (Button) peopleView.findViewById(R.id.cancel_button);
          peopleBack.setOnClickListener(new View.OnClickListener() {
                                            public void onClick(View v) {
                                                peoplePopup.dismiss();
                                            }
                                        }
          );

I print the name to the console first, to make sure that it actually exists and I get the name I tapped (as well as the email and number, obviously), but instead of printing the name to the screen in a TextView, it gives me the following error message:

10-01 17:39:55.505    4259-4259/com.logit.data.returntosender W/System.err﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
10-01 17:39:55.506    4259-4259/com.logit.data.returntosender W/System.err﹕ at com.logit.data.returntosender.MainActivity$1.onItemClick(MainActivity.java:60)

It says the error is at the line

System.err.println(friendNames);

But the TextView is only actually given afterwards, in line 61. I have tried around a bit, and not found what the problem could be. It's technically exactly how it should be, at least that's what I've been able to gather from other StackOverflow questions and a thorough google search. The XML file of the popup with the name (and email and phone number, but that's not part of the question):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@color/colorAroundPopups"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical|center_horizontal">

    <TextView android:id="@+id/peoplePerson"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:background="@color/colorOfPopups"
       android:textStyle="bold"
       android:text=""
       android:textSize="52sp"/>

    <TextView android:id="@+id/peopleInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorOfPopups"
        android:textStyle="normal"
        android:textSize="26sp"/>

    <Button android:id="@+id/cancel_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cancel_it" />

</LinearLayout>

Thanks in advance -Léon

EDIT: I added the part of the code where the popup, containing the TextView, is created. EDIT: I found the found the solution. I had to put the name of the view containing the TextView before findViewById Also, Frank N. Stein, thank you for not helping at all by sending me a question I had already looked through :)

MegaNerd5
  • 19
  • 4
  • I'm guessing peoplePerson is on the list item view? In which case I can see the problem, I'll elaborate in a answer if that is correct. – RobVoisey Oct 01 '15 at 16:04
  • This means that at the time of the click there's no View in the Activity (or Window) that goes by R.id.peoplePerson. How do you prepare and show this popup you talk about? – Eugen Pechanec Oct 01 '15 at 16:08
  • I added "childView" to the findViewById, but now it shows almost the same error, but instead with line 62, being peopleName.setText(friendNames) The View of the popup is a bit further in the code, I'll add it to the post now – MegaNerd5 Oct 01 '15 at 16:11

2 Answers2

1

You are retrieving the view from the activity, if the TextView was on the list item view, you would change...

TextView peopleName = (TextView) findViewById(R.id.peoplePerson);

to...

TextView peopleName = (TextView) childView.findViewById(R.id.peoplePerson);
RobVoisey
  • 1,083
  • 15
  • 24
0

instead of doing this:

TextView peopleName = (TextView) findViewById(R.id.peoplePerson);

do this:

TextView peopleName = (TextView) childView.findViewById(R.id.peoplePerson);

as your textview is a child of the listview.

Aakash
  • 5,181
  • 5
  • 20
  • 37