我想獲得edittext的值並將其放入烤麪包。 顯然這行代碼有問題,因爲應用程序崩潰時,我打好了。將Edittext的值存儲在字符串中?
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)
Logcat,請問? –