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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:14:59+00:00 2026-05-15T07:14:59+00:00

I am overriding the onCreateDialog and onPrepareDialog methods or the Dialog class. I have

  • 0

I am overriding the onCreateDialog and onPrepareDialog methods or the Dialog class.

I have followed the example from Reto Meier’s Professional Android Application Development book, Chapter 5 to pull some XML data and then use a dialog to display the info.

I have basically followed it exactly but changed the variables to suit my own XML schema as follows:


@Override
public Dialog onCreateDialog(int id) {
  switch(id) {
    case (SETTINGS_DIALOG) :        
      LayoutInflater li = LayoutInflater.from(this);
      View settingsDetailsView = li.inflate(R.layout.details, null);

      AlertDialog.Builder settingsDialog = new AlertDialog.Builder(this);
      settingsDialog.setTitle("Provisioned Settings");         
      settingsDialog.setView(settingsDetailsView);
return settingsDialog.create();
  }
  return null;
}

@Override
public void onPrepareDialog(int id, Dialog dialog) {
  switch(id) {
    case (SETTINGS_DIALOG) :                  

String afpunText = "  ";

     if(setting.getAddForPublicUserNames() == 1){
      afpunText = "Yes";
     }
     else{
      afpunText = "No";
     }
      String Text = "Login Settings: " + "\n" 
                       + "Password: " + setting.getPassword()  + "\n" 
                       + "Server: " + setting.getServerAddress() + "\n";


      AlertDialog settingsDialog = (AlertDialog)dialog; 
settingsDialog.setTitle(setting.getUserName());

tv = (TextView)settingsDialog.findViewById(R.id.detailsTextView);
if (tv != null)
        tv.setText(Text);

      break;
  }
}

It works fine until I try changing the screen orientation, When I do this onPrepareDialog gets call but I get null pointer exceptions on all my variables.

The error still occurs even when I tell my activity to ignore screen orientation in the manifest.

So I presume something has been left out of the example in the book do I need to override another method to save my variables in or something?

I have now added the following:


@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  // Save UI state changes to the savedInstanceState.
  // This bundle will be passed to onCreate if the process is
  // killed and restarted.
  savedInstanceState.putString("Username", setting.getUserName());
  savedInstanceState.putString("Password", setting.getPassword());
  savedInstanceState.putString("Server", setting.getServerAddress());

  super.onSaveInstanceState(savedInstanceState);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  settings.clear();
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  username = savedInstanceState.getString("Username");
  password = savedInstanceState.getString("Password");
  serveraddress = savedInstanceState.getString("Server");


Settings setting = new Settings(username, password, serveraddress);
addNewSettings(setting);

}

But I’m still getting the Null Pointer exception

  • 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-15T07:15:00+00:00Added an answer on May 15, 2026 at 7:15 am

    I don’t have Reto’s book so I can’t check the code, but if there is a mistake in the book you can contact him easily on Twitter to discuss it: http://twitter.com/retomeier

    Remember that if the screen orientation changes, your activity is recreated. That means that any variables you set the last time around, which are not static, will be lost after the activity is recreated under the new orientation.

    My guess is that you hit a NullPointerException here:

    if(setting.getAddForPublicUserNames() == 1){
    

    If so, it is most likely that some user action changes the “setting” variable to something other than null – you need to make sure this is set again when the activity is recreated.

    The most standard method to store/retrieve the activity state between orientations is detailed here: Saving Android Activity state using Save Instance State

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

Sidebar

Related Questions

I understand overriding a method/function redefines its implementation in the derived class from its
When overriding the baseadapter on an android listview, you have to implement this method
I have an overriding Object class (Guide) with a subclass (Session). public class Guide
Overriding Methods in java have the following features : 1> The Overrinding method should
I've read the Overriding and Hiding Methods tutorial. And from that, I gathered the
When overriding a class in C++ (with a virtual destructor) I am implementing the
So I'm overriding OnActionExecuted in my BaseController class to set the CurrentUser property of
I'm overriding Android's ImageView in order to make the corners of my image transparent.
I created an overriding formatter which masks some strings like this: class MaskFormatter(logging.Formatter): def
Normal overriding would work this way: class Fruit { public: string color(); }; string

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.