Home > other >  textViewEvent returns null
textViewEvent returns null

Time:11-24

I am having problems extracting .getText().toString() from an editable field. The layout-file (activity_main.xml) is in perfect shape. The code compiles and runs, but if you see the if-sentences they return null without the != null.

This is the code:

    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();
        }
    });

Please anyone?

CodePudding user response:

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

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

CodePudding user response:

//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();
            }
        });
  • Related