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

  • Home
  • SEARCH
  • 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 9236647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:20:45+00:00 2026-06-18T07:20:45+00:00

I am dividing Preference menu into three parts. How to check if update_button exist

  • 0

I am dividing Preference menu into three parts. How to check if “update_button” exist before creating ClickListener, because i’m getting NullPointerException, if i’m going to fragment where is no “update_button”. Or there are other way? Thank you for your time and answers.

public static class PrefsFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle aSavedState) {
        super.onCreate(aSavedState);
        Context anAct = getActivity().getApplicationContext();
        int thePrefRes = anAct.getResources().getIdentifier(
                getArguments().getString("pref-resource"), "xml", anAct.getPackageName());
        addPreferencesFromResource(thePrefRes);
        try {
            Preference button = (Preference) findPreference("update_button");
            button.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference arg0) {
                    Toast.makeText(getActivity(), "Update Done.",Toast.LENGTH_LONG).show();
                    return true;
                }
            });
        } catch(Exception e) {
            e.printStackTrace();
        }               
    }    
}
  • 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-18T07:20:46+00:00Added an answer on June 18, 2026 at 7:20 am

    One way is to check if button is null.

    Example:

    Preference button = (Preference) findPreference("update_button");
    if (button != null) {
        button.setOnPreferenceClickListener(
            new Preference.OnPreferenceClickListener() {
                // code
            });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do you go about dividing the views into smaller views. Let's say I
C++ was the the first language I've learnt so dividing source code into .h
I've got a 1km square, I'm dividing it up into 100meters both on the
I am currently working on a .net project which I am dividing into different
When dividing your code up into multiple files, what exactly should go into an
In XAML, how do I put a standard dividing line in a menu? eg
I have a problem with dividing site parts to different files so it's easier
I'm playing around a little with dividing up a MVC-site into several assemblies (ran
I would like to send big file by dividing it to small parts and
Is dividing the work into 5 functions as opposed to one big function more

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.