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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:47:46+00:00 2026-06-04T15:47:46+00:00

I have a custom dialog with an EditText and Button in it. In my

  • 0

I have a custom dialog with an EditText and Button in it. In my dialog neutral button’s onClick method, I want to make a new string from what has been typed into the EditText.

What I am doing is inflating a menu when the menu button is pressed. The menu buttons show AlertDialogs with 2 options. When an option is selected in the AlertDialog, another one is created which has an EditText in it asking for a name. The null pointer error comes when I attempt “String name = nameOfEditText.getText().toString();”

here is the code

    // creation of the dialog
    Builder mBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater)                 this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout= inflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.RelativeLayout1));

mBuilder.setNeutralButton("Add", new AddOnClickListener());
mBuilder.setView(layout);
Dialog mDialog = mBuilder.create();
mDialog.show();

    nameOfLocalEditText = (EditText) findViewById(R.id.name);

    Button dialogButton = (Button) mDialog.findViewById(R.id.button1);
dialogButton.setOnClickListener(new OnClickListener() {
        @Override
    public void onClick(View v) {
            Intent imp = new Intent();
            imp.setType("file/*");
        imp.setAction(Intent.ACTION_GET_CONTENT);
        imp.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(imp, 0);
            }
});

Now here is the AddOnClickListener() method. nameOfLocalEditText is an EditText local to the class containing these methods.

    private final class AddOnClickListener implements DialogInterface.OnClickListener {
    public void onClick(DialogInterface dialog, int which) {
        String s = nameOfLocalEditText.getText().toString();
    }
}

Can anyone tell me why my app force closes at the point of making the String s?

This is the xml for custom_dialog

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RelativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="16dp"
            android:text="Get File" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button1"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="18dp"
            android:ems="10" >

        <requestFocus />
        </EditText>
    </RelativeLayout>

This is the logcat error message

   05-24 23:27:27.166: E/AndroidRuntime(595): FATAL EXCEPTION: main
   05-24 23:27:27.166: E/AndroidRuntime(595): java.lang.NullPointerException
   05-24 23:27:27.166: E/AndroidRuntime(595): at         com.akonwi.listSyllabi.ListSyllabiActivity$AddOnClickListener.onClick(ListSyllabiActivity.ja va:145)
   05-24 23:27:27.166: E/AndroidRuntime(595):   at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
   05-24 23:27:27.166: E/AndroidRuntime(595):   at android.os.Handler.dispatchMessage(Handler.java:99)
   05-24 23:27:27.166: E/AndroidRuntime(595):   at android.os.Looper.loop(Looper.java:137)
   05-24 23:27:27.166: E/AndroidRuntime(595):   at android.app.ActivityThread.main(ActivityThread.java:4424)
   05-24 23:27:27.166: E/AndroidRuntime(595):   at java.lang.reflect.Method.invokeNative(Native Method)
   05-24 23:27:27.166: E/AndroidRuntime(595):   at java.lang.reflect.Method.invoke(Method.java:511)
   05-24 23:27:27.166: E/AndroidRuntime(595):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
   05-24 23:27:27.166: E/AndroidRuntime(595):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
   05-24 23:27:27.166: E/AndroidRuntime(595):   at dalvik.system.NativeStart.main(Native Method)
  • 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-04T15:47:48+00:00Added an answer on June 4, 2026 at 3:47 pm

    The same [this question][1]

    Try this

      nameOfLocalEditText = (EditText)layout.findViewById(R.id.name);
      layout.addView(nameOfLocalEditText);
    

    Update: refer to [this link][2] you can try

      final Dialog dialog = new Dialog(context);
      dialog.setContentView(R.layout.custom);
    

    Use dialog.setContentView instead of inflate layout should work.

    Right now my preference is to use the AlertDialog so I can have the
    neutral button. I already have a button in my custom dialog to send
    the user to find a file. I want the neutral button to take the data
    from the AlertDialog components

    This code worked for me:

     // creation of the dialog
        Builder mBuilder = new AlertDialog.Builder(this);
        LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.custom_dialog,
                (ViewGroup) findViewById(R.id.RelativeLayout1));
    
        mBuilder.setNeutralButton("Add", new AddOnClickListener());
        mBuilder.setView(layout);
        Dialog mDialog = mBuilder.create();
        mDialog.show();
    
        nameOfLocalEditText = (EditText) layout.findViewById(R.id.editText1);
    
        Button dialogButton = (Button) layout.findViewById(R.id.button1);
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent imp = new Intent();
                imp.setType("file/*");
                imp.setAction(Intent.ACTION_GET_CONTENT);
                imp.addCategory(Intent.CATEGORY_OPENABLE);
                startActivityForResult(imp, 0);
            }
        });
    
       private final class AddOnClickListener implements DialogInterface.OnClickListener {
        public void onClick(DialogInterface dialog, int which) {
            String s = nameOfLocalEditText.getText().toString();
            Log.d(TAG, s);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written code for onClick method for Custom Dialog top of the another
I have a Custom Dialog that has an EditText at the bottom of the
I have a custom dialog with one editText view and two buttons ok and
I have a custom dialog in my android application. I want to have in
I want to add a custom Dialog, but I have problems creating a xml
I have a custom dialog in my app that descends from Activity and uses
I have some custom made dialog that have on it Set Button , I
I have ListActivity , onClick of each item a Custom Dialog appears. Custom Dialog
I have a custom dialog with a button and an input on it. On
I have a custom dialog box that collects two strings from the user. 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.