android textViewEvent返回空值

ia2d9nvy  于 2022-11-27  发布在  Android
关注(0)|答案(2)|浏览(147)

我在从一个可编辑的字段中提取.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();
        }
    });

请任何人?

xjreopfe

xjreopfe1#

//Copy and paste this code it works for you(Remove view from it)    

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) findViewById(R.id.txtEvent);
                EditText textViewComment = (EditText) findViewById(R.id.txtComment);
                EditText textViewDateAdded = (EditText) findViewById(R.id.txtDateTime);
                TextView textViewDateAccessed = (TextView) 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();
            }
        });
ogsagwnx

ogsagwnx2#

//try textutils for null validation and don't cast edit text.

    if (TextUtils.isEmpty(string){
//String is null
}else{
//String not null
}

相关问题