Android Endless ListView

时间:2015-05-21 13:53:50

标签: android listview android-listview

我试图在我的应用程序上实现无尽的列表视图,但我得到了NullPointerException ...

有人可以帮助我吗?

活动致电课程onCreate

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    this.FormataActionBar();

    ListView list = (ListView) findViewById(R.id.list);
    //Calling the function with the infinte scroll
    list.setOnScrollListener(new EndlessScrollListener() {

        @Override
        public void onLoadMore(int page, int totalItemsCount) {
            // TODO Auto-generated method stub

            PopulateListView(page);
        }
    });

}

PopulateListView方法

private void PopulateListView(int page) {
    if (manager != null) {
        //Here i'm calling a webservice to populate data
        manager.setFretes(FreteDAL.GetFretesAutoList(page));
    }
}

EndlessScrollListener CLASS

package com.polifrete.polifreteclass;

import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;

public abstract class EndlessScrollListener implements OnScrollListener {
// The minimum amount of items to have below your current scroll position
// before loading more.
private int visibleThreshold = 15;
// The current offset index of data you have loaded
private int currentPage = 0;
// The total number of items in the dataset after the last load
private int previousTotalItemCount = 0;
// True if we are still waiting for the last set of data to load.
private boolean loading = true;
// Sets the starting page index
private int startingPageIndex = 0;

public EndlessScrollListener() {
}

public EndlessScrollListener(int visibleThreshold) {
    this.visibleThreshold = visibleThreshold;
}

public EndlessScrollListener(int visibleThreshold, int startPage) {
    this.visibleThreshold = visibleThreshold;
    this.startingPageIndex = startPage;
    this.currentPage = startPage;
}

// This happens many times a second during a scroll, so be wary of the code
// you place here.
// We are given a few useful parameters to help us work out if we need to
// load some more data,
// but first we check if we are waiting for the previous load to finish.
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
    // If the total item count is zero and the previous isn't, assume the
    // list is invalidated and should be reset back to initial state
    // If there are no items in the list, assume that initial items are
    // loading
    if (!loading && (totalItemCount < previousTotalItemCount)) {
        this.currentPage = this.startingPageIndex;
        this.previousTotalItemCount = totalItemCount;
        if (totalItemCount == 0) {
            this.loading = true;
        }
    }

    // If it’s still loading, we check to see if the dataset count has
    // changed, if so we conclude it has finished loading and update the
    // current page
    // number and total item count.
    if (loading) {
        if (totalItemCount > previousTotalItemCount) {
            loading = false;
            previousTotalItemCount = totalItemCount;
            currentPage++;
        }
    }

    // If it isn’t currently loading, we check to see if we have breached
    // the visibleThreshold and need to reload more data.
    // If we do need to reload some more data, we execute onLoadMore to
    // fetch the data.
    if (!loading
            && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
        onLoadMore(currentPage + 1, totalItemCount);
        loading = true;
    }
}

// Defines the process for actually loading more data based on page
public abstract void onLoadMore(int page, int totalItemsCount);

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    // Don't take any action on changed
}
}

LOGCAT OUTPUT

