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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:30:12+00:00 2026-06-10T06:30:12+00:00

I have been working on the implementation of buttons to update a textview within

  • 0

I have been working on the implementation of buttons to update a textview within my pageadapter. This is the code I have thus far for my pageadapter

public class CharacterStatsPagerAdapter extends PagerAdapter {
public int getCount() {
    return 2;
}

int maxhp = 0;
View view = null;

public Object instantiateItem(View collection, int position) {
    LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    switch (position) {
    case 0:

        view = inflater.inflate(R.layout.characterstats1, null);

        TextView characternamedisplay = (TextView) view.findViewById(R.id.textView1);
        characternamedisplay.setText(com.echaractersheet.BasicInfo.characternameresult);

        TextView genderdisplay = (TextView) view.findViewById(R.id.textView2);
        genderdisplay.setText(com.echaractersheet.BasicInfo.genderresult);

        TextView classdisplay = (TextView) view.findViewById(R.id.textView3);
        classdisplay.setText(com.echaractersheet.BasicInfo.classresult);

        TextView racedisplay = (TextView) view.findViewById(R.id.textView4);
        racedisplay.setText(com.echaractersheet.BasicInfo.raceresult);

        ImageButton plusmaxhp = (ImageButton) view.findViewById(R.id.imageButton1);
        plusmaxhp.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                maxhp = maxhp + 1;
                TextView maxhpdisplay = (TextView) view.findViewById(R.id.textView5);
                maxhpdisplay.setText(maxhp);

            }

        });

        break;
    case 1:
        view = inflater.inflate(R.layout.characterstats2, null);
        break;
    }

    ((ViewPager) collection).addView(view, 0);

    return view;
}

The first 4 textviews update perfectly fine but the problem comes in with my imagebutton. When I click the button to update a characters hp, the program force closes and according to logcat, its a null pointer exception and I am unsure why it is happening.

I have been referencing Button activity with viewPager? and How to write button onClick method in viewpager? but have been having little success to getting rid of my null pointer exception.

Any help would be greatly appreciated. Thank you!

  • 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-10T06:30:13+00:00Added an answer on June 10, 2026 at 6:30 am

    Have fixed Your code.
    Please, refer below:

    public static class CharacterStatsPagerAdapter extends PagerAdapter {
    public int getCount() {
        return 2;
    }
    
    int maxhp = 0;
    View view = null;
    View view2 = null;
    
    public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
        View localView = null;
        switch (position) {
            case 0:
    
                view = inflater.inflate(R.layout.page1, null);
    
                TextView characternamedisplay = (TextView) view.findViewById(R.id.textView1);
                characternamedisplay.setText(R.string.hello_world);
    
                TextView genderdisplay = (TextView) view.findViewById(R.id.textView2);
                genderdisplay.setText(R.string.hello_world);
    
                TextView classdisplay = (TextView) view.findViewById(R.id.textView3);
                classdisplay.setText(R.string.hello_world);
    
                TextView racedisplay = (TextView) view.findViewById(R.id.textView4);
                racedisplay.setText(R.string.hello_world);
    
                ImageButton plusmaxhp = (ImageButton) view.findViewById(R.id.imageButton1);
                plusmaxhp.setOnClickListener(new View.OnClickListener(){
                    public void onClick(View v) {
                        maxhp = maxhp + 1;
                        TextView maxhpdisplay = (TextView) view.findViewById(R.id.textView5);
                        if (maxhpdisplay != null) { 
                            maxhpdisplay.setText(String.valueOf(maxhp));
                        }
                    }
    
                });
                localView = view;
                break;
            case 1:
                view2 = inflater.inflate(R.layout.page2, null);
                localView = view2;
                break;
        }
    
        ((ViewPager) collection).addView(localView, 0);
    
        return localView;
    }
    
    @Override
    public boolean isViewFromObject(View view, Object o) {
    
        return (view == (View)o);
    }
    }
    

    Seems the issue was in the following: You’ve used the same variable for both view. If pager at first makes call to obtain first item and later – second one, then Your variable will hold reference to second item which doesn’t have textView5 in where.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have been working on this question for a couple hours and have come close
I have been working on this app for at least 3-4 months and just
I have been working on this sort of ATM (With a maximum of 50
I have been working on an ECMAScript implementation and I am currently working on
So far I have been working on Iphone applications which use SOAP service to
I have been working on a Java project for a class for a while
I have been working on this plugin system. I thought I passed design and
I have been working on this for a couple of days and am quite
I have been working on a class and it is all big and ugly
I have been working with SQL Server as a Developer a while. One thing

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.