使用时getApplicationContext()抛出一个异常 [英] getApplicationContext() throws an exception when used
问题描述
我指定一个类的基础上,另一位在现有的Android项目。该addRow()方法应该是动态添加行到表中。 当创建一个新的TextView创建行时添加到我行也,我应该指定背景。目前的办法,试图getApplicationContext()抛出了一个NullPointerException异常。 那么,我应该得到这方面?
公共类DistanceTableView扩展ContactListActivity
{
公共无效addRow(LocationMessage locationMsg){
查看消息查看= theInflater.inflate(R.layout.homepage,NULL);
TableLayout表=(TableLayout)messageView.findViewById(R.id.distanceTable);
TextView的senderNameTextView =新的TextView(getApplicationContext());
senderNameTextView.setText(locationMsg.getSenderName());
的TableRow TR =新的TableRow(getApplicationContext());
tr.addView(distanceTextView);
table.addView(TR);
rows.addFirst(消息查看);
}
}
这是我的观点是扩展类:
公共类ContactListActivity扩展MapActivity工具
ConnectionListener {}
我想你有上下文传递到您的类的构造函数。
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 {}
I guess you have to pass the context to the constructor of your class.
这篇关于使用时getApplicationContext()抛出一个异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!