0

After reading some similiar problem here, I'm still forced to post my question here.

I have an Activity that should add and show a certain amount of Buttons relative to an amount of games in my SQLite-database. This Activity must be scrollable.

So i have my basic layout-xml-file like this:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    android:paddingLeft="15dp"
    android:paddingRight="15dp" >
    <RelativeLayout 
        android:id="@+id/rel_all_football" 
        android:orientation="vertical"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"        
        >

        <TextView 
            style="@style/TextHeader"
            android:id="@+id/tv_all_football_header"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/all_games"
            />
        <View   
            android:id="@+id/v_cond_line1"
            android:layout_below="@+id/tv_all_football_header"
            android:layout_height="1dp"
            android:layout_width="fill_parent"
            android:layout_marginBottom="10dp"
            android:background="#FFFFFF"
            />
    </RelativeLayout>

</ScrollView>

Now for every entry in the database, I want to add a Button with the certain information in that RelativeLayout under the TextView (header) and the View (simple line).

So after reading some questions here, I tried something like this:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.rel_all_football);

        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        Button button;
        for(int i=0; i<3; i++){
            button = new Button(this);
            button.setLayoutParams(
                    new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
                                     ViewGroup.LayoutParams.WRAP_CONTENT));
            button.setText("Hello " + i);

            layout.addView(button);
        }

This is causing a NullPointerException at layout.addView(button). To tell the truth, it's the first time I'm trying to add content from code and not directly in xml, so I have no clue if I'm doing this right. I know that a View must have at least the attributes layout_width and layout_height declared, so what is wrong here? Am I inflating wrong? Or do I find the error in the creation of the Button?

2
  • 1
    findViewById() will only find Views that are in the current layout, for example in the layout passed to setContentView(). Is the layout that you posted the one you pass the setContentView()?
    – Sam
    Sep 23, 2012 at 17:59
  • Put a breakpoint on layout.addView(button) and run your code. I'd bet that layout is null.
    – Simon
    Sep 23, 2012 at 18:15

1 Answer 1

1

i think you are facing some problem with id's sotTry this

ScrollView scrollView = (ScrollView)getLayoutInflater().inflat(R.layout.filename)
        RelativeLayout layout = (RelativeLayout) scrollView.findViewById(R.id.rel_all_football);
        Button button;
        for(int i=0; i<3; i++){
            button = new Button(this);
            button.setLayoutParams( new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            button.setText("Hello " + i);

            layout.addView(button);
        }

    setContentView(scrollView);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.