-2

I'm new to Android development and it is a basic question.

I have tried to replace IllegalArgumentException with the String value. How to replace this that Android error will not be shown? Need help.

It wil not work:

R.string.msg_log1

getString(R.string.msg_log1)

My code is the following:

          if (myPhone == null || myPhone < 0) {
            throw new IllegalArgumentException("Data requires valid phone");
Anisuzzaman Babla
  • 6,510
  • 7
  • 36
  • 53
user447320
  • 1
  • 1
  • 6
  • What exactly do you want to replace, the `"Data requires valid phone"`? And what does "not work"? – Henry Aug 28 '18 at 11:42
  • Is it in an activity/fragment or adapter..? – Ümañg ßürmån Aug 28 '18 at 11:42
  • `throw new IllegalArgumentException(getString(R.string.msg_log1));` – Shark Aug 28 '18 at 12:18
  • what do you want the code to do instead of throwing exception? – Vladyslav Matviienko Aug 28 '18 at 12:23
  • Throwing my exception is intended report to know all activities which are positive or negative (errors). I thought it can be stored my hard-coded text into strings.xml and not as a Toast message as some proposed. All options were wrong like R.string.msg_log or getString(R.string.msg_log1). See Data requires a valid phone. This is hard-coded text. – user447320 Sep 08 '18 at 20:20

1 Answers1

-1

If you are in an Activity you could use it like this.

if (myPhone == null || myPhone < 0){ 
    Toast.makeText(this, getResources().getString(R.string.msg_log1), Toast.LENGTH_SHORT).show();
}

make sure you add the string with the name msg_log1 to the strings.xml

Yasiru Nayanajith
  • 1,647
  • 17
  • 20
  • My hard corded text is not a good solution. Data requires valid phone should be replaced using Strings.xml file. Toast is not valid due to IllegalArgumentException which should not be passed inside Toast but Logcat. – user447320 Aug 29 '18 at 12:15