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?

4

1 回答 1

1

我认为您在使用 id's sot 时遇到了一些问题试试这个

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);
于 2012-09-23T17:59:50.053 回答