05-21 11:01:38.787: E/AndroidRuntime(30046): FATAL EXCEPTION: main
05-21 11:01:38.787: E/AndroidRuntime(30046): java.lang.RuntimeException:     Unable to start activity  ComponentInfo{com.polifrete.polifreteandroid/com.polifrete.polifreteandroid.FretesProximosActivity}: java.lang.NullPointerException
05-21 11:01:38.787: E/AndroidRuntime(30046):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
05-21 11:01:38.787: E/AndroidRuntime(30046):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
05-21 11:01:38.787: E/AndroidRuntime(30046):    at android.app.ActivityThread.access$700(ActivityThread.java:134)
05-21 11:01:38.787: E/AndroidRuntime(30046):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
05-21 11:01:38.787: E/AndroidRuntime(30046):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 11:01:38.787: E/AndroidRuntime(30046):    at android.os.Looper.loop(Looper.java:137)
05-21 11:01:38.787: E/AndroidRuntime(30046):    at android.app.ActivityThread.main(ActivityThread.java:4867)
05-21 11:01:38.787: E/AndroidRuntime(30046):    at java.lang.reflect.Method.invokeNative(Native Method)
05-21 11:01:38.787: E/AndroidRuntime(30046):    at java.lang.reflect.Method.invoke(Method.java:511)
05-21 11:01:38.787: E/AndroidRuntime(30046):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
05-21 11:01:38.787: E/AndroidRuntime(30046):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
05-21 11:01:38.787: E/AndroidRuntime(30046):    at dalvik.system.NativeStart.main(Native Method)
05-21 11:01:38.787: E/AndroidRuntime(30046): Caused by: java.lang.NullPointerException
05-21 11:01:38.787: E/AndroidRuntime(30046):    at com.polifrete.polifreteandroid.FretesProximosActivity.onCreate(FretesProximosActivity.java:188)
05-21 11:01:38.787: E/AndroidRuntime(30046):    at android.app.Activity.performCreate(Activity.java:5047)
05-21 11:01:38.787: E/AndroidRuntime(30046):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
05-21 11:01:38.787: E/AndroidRuntime(30046):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
05-21 11:01:38.787: E/AndroidRuntime(30046):    ... 11 more

XML CODE

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rlTopo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#F2F2F2"
  tools:context="com.polifrete.polifreteandroid.FretesProximosActivity$PlaceholderFragment" >

<RelativeLayout
    android:id="@+id/rlNrFretes"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/expandirMapa"
        android:layout_width="180dp"
        android:layout_height="30dp"
        android:background="@drawable/botao_vermelho"
        android:fontFamily="sans-serif"
        android:onClick="btnExibirMapa_OnClick"
        android:paddingLeft="10dp"
        android:text="@string/exibirMapa"
        android:textColor="#FFFFFF"
        android:textSize="12sp" />

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:contentDescription="@string/localAtual"
        android:paddingLeft="10dp"
        android:src="@drawable/ic_full_map" />

    <Button
        android:id="@+id/btnFiltrarBusca"
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:background="@drawable/botao_vermelho"
        android:fontFamily="sans-serif"
        android:onClick="imgFiltro_OnClick"
        android:text="@string/btnFiltrarBusca"
        android:textColor="#FFFFFF"
        android:textSize="12sp" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/rlLimparFiltro"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/rlNrFretes"
    android:paddingTop="5dp"
    android:visibility="gone" >

    <TextView
        android:id="@+id/msgNrFretes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:gravity="center"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:text="@string/textView_fretesEncontrados"
        android:textSize="13sp" />

    <TextView
        android:id="@+id/txtFiltroCidade"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/msgNrFretes"
        android:fontFamily="sans-serif"
        android:paddingLeft="10dp"
        android:paddingTop="5dp"
        android:textSize="11sp" />

    <TextView
        android:id="@+id/txtFiltros"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtFiltroCidade"
        android:fontFamily="sans-serif"
        android:paddingLeft="10dp"
        android:paddingTop="5dp"
        android:textSize="11sp" />

    <Button
        android:id="@+id/limparFiltro"
        android:layout_width="100dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:background="@drawable/botao_vermelho"
        android:fontFamily="sans-serif"
        android:onClick="btnLimparFiltro_OnClick"
        android:text="@string/limparFiltro"
        android:textColor="#FFFFFF"
        android:textSize="12sp" />
</RelativeLayout>

<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/rlLimparFiltro"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="15dp"
    android:divider="@android:color/transparent"
    android:dividerHeight="10.0dp" >
</ListView>

</RelativeLayout>

更改后的LOGCAT FOT FRAGMENT_FRETES_PROXIMOS

