Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8984225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:02:41+00:00 2026-06-15T21:02:41+00:00

I get a NullPointerException on doing a swipe. What is wrong and how to

  • 0

I get a NullPointerException on doing a swipe. What is wrong and how to correct? The exception is triggered in method onTouchEvent by statement return gestureDetector.onTouchEvent(event);

This activity (List8) is called as a TabActivity from class Tabs3 which is also provided for completess sake.

package myapp.tabnavui;

import myapp.tabnavui.R;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.AbsListView;
import android.widget.Toast;

import java.util.ArrayList;


/**
 * A list view that demonstrates the use of setEmptyView. This example alos uses
 * a custom layout file that adds some extra buttons to the screen.
 */
public class List8 extends ListActivity {

    private GestureDetector gestureDetector;
    PhotoAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Use a custom layout file
        setContentView(R.layout.list_8);

        // Tell the list view which view to display when the list is empty
        getListView().setEmptyView(findViewById(R.id.empty));

        // Set up our adapter
        mAdapter = new PhotoAdapter(this);
        setListAdapter(mAdapter);

        // Wire up the clear button to remove all photos
        Button clear = (Button) findViewById(R.id.clear);
        clear.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                mAdapter.clearPhotos();
            } });

        // Wire up the add button to add a new photo
        Button add = (Button) findViewById(R.id.add);
        add.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                mAdapter.addPhotos();
            } });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }


    /**
     * A simple adapter which maintains an ArrayList of photo resource Ids. 
     * Each photo is displayed as an image. This adapter supports clearing the
     * list of photos and adding a new photo.
     *
     */
    public class PhotoAdapter extends BaseAdapter {

        private Integer[] mPhotoPool = {
                R.drawable.sample_thumb_0, R.drawable.sample_thumb_1, R.drawable.sample_thumb_2,
                R.drawable.sample_thumb_3, R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
                R.drawable.sample_thumb_6, R.drawable.sample_thumb_7};

        private ArrayList<Integer> mPhotos = new ArrayList<Integer>();

        public PhotoAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mPhotos.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            // Make an ImageView to show a photo
            ImageView i = new ImageView(mContext);

            i.setImageResource(mPhotos.get(position));
            i.setAdjustViewBounds(true);
            i.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            // Give it a nice background
            i.setBackgroundResource(R.drawable.picture_frame);
            return i;
        }

        private Context mContext;

        public void clearPhotos() {
            mPhotos.clear();
            notifyDataSetChanged();
        }

        public void addPhotos() {
            int whichPhoto = (int)Math.round(Math.random() * (mPhotoPool.length - 1));
            int newPhoto = mPhotoPool[whichPhoto];
            mPhotos.add(newPhoto);
            notifyDataSetChanged();
        }

    }

    class MyGestureDetector extends SimpleOnGestureListener {
          private static final int SWIPE_MAX_OFF_PATH = 200;
          private static final int SWIPE_MIN_DISTANCE = 50;
          private static final int SWIPE_THRESHOLD_VELOCITY = 200;

        @Override
        public boolean onDown (MotionEvent e) {
            return true;
        }

          @Override
          public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
               if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                   return false;
               // left to right swipe and right to left swipe
               if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                 && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    // left swipe
                    Toast t = Toast.makeText(List8.this, "Left swipe", Toast.LENGTH_LONG);
                    t.show();
                    startActivity(Tabs3.tab1);
                    return true;
               } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                 && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    // right swipe
                    Toast t = Toast.makeText(List8.this, "Right swipe", Toast.LENGTH_LONG);
                    t.show();
                    startActivity(Tabs3.tab3);
                    return true;
               }
               return false;
          }    
    }    
}

Here’s Tabs3:

package myapp.tabnavui;

import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TabHost;
import android.widget.Toast;
import android.app.TabActivity;
import android.content.Intent;

/**
 * An example of tab content that launches an activity via {@link android.widget.TabHost.TabSpec#setContent(android.content.Intent)}
 */
public class Tabs3 extends TabActivity {

    private GestureDetector gestureDetector;
    // View.OnTouchListener gestureListener;
    // Intent go;
    public static Intent tab1, tab2, tab3; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final TabHost tabHost = getTabHost();

        tab1 = new Intent(this, List1.class);
        tabHost.addTab(tabHost.newTabSpec("tab1")
                .setIndicator("list")
                .setContent(tab1));

        tab2 = new Intent(this, List8.class);
        tabHost.addTab(tabHost.newTabSpec("tab2")
                .setIndicator("photo list")
                .setContent(tab2));

        // This tab sets the intent flag so that it is recreated each time
        // the tab is clicked.
        tab3 = new Intent(this, Controls2.class);
        tabHost.addTab(tabHost.newTabSpec("tab3")
                .setIndicator("destroy")
                .setContent(tab3
                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:02:47+00:00Added an answer on June 15, 2026 at 9:02 pm

    Reason

    Tabs3.gestureDetector and List8.gestureDetector are never set, so they are null.

    This is why you get NullPointerException whe you try to use it at gestureDetector.onTouchEvent(event);

    Solution

    Based on this question, you need to set it at creation time with GestureDetector:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // code
        gestureDetector = new GestureDetector(new MyGestureDetector());
        // code
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get a NullPointerException in my onCreate method in some activities. It's hard to
I'm not able to get rid of this NullPointerException in the following class. My
I need to destroy a loader, sometimes I'll get the following exception: java.lang.NullPointerException at
I am just trying to fill my ListView but I get a NullPointerException Here
When I try to call any GL15 function in lwjgl I get A NullPointerException.
It doesn't seem to be working right now. I get a java.lang.NullPointerException I have
GET is a convenient method to post the form id, post the website id
Am I doing this right? I'm trying to implement a simple countdown timer just
I want to show custom dialog with a spinner. Strangely enough, I get NullPointerException
I tried doing this with Apache Commons FileUpload: protected void processRequest(HttpServletRequest request, HttpServletResponse response)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.