2 momo1199 Momo1199 于 2013.01.07 17:05 提问

android-AsyncTask更新列表

求助——
在AsyncTask中调用MainActivity,更新了adapter,但是LogCat给出了onPostExecture的NullPointerException,我检查了列表,里面有值,不是空的啊。

AsyncTask.java

protected void onPostExecute(List<MoviesCategory> result){
    super.onPostExecute(result);
    //Here i have Error, i cheked result it's not null.
    //On AsyncTask i have Constructor when i get Activity.
    activity.mMoviesAdapter.addCategory(result);
}

Adapter.java

List<MoviesCategory> mListMovies;

public void addCategory(List<MoviesCategory> list){
    mListMovies.addAll(list);
    notifyDataSetChanged();
}

我做的更新:

更新

FATAL EXCEPTION: main
java.lang.NullPointerException
    at uz.afishamedia.Adapter.MoviesAdapter.addCategory(MoviesAdapter.java:49)
    at uz.afishamedia.Utils.LoadMovies.onPostExecute(LoadMovies.java:74)
    at uz.afishamedia.Utils.LoadMovies.onPostExecute(LoadMovies.java:1)
    at android.os.AsyncTask.finish(AsyncTask.java:631)
    at android.os.AsyncTask.access$600(AsyncTask.java:177)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5191)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
    at dalvik.system.NativeStart.main(Native Method)

2个回答

Kill_it
Kill_it   2013.01.07 17:09
已采纳

问题出在mListMovies本来就是空的,需要对它先初始化

Adapter.java:

List<MoviesCategory> mListMovies = new ArrayList<MoviesCategory>();
Kakalapa1986
Kakalapa1986   2013.01.07 17:11

你的mListMovies没初始化啊。
加上这个就行了:

List<MoviesCategory> mListMovies = new ArrayList<MoviesCategory>();
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!