2013-04-09 135 views
-1

我正在使用對話框從edittext獲取字符串值,但它沒有給出editext的值。如何從Android中的EditText獲取字符串值?

我的代碼:

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

登錄貓錯誤:

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 
+0

什麼是確切的錯誤?你可以在這裏發佈嗎? – blitzen12 2013-04-09 09:58:14

+0

顯示您的logcats – 2013-04-09 09:58:22

+0

顯示您的Logcat @Shiva – 2013-04-09 09:58:35

回答

1

因爲你是從主要佈局發現視圖(EditText上),而不是膨脹佈局。

嘗試:

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

這將使他的對話框只有一個edittext視圖。 – Robin 2013-04-09 10:04:04

+0

@Robin爲什麼?充滿search_dialog佈局,以及它如何成爲只有EditText的對話框? – 2013-04-09 10:05:47

+0

對不起,我的錯誤,我認爲我今天審查了太多的代碼... – Robin 2013-04-09 10:07:29

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 
      } 
     } 
    } 
+0

不,它不起作用 – Shweta 2013-04-09 10:04:38

+0

奇怪,任何輸出? – Robin 2013-04-09 10:09:10

+0

錯誤行SearchText =(EditText)dialog.findViewById(R.id.search_text).getText()。toString(); – Shweta 2013-04-09 10:14:28

相關問題