简体   繁体   中英

NullPointerException getting a TextView

I am creating an extremely simple Android application to serve as a proof of concept and a reference for an application I will create in the future.

My goal is to create a ListView , each item containing a TextView and a Button that, when pressed, makes a Toast pop up displaying the content of the TextView .

Here is my code and xml:

main.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);
    }

}

I get an Kin the line

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

And for the life of me I can't figure out why. This is likely due to my lack of knowledge to how certain functions in the API work.

Your immediate issue is that findViewById looks in the layout you've inflated with setContentView() and since the Views you are trying to inflate aren't in that layout , they return null .

So this line returns null

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

which causes a NPE at this line as you've noticed

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

because layout is null .

The bigger issue

is the way you are going about using your ListView . Since you want to use a custom layout for the rows, you should be using a custom Adapter and inflate the appropriate layout there . You can then utilize Adapter 's getView() method to set the data in each row as needed.

This tutorial can help you get started on that.

You also may want to watch Google I/O World of ListView

ListView Docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM