0

I just want to accomplish this:

TextView    TextView    TextView

TextView    EditText    EditText
TextView    EditText    EditText
TextView    EditText    EditText
..and so on

This has to be done dynamically.

I've implemented a test code in this way but i get error of java null pointer exception for addview(). This is my XML file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/sem_scores" >

<TextView
    android:id="@+id/tvSemester"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="21dp"
    android:layout_marginTop="17dp"
    android:text="Semester"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/tvCgpa"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/tvSemester"
    android:layout_alignBottom="@+id/tvSemester"
    android:layout_marginRight="34dp"
    android:layout_toLeftOf="@+id/tvCredits"
    android:text="CGPA"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/tvCredits"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/tvCgpa"
    android:layout_alignBottom="@+id/tvCgpa"
    android:layout_alignParentRight="true"
    android:layout_marginRight="17dp"
    android:text="Total credits"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:text="@string/next" />

And this is my class file:

public class SemScores extends Activity {

// public DataBaseHelper dbHelper=new DataBaseHelper(this);
static int cur_sem;
public RelativeLayout rl;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    rl=(RelativeLayout)findViewById(R.id.sem_scores);
    initialize();
    setContentView(R.layout.sem_scores);
}

void initialize()
{
    TextView[] tvArray=new TextView[8];
    RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); 
    cur_sem=DataBaseHelper.convertsem(MainActivity.cur_sem);
    for(int i=0;i<cur_sem;i++)
    {
        tvArray[i]=new TextView(this);
        tvArray[i].setId(i);
        tvArray[i].setText("heloooo");

        params.addRule(RelativeLayout.ALIGN_LEFT,R.id.tvSemester);

        //I'll add other rules later this is just a test code
        //params[i].addRule(RelativeLayout.BELOW);
        rl.addView(tvArray[i],params);
    }
}

}

LogCat:

03-08 19:32:56.041: E/AndroidRuntime(30898): java.lang.RuntimeException:
Unable to start activity
ComponentInfo{com.testlab.testing/com.testlab.testing.SemScores}:
java.lang.NullPointerException  

I'm getting a feeling like there's a simple error somewhere, just help me out. Thanks.

Update: Oh man, I feel pretty stupid now :D

Gurupad Mamadapur
  • 989
  • 1
  • 13
  • 24

1 Answers1

0

You are trying to get a reference to a View before setContentView() is called; the View hierarchy has not been inflated yet. Move setContentView(R.layout.sem_scores); right below super.onCreate()

Emmanuel
  • 13,083
  • 4
  • 39
  • 53