-1

我正在使用一个对话框从 获取字符串值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();

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

记录猫错误:

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
4

2 回答 2

1

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

尝试:

 View searchView= inflater.inflate(R.layout.search_dialog, null);
 builder.setView(searchView);
 Searchtxt = (EditText) searchView.findViewById(R.id.search_text);
于 2013-04-09T10:00:06.617 回答
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 
            }
        }
    }
于 2013-04-09T09:59:32.523 回答