Ash
Ash

Reputation: 75

how to setMessage dialog with EditText Android

I want to create dialog for looping dialog and view edittext into dialog.

  1. when activity start, dialog show to looping dialog no.1
  2. dialog looping = no.1 input user
  3. dialog looping to for show input dialog no.3

My error log :

1-29 14:54:23.314: E/AndroidRuntime(1099): FATAL EXCEPTION: main
01-29 14:54:23.314: E/AndroidRuntime(1099): java.lang.NullPointerException
01-29 14:54:23.314: E/AndroidRuntime(1099):     at com.example.dialog_looping.MainActivity$dialog$1.onClick(MainActivity.java:44)
01-29 14:54:23.314: E/AndroidRuntime(1099):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
01-29 14:54:23.314: E/AndroidRuntime(1099):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 14:54:23.314: E/AndroidRuntime(1099):     at android.os.Looper.loop(Looper.java:123)
01-29 14:54:23.314: E/AndroidRuntime(1099):     at android.app.ActivityThread.main(ActivityThread.java:3683)
01-29 14:54:23.314: E/AndroidRuntime(1099):     at java.lang.reflect.Method.invokeNative(Native Method)
01-29 14:54:23.314: E/AndroidRuntime(1099):     at java.lang.reflect.Method.invoke(Method.java:507)
01-29 14:54:23.314: E/AndroidRuntime(1099):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-29 14:54:23.314: E/AndroidRuntime(1099):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-29 14:54:23.314: E/AndroidRuntime(1099):     at dalvik.system.NativeStart.main(Native Method)

My full code of app :

package com.example.dialog_looping;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        setContentView(new dialog(this));
    }
    public class dialog extends View {

        public dialog(Context context) {
            super(context);


            //this.setNama(name.getText().toString());
            LayoutInflater inflater = (MainActivity.this).getLayoutInflater();
            new AlertDialog.Builder(MainActivity.this)
            .setIcon(R.drawable.ic_launcher)
            .setTitle("Input Your Name")
            .setView(inflater.inflate(R.layout.dialoginputname, null))
            .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                EditText name;
                name = (EditText)findViewById(R.id.name);
                //String a = name.setText(name.getText().toString());
                //int z = Integer.valueOf(a);
                   new AlertDialog.Builder(MainActivity.this)
                  .setTitle("YOur Name Is")
                  .setMessage(name.getText().toString())
                  .setNeutralButton("Close", new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dlg, int sumthin) {
                  finish();
                  }})
                  .show();
             }
              })
              .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {

                 finish();
               }
               })
                .show();
                }

        protected CharSequence nama() {
            // TODO Auto-generated method stub
            return null;
        }
            };


    }

Upvotes: 0

Views: 3383

Answers (2)

TN888
TN888

Reputation: 7739

Improved code for you :

    EditText name;
                name = (EditText)findViewById(R.id.name);
                String a = name.getText().toString();
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("something");
alertDialog.setMessage(name);
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
}
});
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();

Try now again. If my answer was helpful, accept its please.

Upvotes: 1

Grambot
Grambot

Reputation: 4524

Change

.setMessage(name())

To

.setMessage(name.getText().toString())

Upvotes: 2

Related Questions