0

I have a DialogFragment on an Activity class. This fragment has an EditText field and all I want to do is check if the field has less than 3 digits on the input then show a toast message. App keeps crashing and I can't even see what exception it is throwing me in the LogCat/Stacktrace.

Activity Class:

public class ParentActivity extends AppCompatActivity{
    public boolean getTextCodeLength(){
        EditText editTextfield = (EditText)findViewById(R.id.textFieldName);
        if(editTextfield.length() < 4)
        {
            return false;
        }
        return true;
    }
}

Fragment Class:

public class EnterTextFragment extends DialogFragment {
    public void onDialogOkClick(DialogInterface dialog) {
     try {
            15. boolean result = ((ParentActivity) getActivity()).getTextCodeLength();
            if (result == false) {
                Toast.makeText(myContext, "Code needs to be longer than 4 digits", Toast.LENGTH_LONG).show();
            }
        }
        catch(Exception ex)
        {
            Log.e("YOUR_APP_LOG_TAG", "I got an error", ex);
        }

    //Perform some other functions
    }
}

Whenever it hits the line marked with number 15 - it keeps crashing and I don't even get what's causing the error because I am unable to see any exceptions in the LogCat as mentioned. Some help would great be appreciated.

More Context: The toast is for testing purposes. Ideally I want to let the user stay on the fragment if their input is less than 4 digits.

ColdFire
  • 6,764
  • 6
  • 35
  • 51
Harold_Finch
  • 682
  • 2
  • 12
  • 33

2 Answers2

1

Why you don't handle getTextCodeLength() inside the fragment? It's wrong to define it inside the activity since it uses fields from fragment and the result is used only inside the fragment. I would is to keep a reference of the field inside the fragment (finding a view in the hierarchy is costly and it shouldn't be done each time when you press the button). So, declare a EditText in the fragment:

private EditText editText;

Override onCreateView to get a reference of the field:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View contentView = inflater.inflate(R.layout.your_fragment_layout, container, false);
    editText = contentView.findViewById(R.id.textFieldName);
    return contentView;
}

Then delete the getTextCodeLength from the activity(also, maybe rename this method since the current name is misleading) and move it inside the fragment:

public boolean getTextCodeLength(){
    if(editText.length() < 4)
    {
        return false;
    }
    return true;
}

Now everything should work smoothly without any crashes.

Iulian Popescu
  • 2,595
  • 4
  • 23
  • 31
0

Move this code to onCreate method of activity:

mEditTextfield = (EditText)findViewById(R.id.textFieldName);

Add field to your activity class:

private EditText mEditTextfield;

Modify your method:

public boolean getTextCodeLength(){
        if(mEditTextfield.length() < LIMIT)
        {
            return false;
        }
        return true;
}

Also check if fragment is attached to activity in onDialogOkClick().

Alexey Zatsepin
  • 1,179
  • 7
  • 14
  • I implemented your changes and included a check: ` if(((ConnectingActivity) getActivity()) == null)` and it crashes again on this line – Harold_Finch Mar 19 '18 at 15:03
  • Also had a look at: https://stackoverflow.com/questions/10919240/fragment-myfragment-not-attached-to-activity but no luck – Harold_Finch Mar 19 '18 at 15:05
  • How do you show dialog? It can be CEE (class cast exception). So you should check if getActivity instanceOf ConnectingActivity – Alexey Zatsepin Mar 19 '18 at 15:12
  • `private ConnectingActivity myActivity;` Then in my `onCreateDialog` hook I instantiate it: `myActivity = (ConnectingActivity) getActivity();` how can I get to see my exceptions? LogCat is not showing me anything – Harold_Finch Mar 19 '18 at 15:21