对话框中的EditText返回空字符串

时间:2015-09-29 05:56:02

标签: android android-edittext android-dialog

我有button来显示dialog,而EditText又有一个done供用户编写内容,然后点按button按钮以保存另一个dialog关闭String。这里的问题是我无法获取用户编写的dialog。它会返回一个null sting而导致应用程序崩溃。这是代码:

我点击按钮时调用 //Write Review Dialog Box writeReviewBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i("HAPPENED", "This Onclick is working"); try{ // Create custom dialog object final Dialog dialog = new Dialog(GetReviewActivity.this); // Include dialog's xml file dialog.setContentView(R.layout.write_review_activity); // Set dialog title dialog.setTitle("Your review is valuable"); final EditText etWR = (EditText)findViewById(R.id.etWR); Button writeButton = (Button) dialog.findViewById(R.id.buttonWR); Button declineButton = (Button) dialog.findViewById(R.id.buttonNoThanks); dialog.show(); //Done Button writeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i("HAPPENED: ","writeButton Block Is WORKING!"); Activity activity = GetReviewActivity.this; activity.runOnUiThread(new Runnable() { @Override public void run() { String Review = etWR.getText().toString(); StoreAReview(Review); } }); } }); // if decline button is clicked, close the custom dialog declineButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Close dialog dialog.dismiss(); } }); }catch (Exception e){ e.printStackTrace(); } } });

com.sun.jndi.ldap.connect.pool.timeout

5 个答案:

答案 0 :(得分:4)

final EditText etWR = (EditText)findViewById(R.id.etWR);正在解析为Activity#findViewById。但是您的对话框布局未附加到您的活动窗口,它已附加到对话框中。所以你必须从对话框的视图中膨胀EditText

尝试

final EditText etWR = (EditText)dialog.findViewById(R.id.etWR);

而不是

final EditText etWR = (EditText)findViewById(R.id.etWR);

答案 1 :(得分:3)

以前经历过这个问题。我尝试使用data: [ {firstName: 'Tom',value:2}, {firstName: 'Ed',value:4}, {firstName: 'Jam',value:6}, {firstName: 'Aar',value:8}, {firstName: 'Dav',value:10}, {firstName: 'Mic',value:12} ] 引用启动EditText,但这不起作用

Dialog

<强>解决方案:

这就是我解决它的方法:

final EditText etWR = (EditText)dialog.findViewById(R.id.etWR);

答案 2 :(得分:2)

您应该初始化EditText

 EditText etWR = (EditText)dailog.findViewById(R.id.etWR);

becoz EditText来自dailog。所以

答案 3 :(得分:0)

你错过了 dialog.findViewById(R.id.etWR)

更改此行

 final EditText etWR = (EditText)findViewById(R.id.etWR);

到这个

final EditText etWR = (EditText)dialog.findViewById(R.id.etWR);

然后,添加点击事件代码

答案 4 :(得分:0)

请参阅以下示例,这是一个可行的工作。

Main.java

public class MainActivity extends Activity {

    final Context context = this;
    private Button button;
    private EditText result;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // components from main.xml
        button = (Button) findViewById(R.id.buttonPrompt);
        result = (EditText) findViewById(R.id.editTextResult);

        // add button listener
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // get prompts.xml view
                LayoutInflater li = LayoutInflater.from(context);
                View promptsView = li.inflate(R.layout.prompts, null);

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        context);

                // set prompts.xml to alertdialog builder
                alertDialogBuilder.setView(promptsView);

                final EditText userInput = (EditText) promptsView
                        .findViewById(R.id.editTextDialogUserInput);

                // set dialog message
                alertDialogBuilder
                        .setCancelable(false)
                        .setPositiveButton("OK",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        // get user input and set it to result
                                        // edit text
                                        result.setText(userInput.getText());
                                    }
                                })
                        .setNegativeButton("Cancel",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        dialog.cancel();
                                    }
                                });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();

            }

        });

    }

}

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/buttonPrompt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Prompt Dialog" />

    <EditText
        android:id="@+id/editTextResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

    </EditText>

</LinearLayout>

promt.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Type Your Message : "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextDialogUserInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>

</LinearLayout>

就是这样。