在Activity中将addFooterView添加到Listview会导致空指针异常

时间:2015-03-05 08:09:55

标签: android android-activity android-listview

您好我正在使用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)

2 个答案:

答案 0 :(得分:0)

ListView在setAdapter之前添加FootView或HeadView,请参阅下面的源代码。

 /**
 * Add a fixed view to appear at the bottom of the list. If addFooterView is called more
 * than once, the views will appear in the order they were added. Views added using
 * this call can take focus if they want.
 * <p>NOTE: Call this before calling setAdapter. This is so ListView can wrap the supplied
 * cursor with one that will also account for header and footer views.
 *
 *
 * @param v The view to add.
 */
public void addFooterView(View v) {
    addFooterView(v, null, true);
}

答案 1 :(得分:0)

这里我必须在addFooterView之前初始化listView。

首先初始化

footerView = getLayoutInflater().inflate(R.layout.listview_footer, null);

然后

new FilesTask(baseurl, true).execute();

FilesTask(baseurl,true).execute();将执行set adapter的剩余部分。 实际上这种错误发生在KITKAT下面的SDK。解决方案是 1.初始化listview,footerView 2.做剩余的ASNYC任务