我知道我们不能从一个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的值为null。这就是我得到空指针异常的原因吗?