访问静态arraylist时,Android空指针异常

时间:2013-04-05 23:00:50

标签: android static arraylist android-widget nullpointerexception

我遇到了一个我无法理解的错误。我有一个监听器进行webservice调用,并从另外两个类exerciseRoundListActivity和qualifierRoundListActivity填充静态arraylists。因此,在下面的代码中,在foreach循环中,我有两个条件,它们决定将插入哪个arraylist事件。第一个条件“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附加到要显示的arrayadapter。我已经筋疲力尽地想要学习一些东西,以及为什么会这样。最近,由于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;
}

当我在限定符... class:

中添加静态方法时
public static void add(String event) {
    values.add(event);
}

我在values.add(event)行收到了一个空指针异常。

2 个答案:

答案 0 :(得分:0)

当Activity活动结束其生命周期并被垃圾收集时,将删除使用Activity初始化的静态数据。你不应该认为它会存在。

不是直接访问数据,而是在静态方法中查询数据。该方法将首先检查静态数据是否为空,如果是,则在返回之前重新填充它。

Android Activity生命周期令人困惑,您可能已经对活动的行为方式做出了一些合理但不正确的假设。

您的解决方案可能是保留此数据(例如:在SharedPreferences中),除非绝对必要,否则不要将其存储为静态数据。

答案 1 :(得分:-1)

尝试将values = new ArrayList();在setContentView

之前