我知道我们无法从另一个活动中的活动调用方法。我正在尝试找出解决这个问题的最佳方法。
这是我的代码。这是我试图调用的方法。它在我的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
class.
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。这就是我收到空指针异常的原因吗?