从AlertDialog中的EditText获取值

时间:2014-07-17 17:53:59

标签: android alertdialog

我正在尝试生成随机整数,以“min”开头,以“max”结尾,我在Alert对话框中输入。然后我需要进入另一个EditText eROut。

import java.util.Random;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class RandomClass extends Activity {
    public EditText eMin,eMax,eROut;

    int min,max;
    private DialogInterface.OnClickListener listener = new  DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getApplicationContext(), "Canceled", Toast.LENGTH_SHORT).show(); 
            // TODO Auto-generated method stub

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.random_activity);
        eROut = (EditText) findViewById(R.id.eROut);
        eMax = (EditText) findViewById(R.id.eMax);
        eMin = (EditText) findViewById(R.id.eMin);

    }


    public void onClick(View v){


        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(RandomClass.this);
        LayoutInflater inflater = RandomClass.this.getLayoutInflater(); 
            builder.setView(inflater.inflate(R.layout.int_layout, null))
                   .setTitle("Generate integer")
                   .setPositiveButton("Generate!", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        min = Integer.parseInt(eMin.getText().toString());
                        max = Integer.parseInt(eMax.getText().toString());
                        eROut.setText(Integer.toString(RandInt(min, max)));



                        // TODO Auto-generated method stub

                    }
                })
                .setNegativeButton("Cancel", listener);

            dialog = builder.create();

            dialog.show();






    }
    public static int RandInt(int n, int x){
        Random rand = new Random();
        int randInt  = rand.nextInt((x-n)+1)+n;
        return randInt;

    }
}

但是点击“生成”后程序会出现故障。 我的问题在哪里?

Logcat

07-17 21:06:25.290: I/ActivityManager(508): START {cmp=com.example.translit/.RandomClass (has extras)} from pid 26851
07-17 21:06:25.390: I/ActivityManager(508): Displayed com.example.translit/.RandomClass: +76ms
07-17 21:06:25.390: D/OpenGLRenderer(26851): Flushing caches (mode 0)
07-17 21:06:26.570: W/ActivityManager(508): Activity destroy timeout for ActivityRecord{415a0ef8 com.example.translit/.StartClass}
07-17 21:06:28.640: D/dalvikvm(26851): GC_CONCURRENT freed 142K, 4% free 6853K/7111K, paused 4ms+4ms
07-17 21:06:35.400: D/AndroidRuntime(26851): Shutting down VM
07-17 21:06:35.400: W/dalvikvm(26851): threadid=1: thread exiting with uncaught exception (group=0x40a511f8)
07-17 21:06:35.410: E/AndroidRuntime(26851): FATAL EXCEPTION: main
07-17 21:06:35.410: E/AndroidRuntime(26851): java.lang.NullPointerException
07-17 21:06:35.410: E/AndroidRuntime(26851):    at com.example.translit.RandomClass$3.onClick(RandomClass.java:89)
07-17 21:06:35.410: E/AndroidRuntime(26851):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
07-17 21:06:35.410: E/AndroidRuntime(26851):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 21:06:35.410: E/AndroidRuntime(26851):    at android.os.Looper.loop(Looper.java:137)
07-17 21:06:35.410: E/AndroidRuntime(26851):    at android.app.ActivityThread.main(ActivityThread.java:4424)
07-17 21:06:35.410: E/AndroidRuntime(26851):    at java.lang.reflect.Method.invokeNative(Native Method)
07-17 21:06:35.410: E/AndroidRuntime(26851):    at java.lang.reflect.Method.invoke(Method.java:511)
07-17 21:06:35.410: E/AndroidRuntime(26851):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-17 21:06:35.410: E/AndroidRuntime(26851):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-17 21:06:35.410: E/AndroidRuntime(26851):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

你应该从膨胀的布局中找到View而不是从内容视图中找到。

    AlertDialog dialog;
    AlertDialog.Builder builder = new AlertDialog.Builder(RandomClass.this);
    LayoutInflater inflater = RandomClass.this.getLayoutInflater(); 
    View layout = inflater.inflate(R.layout.int_layout, null);

    eMax = (EditText) layout.findViewById(R.id.eMax);
    eMin = (EditText) layout.findViewById(R.id.eMin);


        builder.setView(layout)
               .setTitle("Generate integer")
               .setPositiveButton("Generate!", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    min = Integer.parseInt(eMin.getText().toString());
                    max = Integer.parseInt(eMax.getText().toString());
                    eROut.setText(Integer.toString(RandInt(min, max)));



                    // TODO Auto-generated method stub

                }
            })
            .setNegativeButton("Cancel", listener);

        dialog = builder.create();

        dialog.show();