1

我已经指定了一个类,基于现有 Android 项目中的另一个类。addRow() 方法应该动态地将行添加到表中。在创建新的 TextView 以添加到我的行以及创建该行时,我应该指定“上下文”。当前的方式,尝试“getApplicationContext()”会抛出 NullPointerException。那么我应该从哪里获得上下文呢?

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

我的观点正在扩展的课程:

public class ContactListActivity extends MapActivity implements
        ConnectionListener {}
4

2 回答 2

3

我猜你必须将上下文传递给你的类的构造函数。

于 2011-01-31T13:34:46.733 回答
0

试试这个:

TableRow tr = new TableRow(this);

于 2011-01-31T13:35:03.333 回答