将页脚添加到列表视图

时间:2014-06-05 04:29:17

标签: java android listview

我有一个填充了虚拟"位置的ListView。"这很好用。现在,我试图在ListView(一个progess栏)的底部添加一个页脚。当我这样做时,我得到一个空指针错误,我不知道为什么。有任何想法吗?谢谢!

此行发生错误

listView.addFooterView(footerView);

这是logcat:

06-05 00:43:27.887: D/AndroidRuntime(1839): Shutting down VM
06-05 00:43:27.887: W/dalvikvm(1839): threadid=1: thread exiting with uncaught exception (group=0x419397c0)
06-05 00:43:27.887: E/AndroidRuntime(1839): FATAL EXCEPTION: main
06-05 00:43:27.887: E/AndroidRuntime(1839): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test6/com.example.test6.MainActivity}: java.lang.NullPointerException
06-05 00:43:27.887: E/AndroidRuntime(1839):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
06-05 00:43:27.887: E/AndroidRuntime(1839):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2389)
06-05 00:43:27.887: E/AndroidRuntime(1839):     at android.app.ActivityThread.access$600(ActivityThread.java:153)
06-05 00:43:27.887: E/AndroidRuntime(1839):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
06-05 00:43:27.887: E/AndroidRuntime(1839):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 00:43:27.887: E/AndroidRuntime(1839):     at android.os.Looper.loop(Looper.java:137)
06-05 00:43:27.887: E/AndroidRuntime(1839):     at android.app.ActivityThread.main(ActivityThread.java:5289)
06-05 00:43:27.887: E/AndroidRuntime(1839):     at java.lang.reflect.Method.invokeNative(Native Method)
06-05 00:43:27.887: E/AndroidRuntime(1839):     at java.lang.reflect.Method.invoke(Method.java:525)
06-05 00:43:27.887: E/AndroidRuntime(1839):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
06-05 00:43:27.887: E/AndroidRuntime(1839):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
06-05 00:43:27.887: E/AndroidRuntime(1839):     at dalvik.system.NativeStart.main(Native Method)
06-05 00:43:27.887: E/AndroidRuntime(1839): Caused by: java.lang.NullPointerException
06-05 00:43:27.887: E/AndroidRuntime(1839):     at com.example.test6.MainActivity.onCreate(MainActivity.java:75)
06-05 00:43:27.887: E/AndroidRuntime(1839):     at android.app.Activity.performCreate(Activity.java:5133)
06-05 00:43:27.887: E/AndroidRuntime(1839):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-05 00:43:27.887: E/AndroidRuntime(1839):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2293)

和完整代码:

public class MainActivity extends ActionBarActivity {

ListView listView;
ArrayList<Location> arrayOfLocations;
LocationAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    arrayOfLocations = new ArrayList<Location>();

    View footerView = ((LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
            R.layout.progress_footer, null, false);
        listView.addFooterView(footerView);

    listView = (ListView) findViewById(R.id.listView1);

    // Create the adapter to convert the array to views
    adapter = new LocationAdapter(this, arrayOfLocations);

            // fill list from webserver by using AsyncTask
    FillLocations myList2 = new FillLocations();
    myList2.execute();

    listView.setAdapter(adapter);

    listView.setOnScrollListener(new OnScrollListener() {

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {

            if (listView != null
                    && listView.getAdapter() != null
                    && listView.getChildAt(listView.getChildCount() - 1) != null
                    && listView.getLastVisiblePosition() == listView
                            .getAdapter().getCount() - 1
                    && listView.getChildAt(listView.getChildCount() - 1)
                            .getBottom() <= listView.getHeight()) {

                startLoop += 10;
                endLoop += 10;

            }
        }

    });

}
}

4 个答案:

答案 0 :(得分:3)

尝试这种方式我猜你的contextNULL所以你得到NPE

View footerView = ((LayoutInflater) MainActivity.this.
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
        R.layout.progress_footer, null, false);`

更新

添加你的

listView.addFooterView(footerView);

之后

listView = (ListView) findViewById(R.id.listView1);

答案 1 :(得分:2)

以下是null context的问题。您必须初始化您的上下文。 Check它。

第二期

这里你必须在addFooterView之前初始化listView。您的代码序列应为

首先初始化

listView = (ListView) findViewById(R.id.listView1);

然后

listView.addFooterView(footerView);

答案 2 :(得分:1)

Replace this code:
View footerView = ((LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
            R.layout.progress_footer, null, false);
With this code:
View footerView = LayoutInflater.from(this).inflate(R.layout.progress_footer, null, false);

答案 3 :(得分:1)

试试这个:

View footerView = ((LayoutInflater) MainActivity.this.
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
        R.layout.progress_footer, null, false);
listView.addFooterView(footerView);

context替换为MainActivity.this。直接使用Context有时可能为null,因此会导致 nullpointerexception 。使用ClassName.this代替上下文将提供更明确的上下文值,该值可能不为null。