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 8542133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:59:20+00:00 2026-06-11T11:59:20+00:00

I have an Activity which has two different layouts based on grid view and

  • 0

I have an Activity which has two different layouts based on grid view and full page gallery view, when user clicks on an image in grid view it goes to full screen gallery. When the user in full screen Gallery and when back button is clicked it goes back to grid view. Finally if the user again clicks back button once more I want Activity to terminates.

Here is How I handle switching views on back button pressed, It works well except finishing activity. I have null pointer exception on the line I call finish(). How can I handle it?

@Override
public void onBackPressed() {

    ViewPager mPager = (ViewPager) findViewById(R.id.gallery_view);

    if(mPager.getVisibility() == View.VISIBLE){

        setContentView(R.layout.thumbnail_gallery);
        GridView g = (GridView) findViewById(R.id.myGrid);
        g.setAdapter(new GridGallery(this));

        return;

    }else {
        finish();
        return;
    }
}
  • 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-11T11:59:20+00:00Added an answer on June 11, 2026 at 11:59 am

    I have found the reason of NullPointerException in the end. The method findViewById causes it. Instead of checking the visibility of the layout, I have used a boolean variable to check it. Basically when I set the the content view to a layout I assign this variable to true, and then when I switch the view I also switch the boolean variable to false. It works quite well.

    @Override
    public void onBackPressed() {
    
        if(isGalleryVisible) {
    
            setContentView(R.layout.thumbnail_gallery);
            GridView g = (GridView) findViewById(R.id.myGrid);
            g.setAdapter(new GridGallery(this));
            isGalleryVisible = false;
            return;
        } else {
            finish();
            return;
        }
    }
    

    And in the place I switch the view I assign it to true. Take a look below.

    GridView g = (GridView) findViewById(R.id.myGrid);
    g.setAdapter(new GridGallery(context));
    isGalleryVisible = false;
    g.setOnItemClickListener(new OnItemClickListener() {
    
      public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
         myPagerAdapter = new MyPagerAdapter(context);
         pager = (ViewPager) findViewById(R.id.gallery_view);
         pager.setAdapter(myPagerAdapter);
         pager.setCurrentItem(position);
         isGalleryVisible = true;
      }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an activity which has two edit text fields: one for the title
I have a Rails 2.3.11 app which has two key Models: Activity Transaction Live
Basically, I got one activity has two layouts, one is gridview, which has mBaseAdapter
I have an activity that has two layouts one in the folder layout and
I have my remote service which tries to run an activity from a different
I have an application which has two Activities, let's call them A and B.
I have two Activities, both have an inner class SurfaceView, which has an inner
I have a View in Android that has an onTouchEvent which stores the x
So I have an Activity which has a FragmentPagerAdapter and inside each fragment I
I have a workflow which has a custom activity which calls a web service.

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.