我遇到了一个我一生都无法理解的错误。我有一个侦听器,它进行 Web 服务调用并从其他两个类 practiceRoundListActivity 和 qualifierRoundListActivity 填充静态数组列表。因此,在下面的代码中,在 foreach 循环中,我有两个条件来决定将插入事件的数组列表。第一个条件“praciticeRoundListActivity.values ...”工作正常,但对“qualifierRoundListActivity.values ...”的调用不起作用,我得到一个空指针异常。
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if (arg2 != 0) {
String name = (String) arg0.getItemAtPosition(arg2);
Log.d(TAG, name);
Team team = SpectatorActivity.map2.get(name);
Log.d(TAG, team.getGSID());
String url = "http://qualifiers.golfstat.com/webservices/remote.cfc?method=getEventsByGSID&GSID=";
url += team.getGSID();
wt.execute(url, team);
//practiceRoundListActivity.values = new ArrayList<String>();
//qualifierRoundListActivity.values2 = new ArrayList<String>();
for(Event event : team.getEvents()) {
if(event.getType() == 'p') {
Log.d(TAG, "PR found");
practiceRoundListActivity.values.add(event.getEventDescription() + " " + event.getTournamentDescription());
} else if(event.getType() == 'q') {
Log.d(TAG, "QR found");
// //HERE IS THE BUG qualifierRoundListActivity.values.add(event.getEventDescription() + " " + event.getTournamentDescription());
}
}
tabHost.setCurrentTab(1);
tabHost.setVisibility(0);
}
}
作为列表活动, practiceRoundListActivity 和 qualifierRoundListActivity 附加到要显示的数组适配器。我已经用尽了每一个想法来学习一些东西,任何关于为什么会发生这种情况的东西。最近,由于 practiceRound... 和 qualifierRound... 非常相似,我将 practiceRound... 类的代码复制到 qualifierRound... 并将所有必要的名称更改为 quailifierRound...(我知道这些类名很烦人长)。预先感谢您的任何帮助。
public class qualifierRoundListActivity extends ListActivity {
static ListView listView;
ArrayAdapter<String> adapter;
public static ArrayList<String> values;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.round_list);
listView = getListView();
values = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(), LeaderBoardActivity.class);
startActivity(intent);
}
});
}
public static void setMultipleChoice() {
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
@Override
public void onPause() {
super.onPause();
listView.setOnItemLongClickListener(null);
}
}
Logcat:第 154 行是“qlualifierRoundListActivity.values.add(...”行。
03-20 23:05:59.272: E/AndroidRuntime(2392): FATAL EXCEPTION: main
03-20 23:05:59.272: E/AndroidRuntime(2392): java.lang.NullPointerException
03-20 23:05:59.272: E/AndroidRuntime(2392): at com.gstat.activities.SpectatorActivity.onItemSelected(SpectatorActivity.java:154)
03-20 23:05:59.272: E/AndroidRuntime(2392): at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
03-20 23:05:59.272: E/AndroidRuntime(2392): at android.widget.AdapterView.access$200(AdapterView.java:42)
03-20 23:05:59.272: E/AndroidRuntime(2392): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
03-20 23:05:59.272: E/AndroidRuntime(2392): at android.os.Handler.handleCallback(Handler.java:587)
03-20 23:05:59.272: E/AndroidRuntime(2392): at android.os.Handler.dispatchMessage(Handler.java:92)
03-20 23:05:59.272: E/AndroidRuntime(2392): at android.os.Looper.loop(Looper.java:123)
03-20 23:05:59.272: E/AndroidRuntime(2392): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-20 23:05:59.272: E/AndroidRuntime(2392): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 23:05:59.272: E/AndroidRuntime(2392): at java.lang.reflect.Method.invoke(Method.java:507)
03-20 23:05:59.272: E/AndroidRuntime(2392): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-20 23:05:59.272: E/AndroidRuntime(2392): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-20 23:05:59.272: E/AndroidRuntime(2392): at dalvik.system.NativeStart.main(Native Method)
因此,我将 qualifierRoundListActivity 显示在选项卡中,下面是我从主类启动它的方式。
public void setupTab(final View view, final String tag) {
View tabView = createTabView(tabHost.getContext(), tag);
TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(tabView);
Intent intent;
if(tag == "Practice Rounds") {
intent = new Intent(this, practiceRoundListActivity.class);
setContent.setContent(intent);
}
else {
intent = new Intent(this, qualifierRoundListActivity.class);
setContent.setContent(intent);
}
tabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context).inflate(R.layout.tabs_small, null);
TextView textView = (TextView) view.findViewById(R.id.tabsText);
textView.setText(text);
return view;
}
当我在 qualifier... 类中添加静态方法时:
public static void add(String event) {
values.add(event);
}
我在 values.add(event) 行收到一个空指针异常。