【问题标题】:NullPointerException getting a TextViewNullPointerException 获取 TextView
【发布时间】:2014-12-04 17:41:52
【问题描述】:

我正在创建一个极其简单的 Android 应用程序,作为概念验证和我将来创建的应用程序的参考。

我的目标是创建一个ListView,每个项目都包含一个TextView 和一个Button,按下时会弹出一个Toast,显示TextView 的内容。

这是我的代码和 xml:

ma​​in.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<ListView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/mainListView1"/> 

</LinearLayout>

entry.XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="horizontal"
android:id="@+id/entryLayout"
>

<TextView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:id="@+id/entryTextView1"
    android:padding="10dp"
    android:textSize="25sp"/>

<ImageButton
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@android:drawable/ic_menu_close_clear_cancel"
    android:id="@+id/entryImageButton"
    />

</LinearLayout>

MainActivity.Class

public class MainActivity extends Activity{
/* Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    // Set main.xml as user interface layout
    setContentView(R.layout.main);

    String[] companies = new String[] { "Test1", "Test2", "Test3",
        "Test4", "Test4", "Test6", "Test7", "Test8", "Test9", "Test10"};

    ArrayList<LinearLayout> views =  new ArrayList<LinearLayout>();
    for(int x=0; x!= companies.length-1; x++){

        LinearLayout layout = (LinearLayout) findViewById(R.id.entryLayout);
        TextView text = (TextView) layout.getChildAt(0);
        text.setText(companies[x]);
        ImageButton button = (ImageButton) layout.getChildAt(1);
        button.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v){
                LinearLayout layout = (LinearLayout) v.getParent();
                TextView view = (TextView) layout.getChildAt(0);
                Toast.makeText(MainActivity.this,view.getText(), Toast.LENGTH_SHORT).show();
            }

        });
        views.add(layout);
    }


    ListView listView = (ListView) findViewById(R.id.mainListView1);
    for(LinearLayout layout: views){
        listView.addView(layout);
    }

}

我得到一个亲戚

TextView text = (TextView) layout.getChildAt(0);

对于我的生活,我无法弄清楚为什么。这可能是由于我对 API 中某些函数的工作原理缺乏了解。

【问题讨论】:

标签: java android android-layout android-listview nullpointerexception


【解决方案1】:

您的直接问题是findViewById 看起来在layout 中,您已经用setContentView() 充气,并且由于您尝试充气的Views 不在layout 中,它们返回null .

所以这行返回null

LinearLayout layout = (LinearLayout) findViewById(R.id.entryLayout);

正如您所注意到的,这会导致在这一行出现NPE

TextView text = (TextView) layout.getChildAt(0);

因为layoutnull

更大的问题

是您使用ListView 的方式。由于您想对行使用自定义 layout,因此您应该使用自定义 Adapter 并在此处填充适当的 layout 。然后,您可以根据需要利用AdaptergetView() 方法设置每一行中的数据。

This tutorial 可以帮助您开始。

你可能还想看Google I/O World of ListView

ListView Docs

【讨论】:

  • 我以为我忽略了一个更大的概念。谢谢,我会调查一下
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-03
  • 2017-05-11
  • 1970-01-01
  • 1970-01-01
  • 2012-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多