-2

I'm trying to do a infinite scroll like twitter, I'm using Android-Infinite-Scroll-Listview but always appears this error.

FATAL EXCEPTION: AsyncTask #1
Process: candel.ramon.cloudy, PID: 14907
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at candel.ramon.cloudy.TablonCloudy$1$1.doInBackground(TablonCloudy.java:75)

This is my class:

public class TablonCloudy extends Activity {

    // A setting for how many items should be loaded at once from the server
    private static final int SEVER_SIDE_BATCH_SIZE = 10;

    private InfiniteScrollListView infiniteScroll;

    private TablonAdapter tablonAdapter;
    // cambiar nombre y datos de esta clase
    private BogusRemoteService bogusRemoteService;
    private Handler handler;
    private AsyncTask<Void, Void, List<String>> fetchAsyncTask;

    private Map<String, Integer> sushiMappings;
    // formas de cargar
    private LoadingMode loadingMode = LoadingMode.SCROLL_TO_TOP;
    private StopPosition stopPosition = StopPosition.START_OF_LIST;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tablon_cloudy);
        handler = new Handler();

        infiniteScroll = (InfiniteScrollListView) this
                .findViewById(R.id.infinite_scrollview);

        infiniteScroll.setLoadingMode(loadingMode);
        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        infiniteScroll.setLoadingView(layoutInflater.inflate(
                R.layout.loading_view, null));

        tablonAdapter = new TablonAdapter(new NewPageListener() {

            @Override
            public void onScrollNext() {
                fetchAsyncTask = new AsyncTask<Void, Void, List<String>>() {
                    @Override
                    protected void onPreExecute() {
                        // Loading lock to allow only one instance of loading
                        tablonAdapter.lock();
                    }

                    @Override
                    protected List<String> doInBackground(Void... params) {
                        List<String> result;
                        // Mimic loading data from a remote service
                        if (loadingMode == LoadingMode.SCROLL_TO_TOP) {
                            result = bogusRemoteService
                                    .getNextMessageBatch(SEVER_SIDE_BATCH_SIZE);
                        } else {
                            result = bogusRemoteService
                                    .getNextSushiBatch(SEVER_SIDE_BATCH_SIZE);
                        }
                        return result;
                    }

                    @Override
                    protected void onPostExecute(List<String> result) {
                        if (isCancelled() || result == null || result.isEmpty()) {
                            tablonAdapter.notifyEndOfList();
                        } else {
                            // Add data to the placeholder
                            if (loadingMode == LoadingMode.SCROLL_TO_TOP) {
                                tablonAdapter.addEntriesToTop(result);
                            }
                            // Add or remove the loading view depend on if there
                            // might be more to load
                            if (result.size() < SEVER_SIDE_BATCH_SIZE) {
                                tablonAdapter.notifyEndOfList();
                            } else {
                                tablonAdapter.notifyHasMore();
                            }
                            // Get the focus to the specified position when
                            // loading completes
                            if (loadingMode == LoadingMode.SCROLL_TO_TOP) {
                                infiniteScroll
                                        .setSelection(result.size() < SEVER_SIDE_BATCH_SIZE ? 0
                                                : 1);

                            }
                        }
                    };

                    @Override
                    protected void onCancelled() {
                        // Tell the adapter it is end of the list when task is
                        // cancelled
                        tablonAdapter.notifyEndOfList();
                    }
                }.execute();
            }

            @Override
            public View getInfiniteScrollListView(int position,
                    View convertView, ViewGroup parent) {
                // Customize the row for list view
                if (convertView == null) {
                    LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = layoutInflater.inflate(R.layout.row_demo,
                            null);
                }
                String name = (String) tablonAdapter.getItem(position);
                if (name != null) {
                    TextView rowName = (TextView) convertView
                            .findViewById(R.id.row_name);
                    ImageView rowPhoto = (ImageView) convertView
                            .findViewById(R.id.row_photo);
                    rowName.setText(name);
                    if (loadingMode == LoadingMode.SCROLL_TO_TOP) {
                        rowPhoto.setImageResource(position % 2 == 0 ? R.drawable.conversation_driver
                                : R.drawable.conversation_officer);
                    } else {
                        rowPhoto.setImageResource(sushiMappings.get(name));
                    }
                }
                return convertView;
            }
        });

        infiniteScroll.setAdapter(tablonAdapter);

        // Display a toast when a list item is clicked
        infiniteScroll.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    final int position, long id) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(
                                TablonCloudy.this,
                                tablonAdapter.getItem(position) + " "
                                        + getString(R.string.ordered),
                                Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

Anyone knows why appears this error or other library for do the infinite scroll view like twitter or facebook?

CandelSR
  • 19
  • 5

1 Answers1

1

You have NullPointerException here:

  TablonCloudy.java:75

most probably bogusRemoteService is null because from your code it looks like it is never assigned a non null value

marcinj
  • 44,446
  • 9
  • 70
  • 91