我在从一个可编辑的字段中提取.getText().toString()时遇到了问题。布局文件(activity_main.xml)的形状很完美。代码可以编译并运行,但是如果你看到if语句,它们会返回空值,而没有!= null。
这是代码:
saveButton = (Button) findViewById(R.id.buttonSave);
saveButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View view) {
String eventName, eventId, eventComment, eventDateAdded, eventDateAccessed = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd:HH:mm:ss:SSS");
String date = sdf.format(new Date());
EditText textViewEvent = (EditText) view.findViewById(R.id.txtEvent);
EditText textViewComment = (EditText) view.findViewById(R.id.txtComment);
EditText textViewDateAdded = (EditText) view.findViewById(R.id.txtDateTime);
TextView textViewDateAccessed = (TextView) view.findViewById(R.id.date_accessed);
if (textViewEvent != null) eventName = textViewEvent.getText().toString(); else eventName = "";
if (textViewComment != null) eventComment = textViewComment.getText().toString(); else eventComment = "";
if (textViewDateAdded != null) eventDateAdded = textViewDateAdded.getText().toString(); else eventDateAdded = date;
if (textViewDateAccessed != null) eventDateAccessed = textViewDateAccessed.getText().toString(); else eventDateAccessed = date;
Toast toast = Toast.makeText(getApplicationContext(), eventName + ":" + eventComment +
":" + eventDateAdded, 5);
toast.show();
database.insertNewEventDetails(eventName, eventComment, eventDateAdded, eventDateAccessed);
refreshData();
}
});
请任何人?
2条答案
按热度按时间xjreopfe1#
ogsagwnx2#