如何从Android中的EditText获取String值?

时间:2013-04-09 09:56:19

标签: android

我正在使用对话框从edittext获取字符串值,但它没有给出editext的值。

我的代码:

 protected Dialog onCreateDialog(int id)
    {
        switch (id) 
        {
            case DIALOG_ALERT:
                // Create out AlterDialog

                android.app.AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("Search");
                builder.setCancelable(true);
                LayoutInflater inflater = this.getLayoutInflater();
                builder.setView(inflater.inflate(R.layout.search_dialog, null));
                Searchtxt = (EditText)findViewById(R.id.search_text);
                builder.setPositiveButton("Search", new OkOnClickListener());
                builder.setNegativeButton("Cancel", new CancelOnClickListener());
                AlertDialog dialog = builder.create();
                dialog.show();
        }
                return super.onCreateDialog(id);
    }
    private final class OkOnClickListener implements DialogInterface.OnClickListener
    {
        public void onClick(DialogInterface dialog, int which) 
        {   
            //Implement search method here
            SearchText = Searchtxt.getText().toString();
            System.out.println(SearchText);
        }
    }
    private final class CancelOnClickListener implements DialogInterface.OnClickListener 
    {
        public void onClick(DialogInterface dialog, int which) 
        {
            dialog.dismiss();
        }
    }

但它在行SearchText = Searchtxt.getText().toString();中给出错误。

我需要通过Intent进一步传递字符串searchText

记录cat错误:

04-09 15:18:55.634: E/AndroidRuntime(1847): FATAL EXCEPTION: main
04-09 15:18:55.634: E/AndroidRuntime(1847): java.lang.NullPointerException
04-09 15:18:55.634: E/AndroidRuntime(1847):     at com.web.dms.HomePage$OkOnClickListener.onClick(HomePage.java:155)
04-09 15:18:55.634: E/AndroidRuntime(1847):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
04-09 15:18:55.634: E/AndroidRuntime(1847):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 15:18:55.634: E/AndroidRuntime(1847):     at android.os.Looper.loop(Looper.java:137)
04-09 15:18:55.634: E/AndroidRuntime(1847):     at android.app.ActivityThread.main(ActivityThread.java:4340)
04-09 15:18:55.634: E/AndroidRuntime(1847):     at java.lang.reflect.Method.invokeNative(Native Method)
04-09 15:18:55.634: E/AndroidRuntime(1847):     at java.lang.reflect.Method.invoke(Method.java:511)
04-09 15:18:55.634: E/AndroidRuntime(1847):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-09 15:18:55.634: E/AndroidRuntime(1847):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-09 15:18:55.634: E/AndroidRuntime(1847):     at dalvik.system.NativeStart.main(Native Method)
04-09 15:18:57.044: I/Process(1847): Sending signal. PID: 1847 SIG: 9

2 个答案:

答案 0 :(得分:1)

因为您正在从主布局中找到视图(EditText)而不是膨胀布局。

尝试:

 View searchView= inflater.inflate(R.layout.search_dialog, null);
 builder.setView(searchView);
 Searchtxt = (EditText) searchView.findViewById(R.id.search_text);

答案 1 :(得分:0)

在你的

private final class OkOnClickListener implements DialogInterface.OnClickListener

尝试使用

private final class OkOnClickListener implements DialogInterface.OnClickListener
    {
        public void onClick(DialogInterface dialog, int which) 
        {   
            if(DialogInterface.BUTTON_POSITIVE == which) {
                SearchText = ((EditText)dialog.findViewById(R.id.search_text)).getText().toString();
                System.out.println(SearchText);
                //Do your searching work 
            }
        }
    }