如果String不为null,为什么我不能创建这个TextView?

时间:2015-10-01 16:01:53

标签: java android textview

我想创建一个TextView,其中包含您刚刚点击的人的名字,但是当我点击一个名称时,它会向控制台输出一条错误消息。

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

我首先将名称打印到控制台,以确保它实际存在,并且我得到了我点击的名称(以及电子邮件和号码,显然),但不是在TextView中将名称打印到屏幕上,它给我以下错误信息:

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)

它表示错误在

System.err.println(friendNames);

但是TextView实际上只是在第61行给出了。我已经尝试了一下,但没有找到问题所在。从技术上讲,它应该是应该如何,至少这是我从其他StackOverflow问题和彻底的谷歌搜索中收集到的。 弹出窗口的XML文件,其名称(以及电子邮件和电话号码,但不是问题的一部分):

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

提前致谢 -Léon

编辑:我添加了代码中创建包含TextView的弹出窗口的部分。 编辑:我发现找到了解决方案。我必须在findViewById之前放置包含TextView的视图的名称 另外,Frank N. Stein,感谢你没有给我提供一个我已经查看过的问题的帮助:)

2 个答案:

答案 0 :(得分:1)

您正在从活动中检索视图,如果TextView在列表项视图中,您将更改...

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

为...

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

答案 1 :(得分:0)

而不是这样做:

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

这样做:

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

因为你的textview是listview的孩子。