我正在使用對話框從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
什麼是確切的錯誤?你可以在這裏發佈嗎? – blitzen12 2013-04-09 09:58:14
顯示您的logcats – 2013-04-09 09:58:22
顯示您的Logcat @Shiva – 2013-04-09 09:58:35