0

我实现了一个 onResume() 方法,里面有一个刷新代码,我遇到了一个错误

这是我的代码

// After completing background task Dismiss the progress dialog
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all events
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                // Updating parsed JSON data into ListView
                adapter = new SimpleAdapter(BookmarkActivity.this,
                        eventsList, R.layout.bookmark_list, new String[] {
                                TAG_EID, TAG_ENAME, TAG_CNAME,
                                TAG_CREATED_AT }, new int[] { R.id.bk_eid,
                                R.id.bk_ename, R.id.bk_cname,
                                R.id.bk_created_at });
                // updating listView
                setListAdapter(adapter);
            }
        });
    }
public void onResume(){
    super.onResume();
    ((BaseAdapter) adapter).notifyDataSetChanged();
}

这是我的 logcat 错误

09-05 07:39:36.223: E/AndroidRuntime(626): Caused by: java.lang.NullPointerException
09-05 07:39:36.223: E/AndroidRuntime(626):  at com.stts.sparetimetradingsystem.BookmarkActivity.onResume(BookmarkActivity.java:215)
09-05 07:39:36.223: E/AndroidRuntime(626):  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
09-05 07:39:36.223: E/AndroidRuntime(626):  at android.app.Activity.performResume(Activity.java:3823)
09-05 07:39:36.223: E/AndroidRuntime(626):  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
09-05 07:39:36.223: E/AndroidRuntime(626):  ... 12 more

我该如何解决这个问题?

4

2 回答 2

0

((BaseAdapter) adapter).notifyDataSetChanged();适配器对象中为空......在使用它之前添加一个适配器不为空的检查..

if(adapter != null)
    ((BaseAdapter) adapter).notifyDataSetChanged();
于 2012-09-05T07:45:56.993 回答
0

onResume在您的任务完成之前调用问题!

在执行此之前

((BaseAdapter) adapter).notifyDataSetChanged();

检查适配器是否不为空

if(adapter!=null)
    ((BaseAdapter) adapter).notifyDataSetChanged();
于 2012-09-05T07:46:24.083 回答