创建ListView
并在滚动到最终项目时设置动作加载。
ListView
有效,直到我滚动到最终项目,然后运行loadmore ListView
。我收到了这个错误:
07-01 22:48:10.792: E/AndroidRuntime(4691): java.lang.NullPointerException
07-01 22:48:10.792: E/AndroidRuntime(4691): at android.widget.ListView.clearRecycledState(ListView.java:516)
07-01 22:48:10.792: E/AndroidRuntime(4691): at android.widget.ListView.resetList(ListView.java:503)
07-01 22:48:10.792: E/AndroidRuntime(4691): at android.widget.ListView.setAdapter(ListView.java:445)
07-01 22:48:10.792: E/AndroidRuntime(4691): at com.example.MainActivity$1ProcessDataTask.onPostExecute(MainActivity.java:192)
07-01 22:48:10.792: E/AndroidRuntime(4691): at com.example.MainActivity$1ProcessDataTask.onPostExecute(MainActivity.java:1)
07-01 22:48:10.792: E/AndroidRuntime(4691): at android.os.AsyncTask.finish(AsyncTask.java:631)
07-01 22:48:10.792: E/AndroidRuntime(4691): at android.os.AsyncTask.access$600(AsyncTask.java:177)
07-01 22:48:10.792: E/AndroidRuntime(4691): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
07-01 22:48:10.792: E/AndroidRuntime(4691): at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 22:48:10.792: E/AndroidRuntime(4691): at android.os.Looper.loop(Looper.java:137)
07-01 22:48:10.792: E/AndroidRuntime(4691): at android.app.ActivityThread.main(ActivityThread.java:5103)
07-01 22:48:10.792: E/AndroidRuntime(4691): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 22:48:10.792: E/AndroidRuntime(4691): at java.lang.reflect.Method.invoke(Method.java:525)
07-01 22:48:10.792: E/AndroidRuntime(4691): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-01 22:48:10.792: E/AndroidRuntime(4691): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-01 22:48:10.792: E/AndroidRuntime(4691): at dalvik.system.NativeStart.main(Native Method)
NullPointException
我使用listView.setAdapter(adapter);
方法onPostExcute
进入AsyncTask
。
这是我的班级:
public class MainActivity extends ActionBarActivity {
List<Person> listData;
ListView listView;
CustomAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.person_layout);
listView = (ListView) findViewById(R.id.list_view_person);
listView.setDivider(null);
listData = new ArrayList<Person>();
processData();
listView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
if(totalItemCount > 0 && firstVisibleItem + visibleItemCount == totalItemCount) {
processData();
}
}
});
}
public void processData(){
class ProcessDataTask extends AsyncTask<Void, Void, String> {
public ProcessDataTask() {
super();
}
@Override
protected String doInBackground(Void... params) {
.........//add data to listData here
return null;
}
@Override
protected void onPostExecute(String result) {
if(listData.size() > 0){
adapter = new CustomAdapter(listData, MainActivity.this);
if(listView != null && adapter != null)
listView.setAdapter(adapter);
}
super.onPostExecute(result);
}
}
ProcessDataTask processData = new ProcessDataTask();
processData.execute();
}
}
有什么建议可以解决我的错误吗?
答案 0 :(得分:1)
您忘记了ArrayList
初始化...添加此代码
listData = new ArrayList<Person>();
processData();