在String中存储Edittext的值?

时间:2016-02-28 17:20:54

标签: java android

我试图获取edittext的值并将其放入toast中。 显然这行代码存在问题,因为当我点击时,应用程序崩溃了。

String newItem = et_getItem.getText().toString();

编辑:这是全班:

public class MainActivity extends AppCompatActivity {

TextView txt_array;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(myToolbar);

    txt_array = (TextView) findViewById(R.id.txt_array);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.toolbar_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.action_add) {

        openDialog();

        return true;
    }

    return super.onOptionsItemSelected(item);
}

private void openDialog() {

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

    LayoutInflater inflater = MainActivity.this.getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    View theInflatedView = inflater.inflate(R.layout.dialog_add, null);
    builder.setView(theInflatedView);


    builder.setPositiveButton("Okay!", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            EditText et_getItem = (EditText) findViewById(R.id.et_add);
            String newItem = et_getItem.getText().toString();

            Toast.makeText(getApplicationContext(), "The item " + newItem + " has been added!", Toast.LENGTH_SHORT).show();
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });



    //AFTER setting the properties, create the dialog!
    AlertDialog dialog = builder.create();

    //AFTER creating the dialog, don't forget to show it!
    dialog.show();
}

}

这是dialog_add.xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<EditText
    android:id="@+id/et_add"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginTop="16dp"
    android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    android:hint="Type in a list item..."
    android:inputType="textCapWords" />
</LinearLayout>

logcat的:

02-28 18:36:29.156 5913-5913/com.smaragd16.mylist E/AndroidRuntime: FATAL EXCEPTION: main
                                                                Process: com.smaragd16.mylist, PID: 5913
                                                                java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                                                                    at com.smaragd16.mylist.MainActivity$1.onClick(MainActivity.java:71)
                                                                    at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                    at android.os.Looper.loop(Looper.java:148)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5525)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

1 个答案:

答案 0 :(得分:0)

试试这个:

View theInflatedView = inflater.inflate(R.layout.dialog_add, null);
builder.setView(theInflatedView);

而不是

builder.setView(inflater.inflate(R.layout.dialog_add, null));

此外,您尝试使用错误的引用初始化edittext,因为它已在对话框的布局中声明,您应该将其作为

EditText et_getItem = (EditText) dialog.findViewById(R.id.et_add);

而不是

EditText et_getItem = (EditText) findViewById(R.id.et_add);

我希望它适合你。