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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:11:21+00:00 2026-05-21T09:11:21+00:00

In the below given function i am trying to create an alertDialog box with

  • 0

In the below given function i am trying to create an alertDialog box with username and password field and Insert the values of the fields into the shared preferences.But the app crashes after i press the ok button in alert dialog box after entering the fields

I am not able to access the variables inside the alertDialog box positive button click listener inside the protected Dialog onCreateDialog(int id).When I inspect the variables
prefs,username and password it shows me this.
Unable to retrieve correct enclosing instance of this. Can someone tell me what is the reason for this

       import android.app.Activity;
        import android.app.AlertDialog;
        import android.app.Dialog;
        import android.content.Context;
        import android.content.DialogInterface;
        import android.content.DialogInterface.OnClickListener;
        import android.content.SharedPreferences;
        import android.os.Bundle;
        import android.preference.PreferenceManager;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.widget.EditText;

        public class Login extends Activity {

            public static String PREF_LOGIN_USERNAME = "pref_username";
            public static String PREF_LOGIN_PASSWORD = "pref_password";

            public static final int DIALOG_LOGIN = 100;
            public static final int DIALOG_NEW_PASSWORD = 101;


            LayoutInflater factory;
            View loginView;

            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                showDialog(DIALOG_LOGIN);

            }

            protected Dialog onCreateDialog(int id) {
                switch (id) {

                case DIALOG_LOGIN:
                    // Inflating the View from the xml
                    factory = LayoutInflater.from(Login.this);
                    loginView = factory.inflate(R.layout.alert_dialog_text_entry, null);
                    return new AlertDialog.Builder(this)
                            .setTitle(R.string.alert_dialog_login)
                            .setView(loginView)
                            .setPositiveButton(R.string.dialog_ok,
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,
                                                int whichButton) {
                       Context context = getApplicationContext();
                            SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(context);
                                    SharedPreferences.Editor editor = prefs.edit();

                                    EditText username = (EditText)loginView.findViewById(R.id.username_edit);
                                    EditText password = (EditText)loginView.findViewById(R.id.password_edit);
                                    editor.putString(PREF_LOGIN_USERNAME,
                                            username.getText().toString());
                                    editor.putString(PREF_LOGIN_PASSWORD,
                                            password.getText().toString());
                                    editor.commit();
                                    editor.commit();


                                        }
                                    })
                            .setNegativeButton(R.string.dialog_cancel,
                                    new OnClickListener() {

                                        @Override
                                        public void onClick(DialogInterface dialog,
                                                int which) {
                                            // TODO Auto-generated method stub
                                            showDialog(DIALOG_LOGIN);
                                        }
                                    }).create();
    }
    return null;
        }
    }

xml file

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2008 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView 
        android:id="@+id/username_view"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:text="@string/alert_dialog_username"
        android:gravity="left"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/username_edit"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:scrollHorizontally="true"
        android:autoText="false"
        android:capitalize="none"
        android:gravity="fill_horizontal"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/password_view"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:text="@string/alert_dialog_password"
        android:gravity="left"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/password_edit"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:scrollHorizontally="true"
        android:autoText="false"
        android:capitalize="none"
        android:gravity="fill_horizontal"
        android:password="true"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

The Error log from ddms:

04-14 21:43:32.870: ERROR/AndroidRuntime(16125): java.lang.NullPointerException
04-14 21:43:32.870: 
ERROR/AndroidRuntime(16125):     at com.qrcoder.Login$1.onClick(Login.java:66)
04-14 21:43:32.870: ERROR/AndroidRuntime(16125):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
04-14 21:43:32.870: ERROR/AndroidRuntime(16125):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 21:43:32.870: ERROR/AndroidRuntime(16125):     at android.os.Looper.loop(Looper.java:123)
04-14 21:43:32.870: ERROR/AndroidRuntime(16125):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-14 21:43:32.870: ERROR/AndroidRuntime(16125):     at java.lang.reflect.Method.invokeNative(Native Method)
04-14 21:43:32.870: ERROR/AndroidRuntime(16125):     at java.lang.reflect.Method.invoke(Method.java:521)
04-14 21:43:32.870: ERROR/AndroidRuntime(16125):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
04-14 21:43:32.870: ERROR/AndroidRuntime(16125):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
04-14 21:43:32.870: ERROR/AndroidRuntime(16125):     at dalvik.system.NativeStart.main(Native Method)

Edit:This is the working code.

  • 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-21T09:11:22+00:00Added an answer on May 21, 2026 at 9:11 am

    I’m not sure what’s going on, but I’d suggest moving the call to showDialog out of onCreate. It’s probably better to do it in onResume, when the UI is actually on the screen.

    EDIT Okay, I think I see what’s going on. You are trying to initialize the fields username and password in the onCreate method for your activity, but I’m guessing that these fields only exist in the login dialog. You need to find the fields after you construct the dialog itself, since they don’t exist before that.

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

Sidebar

Related Questions

I am trying to create a function that will block access to some of
I am trying to create a function which will give me alphabet position when
I am trying to create an expression tree containing a function call to a
we are trying to create a calendar function in python. we have created a
Given the one-table design given below how would the following best be queried The
I have the HTML given below: <ul id=thumbsPhotos> <li src=/images/1alvaston-hall-relaxing-lg.jpg onclick=updatePhoto (this.title)><img src=/images/1alvaston-hall-relaxing-sl.jpg width=56
I have a xml given below: <root title=الصفحة الرئيسة> <item title=الصفحة الرئيسة itemuri=tcm:8-29-4 ShowInNav=True
I tried the following code in LINQPad and got the results given below: List<string>
Given the Below Tables. How do I get the Distinct name given the other
Given a string as below, I need to convert: 1 Dec 2008 06:43:00 +0100

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.