【发布时间】:2011-03-14 03:38:53
【问题描述】:
我知道我们不能从另一个 Activity 中的 Activity 调用方法。我正在尝试找出解决此问题的最佳方法。
这是我的代码。这是我试图调用的方法。它在我的ScoreCard 活动中。
public void numPlayerSetup(){
{
int[] ids = {
R.id.TextView11, R.id.TextView12, R.id.TextView13
};
for(int i : ids) {
TextView tv = (TextView)findViewById(i);
tv.setVisibility(View.INVISIBLE);
}
}
这是我尝试调用该方法的方式。 score 是 ScoreCard 类的对象。
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){
int item = spinner.getSelectedItemPosition();
if(item==1){
Log.i("error","This Sucks");
score.numPlayerSetup();
}
}
我试图将numPlayerSetup 方法放在一个不会扩展Activity 的不同类中,只包含逻辑,但我不能在不扩展活动的情况下使用findViewById() 方法。
我是这样称呼它的。
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3){
int item = spinner.getSelectedItemPosition();
ArrayList<TextView> myTextViewList = new ArrayList<TextView>();
TextView tv1 = (TextView)findViewById(R.id.TextView14);
myTextViewList.add(tv1);
if(item==1){
Log.i("error","This Sucks");
Setup.numPlayerSetup(myTextViewList);
}
那么这就是我要调用的类。
public class Setup {
TextView tv;
public static void numPlayerSetup(ArrayList<TextView> tvs){
for(TextView tv : tvs) {
Log.i("crash","This Sucks");
tv.setVisibility(View.INVISIBLE); //this line is highlighted in the debugger as the line my error is coming from
}
}
}
它将消息记录在 logcat 中并给我一个空指针异常。调试器说 tv 的值为空。这就是我得到空指针异常的原因吗?
【问题讨论】:
-
移除实例变量TextView tv