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