1

I've specified a class, based on another one in an existing Android project. The addRow() method is supposed to dynamically add rows to a table. When creating a new TextView to add to my row and also when creating that row, I'm supposed to specify the "context". The current way, trying "getApplicationContext()" throws a NullPointerException. So where am I supposed to get that context from?

public class DistanceTableView extends ContactListActivity
{
    public void addRow(LocationMessage locationMsg){
        View messageView = theInflater.inflate(R.layout.homepage, null);
        TableLayout table = (TableLayout)messageView.findViewById(R.id.distanceTable);

        TextView senderNameTextView = new TextView(getApplicationContext());
        senderNameTextView.setText(locationMsg.getSenderName());

        TableRow tr = new TableRow(getApplicationContext());
        tr.addView(distanceTextView);
        table.addView(tr);

        rows.addFirst(messageView);
    }
}

The class that my view is extending:

public class ContactListActivity extends MapActivity implements
        ConnectionListener {}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Lars
  • 523
  • 1
  • 6
  • 20
  • How are you using DistanceTableView? Also, it might help if we see how ContactListActivity is implemented. – McStretch Jan 31 '11 at 13:42

2 Answers2

3

I guess you have to pass the context to the constructor of your class.

AndyAndroid
  • 4,039
  • 14
  • 44
  • 71
  • That helped. I thought the context would always be the same when I'm extending that class. Thanks – Lars Jan 31 '11 at 13:47
0

Try this instead:

TableRow tr = new TableRow(this);

hanspeide
  • 2,819
  • 4
  • 25
  • 33