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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:51:26+00:00 2026-05-23T08:51:26+00:00

I have a view that I’m loading, and my onCreate() method looks like this:

  • 0

I have a view that I’m loading, and my onCreate() method looks like this:

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

       volume_id=getIntent().getStringExtra("volume_id");
       language=getIntent().getStringExtra("language");
       chapter=Integer.parseInt(getIntent().getStringExtra("chapter"));
       book=getIntent().getStringExtra("book");
       book_id=getIntent().getStringExtra("book_id");

       final Button back=(Button)findViewById(R.id.backButton);
        back.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {


            //Toast.makeText(Verse.this, "Back", Toast.LENGTH_LONG).show();

        }});

     final Button next=(Button)findViewById(R.id.forwardButton);
        next.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {


            //Toast.makeText(Verse.this, "Next", Toast.LENGTH_LONG).show();

        }});
     }

For some reason, the addition of those last several lines of code, that link up to two buttons I’ve made in the layout file, are making my app crash. I am not getting anything in the error log, so I have absolutely no idea what is going on.

It may be good to note that without that button adding code in my view, the view loads fine, and the buttons show up where they are supposed to.

Does the way I’m adding those buttons look alright? Am I doing something wrong? I wish it wasn’t so hard to hook up a GUI button to code…

Thanks

  • 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-05-23T08:51:27+00:00Added an answer on May 23, 2026 at 8:51 am

    To answer your question:

    You need to call setContentView(R.layout.main) (or whatever your layout file is called) before you can call findViewById. Also, the final keyword might be messing your code up, but that’s probably not the issue.

    Other possibly helpful information: (You don’t technically need to read any of this)

    Your code’s formatting is also very messy. Consider this:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // Get intent information
        volume_id = getIntent().getStringExtra("volume_id");
        language = getIntent().getStringExtra("language");
        chapter = getIntent().getIntExtra("chapter")); // Changed from String to int
        book = getIntent().getStringExtra("book");
        book_id = getIntent().getStringExtra("book_id");
    
        // Find views by ID
        Button back = (Button) findViewById(R.id.backButton);        
        Button next = (Button) findViewById(R.id.forwardButton);
    
        // Click listeners
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Back", Toast.LENGTH_LONG).show();
            }
        });
    
        next.setOnClickListener(new View.OnClickListener() {        
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Next", Toast.LENGTH_LONG).show();
            }
        });
    }
    

    Notice:
    1. Spaces before and after each =
    2. Spaces after type casting parentheses (e.g. “(Button)“)
    3. Correct indentation
    4. No extraneous blank lines (for example, between @Override and public void onClick
    5. Helpful comments

    It’s the little things that make your code easier to read from other programmers.

    Also, you might want to consider passing “chapter” as an intExtra instead of parsing it from a String. That wastes precious memory. Change the instance field String to int. Finally, you might want to consider making the Buttons instance fields for the entire class as well.

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

Sidebar

Related Questions

I have a view that looks like this: def login(request): ... # some variables
I have a view that I'm creating in the loadview like this: - (void)loadView
I have a view user control that can post form. This control can be
I have a view That Renders 3 grids what I would like to do
I have a view that is bound to an object called Requisition On this
I have a view that has a list of jobs in it, with data
I have a view that I want to add some custom drawing to. I
I have a View that can vary significantly, depending on the 'mode' a particular
I have a view that is joining two tables and ordering by the first
Image i have a view that is cached with the OutputCache attribute but i

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.