05-21 11:34:45.934: E/FragmentManager(3037): Activity state:
05-21 11:34:47.385: E/AndroidRuntime(3037): FATAL EXCEPTION: main
05-21 11:34:47.385: E/AndroidRuntime(3037): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.polifrete.polifreteandroid/com.polifrete.polifreteandroid.FretesProximosActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f07004e (com.polifrete.polifreteandroid:id/container) for fragment PlaceholderFragment{42078f00 #0 id=0x7f07004e}
05-21 11:34:47.385: E/AndroidRuntime(3037):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
05-21 11:34:47.385: E/AndroidRuntime(3037):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
05-21 11:34:47.385: E/AndroidRuntime(3037):     at android.app.ActivityThread.access$700(ActivityThread.java:134)
05-21 11:34:47.385: E/AndroidRuntime(3037):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
05-21 11:34:47.385: E/AndroidRuntime(3037):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 11:34:47.385: E/AndroidRuntime(3037):     at android.os.Looper.loop(Looper.java:137)
05-21 11:34:47.385: E/AndroidRuntime(3037):     at android.app.ActivityThread.main(ActivityThread.java:4867)
05-21 11:34:47.385: E/AndroidRuntime(3037):     at java.lang.reflect.Method.invokeNative(Native Method)
05-21 11:34:47.385: E/AndroidRuntime(3037):     at java.lang.reflect.Method.invoke(Method.java:511)
05-21 11:34:47.385: E/AndroidRuntime(3037):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
05-21 11:34:47.385: E/AndroidRuntime(3037):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
05-21 11:34:47.385: E/AndroidRuntime(3037):     at dalvik.system.NativeStart.main(Native Method)
05-21 11:34:47.385: E/AndroidRuntime(3037): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f07004e (com.polifrete.polifreteandroid:id/container) for fragment PlaceholderFragment{42078f00 #0 id=0x7f07004e}
05-21 11:34:47.385: E/AndroidRuntime(3037):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919)
05-21 11:34:47.385: E/AndroidRuntime(3037):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
05-21 11:34:47.385: E/AndroidRuntime(3037):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-21 11:34:47.385: E/AndroidRuntime(3037):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
05-21 11:34:47.385: E/AndroidRuntime(3037):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
05-21 11:34:47.385: E/AndroidRuntime(3037):     at com.polifrete.polifreteandroid.FretesProximosActivity.onStart(FretesProximosActivity.java:231)
05-21 11:34:47.385: E/AndroidRuntime(3037):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
05-21 11:34:47.385: E/AndroidRuntime(3037):     at android.app.Activity.performStart(Activity.java:5057)
05-21 11:34:47.385: E/AndroidRuntime(3037):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2065)
05-21 11:34:47.385: E/AndroidRuntime(3037):     ... 11 more

我解决了这个问题

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
    // leave this empty
}

@Override
public void onScrollStateChanged(AbsListView listView, int scrollState) {

    if (scrollState == SCROLL_STATE_IDLE) {
        if (listView.getLastVisiblePosition() >= listView.getCount() - 1
                - threshold) {
            currentPage++;
            // load more list items:
            PopulateListView(currentPage);
        }
    }
}

private void PopulateListView(final int page) {
    final ProgressDialog progress = ProgressDialog.show(
            FretesProximosActivity.this, "Aguarde!",
            "Carregando fretes...", true);

    progress.show();

    Handler handler = new Handler();

    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            if (manager != null) {
                manager.setFretes(FreteDAL.GetFretesAutoList(page));
            }
            progress.dismiss();
            PreencheListView();

        }
    }, 1000);

}

private void PreencheListView() {

    listView = (ListView) findViewById(R.id.list);
    List<Frete> fretes = manager.getFretes();
    if (fretes != null) {
        fretesAll.addAll(fretes);

        if (listView.getAdapter() == null) {

            FretesListViewAdapter adapter = new FretesListViewAdapter(this,
                    R.layout.frete_row, fretesAll);

            listView.setAdapter(adapter);
        } else {
            ((FretesListViewAdapter) listView.getAdapter()).refill();
        }

        listView.setOnItemClickListener(this);
    }
}

1 个答案:

答案 0 :(得分:0)

似乎您没有ID为BEGIN INSERT INTO WEB_FE_USER_QUERY( LISTING_ID , QUERY_NAME , USER_PREFERENCE , USER_CRITERIA , DEFAULT_QUERY , CREATED_DATE_TIME , IS_GLOBAL ) VALUES( '0' , 'NovatedTrades' , 'test' , 'test' , 0 , TO_DATE( '2013-06-14 20:28:16' , 'YYYY-MM-DD HH24:MI:SS' ) , 0 ); END; 的视图。所以你试图将片段添加到无处。您需要在片段事务的container方法中指出现有的id。