【发布时间】:2015-03-05 08:09:55
【问题描述】:
您好,我正在使用 ASYNC 任务来解析来自 json 的数据并将其列入活动。但是,如果没有 addFooterView,它可以正常工作。当我尝试添加它时,它给了我空指针异常。
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
new FilesTask(baseurl, true).execute();
footerView = getLayoutInflater().inflate(R.layout.listview_footer, null); }
@Override
protected void onPreExecute() {
if (isLoading){
this.cancel(true);
} else {
isLoading = true;
}
if (initialload){
pDialog = (RelativeLayout) ll.findViewById(R.id.progressBarHolder);
if (pDialog.getVisibility() == View.GONE) {
pDialog.setVisibility(View.VISIBLE);
feedListView.setVisibility(View.GONE);
}
if (null != feedList){
feedList.clear();
}
if (null != feedListView){
feedListView.setAdapter(null);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
feedListView.addFooterView(footerView);
}
} else {
feedListView.addFooterView(footerView);
}
}
@Override
protected void onPostExecute(Void result) {
if (null != feedList) {
//set adapter
updateList(initialload);
if (pDialog.getVisibility() == View.VISIBLE) {
pDialog.setVisibility(View.GONE);
feedListView.setVisibility(View.VISIBLE);
// Helper.revealView(feedListView,ll);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
feedListView.addFooterView(footerView);
}
} else if(footerView.getVisibility()== View.VISIBLE) {
feedListView.addFooterView(footerView);
}
}else{ // else feedList is empty
pDialog.setVisibility(View.GONE);
}
isLoading = false;
}
供参考
public void updateList(boolean initialload) {
if (initialload){
feedListAdapter = new CustomListAdapter(getApplicationContext(), 0, feedList);
feedListView.setAdapter(feedListAdapter);
} else {
feedListAdapter.addAll(feedList);
feedListAdapter.notifyDataSetChanged();
}
}
这里我粘贴了 onPreExecute 和 postexecute。如果我在这里做错了什么,请告诉我。错误发生在
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
feedListView.addFooterView(footerView);
}
931-931/com.example E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.search.SearchActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.search.SearchActivity$DownloadFilesTask.onPreExecute(SearchActivity.java:198)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
at android.os.AsyncTask.execute(AsyncTask.java:534)
at com.example.search.SearchActivity.onCreate(SearchActivity.java:105)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
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:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
-
你的
footerView在哪里初始化 -
@A.S. public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); footerView = getLayoutInflater().inflate(R.layout.listview_footer, null); }
-
您的 footerView 可能为空,您能详细说明一下它的实例化吗?
-
@Bubu 在 SDK KITKAT 下方,无需页脚即可正常工作。在 SDK Kitkat 之上,它适用于页脚。 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); footerView = getLayoutInflater().inflate(R.layout.listview_footer, null); }
-
您可以尝试在设置适配器之前添加页脚吗?
标签: android android-activity android-listview