I am trying to convert string value into int but it give me error:
android.content.res.resources$notfoundexception string resource id #0x16 <
I tried parsing like:
String date = "29-30-2098";
String[] d = date.split("-");
int di = Integer.parseInt(d[1]);
Toast.makeText(MainActivity.this, di, Toast.LENGTH_SHORT).show();
if i give String data type to "di" then it print the correct value. But when i try to parse it to Int it give the error
And i also tried by using ValueOf(); but in all methods it give me error.
Error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myanxietyjournal, PID: 8830
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myanxietyjournal/com.myanxietyjournal.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x1e
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1e
at android.content.res.HwResources.getText(HwResources.java:442)
at android.widget.Toast.makeText(Toast.java:307)
at com.myanxietyjournal.MainActivity.onCreate(MainActivity.java:48)
at android.app.Activity.performCreate(Activity.java:6915)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
There are two versions of makeText:
makeText(Context context, CharSequence text, int duration)
makeText(Context context, int resId, int duration)
The first one takes a String as the second parameter, and shows the message in the string to the user.
The second one takes a resource id as the second parameter, tries to lookup the resource, and fails because you don't have a resource with that id.
To show the int value to the user, you need to convert it to string first, e.g. String.valueOf(di).
Related
firstNumber and secondNumber are of type EditText. I am trying to get the values from firstNumber and secondNumber and convert them to type float, but when I am doing that I am getting an Exception java.lang.NumberFormatException, I am unable to figure the solution to the problem. Kindly, please help.
Here's the code
firstNumber = (EditText)findViewById(R.id.firstNumber);
secondNumber = (EditText)findViewById(R.id.secondNumber);
result = (TextView)findViewById(R.id.result);
s1 = firstNumber.getText().toString();
s2 = secondNumber.getText().toString();
num1 = Float.parseFloat(s1);
num2 = Float.parseFloat(s2);
Logcat I am getting is -
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.calcmpttos, PID: 17846
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calcmpttos/com.example.calcmpttos.MainActivity}: java.lang.NumberFormatException: For input string: "Enter First Number"
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NumberFormatException: For input string: "Enter First Number"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
at java.lang.Float.parseFloat(Float.java:451)
at com.example.calcmpttos.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-07-07 20:29:53.693 17846-17846/com.example.calcmpttos I/Process: Sending signal. PID: 17846 SIG: 9
Seems like your error is:
Caused by: java.lang.NumberFormatException: For input string: "Enter First Number"
Make sure you don't use the function with anything but a number.
So if you are using edit text make sure there are only numbers
You have to restrict your editText to have numerical values only. Add this to the editTexts where you wish to add numerical values
android:inputType="number"
or
android:inputType="numberDecimal
You can also do this by using
firstNumber.setInputType(InputType.TYPE_CLASS_NUMBER);
You cant just throw a string that does not contain a parsable Float to parseFloat and expect it to parse it, That's why you got NumberFormatException.
Try to use Float.valueOf()
float num1 = Float.valueOf(firstNumber.getText().toString());
float num2 = Float.valueOf(secondNumber.getText().toString());
and add
android:inputType="numberDecimal
to your EditText's.
You can set digits to bypass the error:
firstNumber.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
secondNumber.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
So only numbers can be typed.
code :
public void extractPhoneNumber(String input){
Iterator<PhoneNumberMatch> existsPhone= PhoneNumberUtil.getInstance().findNumbers(input, "IN").iterator();
while (existsPhone.hasNext()){
System.out.println("Phone == " + existsPhone.next().number());
Log.d("existsPhone",":"+existsPhone.next().rawString());
gotPhone.setText(existsPhone.next().rawString());
}
}
Log :
2019-06-11 16:23:43.059 11176-11176/com.example.cardscaning E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cardscaning, PID: 11176
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cardscaning/com.example.cardscaning.Activity.ProcessImage}: java.util.NoSuchElementException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2678)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
Caused by: java.util.NoSuchElementException
at com.google.i18n.phonenumbers.PhoneNumberMatcher.next(PhoneNumberMatcher.java:710)
at com.google.i18n.phonenumbers.PhoneNumberMatcher.next(PhoneNumberMatcher.java:43)
at com.example.cardscaning.Activity.ProcessImage.extractPhoneNumber(ProcessImage.java:291)
at com.example.cardscaning.Activity.ProcessImage.onCreate(ProcessImage.java:97)
at android.app.Activity.performCreate(Activity.java:6687)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2631)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
Inside log I can get desire result but exception occurs when I am adding this line gotPhone.setText(existsPhone.next().rawString())
Desirable outcome is able use extracted number.
You are calling next() thrice in one iteration forcing the Iterator to move to an element that doesn't exist.
Instead of:
while (existsPhone.hasNext()){
System.out.println("Phone == " + existsPhone.next().number());
Log.d("existsPhone",":"+existsPhone.next().rawString());
//...
}
Use something like:
while (existsPhone.hasNext()){
PhoneNumberMatch phone = existsPhone.next();
System.out.println("Phone == " + phone.number());
Log.d("existsPhone",":"+phone.rawString());
//....
}
ContentResolver musicResolver = getContentResolver();
Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri,null,null,null,null);
if(musicCursor!=null && musicCursor.moveToFirst()){
int idColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media._ID);
int titleColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int artistColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int imageColumn = musicCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART);
//Add songs to the array list
do{
String theArt = musicCursor.getString(imageColumn);
Bitmap image = BitmapFactory.decodeFile(theArt);
long songId = musicCursor.getLong(idColumn);
String songTitle = musicCursor.getString(titleColumn);
String songArtist = musicCursor.getString(artistColumn);
songsArrayList.add(new Songs(songId,songTitle,songArtist,image));
}while (musicCursor.moveToNext())
I want to get the cover image of the song which i am getting from the local storage, I have no idea how to do it so i get
int imageColumn =
musicCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART);
this code on internet to get album art this is showing error when i get string from it . That accessing column -1 , I think its not getting the right column to get the image , please let me know how to get the image from media in the way of above code
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.downloadbro.mumusic, PID: 10877
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.downloadbro.mumusic/com.downloadbro.mumusic.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2755)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2816)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1555)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6383)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:438)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at android.database.CursorWrapper.getString(CursorWrapper.java:137)
at com.downloadbro.mumusic.MainActivity.getSongList(MainActivity.java:80)
at com.downloadbro.mumusic.MainActivity.onCreate(MainActivity.java:45)
at android.app.Activity.performCreate(Activity.java:6861)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
And this may give me album art but i want media art no function as such showing to me
Please, i am trying to insert data into my database but i ran into some errors with Android about input string.
Here is my code:
public void addEvent(){
volunteerHandler volunteerHandler = new volunteerHandler(this);
String activity_name = etActivityName.getText().toString();
String location = etLocation.getText().toString();
int date = Integer.parseInt(mydp.getDayOfMonth() + "/" + (mydp.getMonth()+1) + "/" + mydp.getYear());
int time = Integer.parseInt(mytp.getHour() + ":" + mytp.getMinute());
String volunteer_name = etVolunteerName.getText().toString();
volunteerHandler.addVolunteer(activity_name, location, date, time, volunteer_name);
}
Error is:
FATAL EXCEPTION: main
Process: com.igng.ivolunteer, PID: 6375
java.lang.NumberFormatException: For input string: "31/3/2018"
at java.lang.Integer.parseInt(Integer.java:608)
at java.lang.Integer.parseInt(Integer.java:643)
at com.igng.ivolunteer.Dashboard.addEvent(Dashboard.java:125)
at com.igng.ivolunteer.Dashboard$1$1.onClick(Dashboard.java:101)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Look at this error logcat:
java.lang.NumberFormatException: For input string: "31/3/2018"
You can not parse your date into integer, you should save date with string type.
Im a beginner in java but I'm trying to build exercises and I ran into this problem. The user types in a donation, presses the button and the value of donations increments. Ive written the entire code but when I click the button to add to the donation count, the app crashes. Ive followed all the teachings of my teacher. I create a method in the activity, the in the ui builder I go to the button on click property and set it to that method. It should go smoothly but it crashes whenever I click the button. This is my activity which I set the onClick property to computeDonation.
public void computeDonation(View v){
String addedText = ((EditText)findViewById(R.id.AmountText)).getText().toString();
DonationModel donation = new DonationModel(addedText);
String answer = donation.calcDonation();
((TextView)findViewById(R.id.totalText)).setText(answer);
}
This is the model:
String textAdded;
double addedDonation;
public DonationModel(String y) {
textAdded = y;
}
public String calcDonation(){
double counter = Double.parseDouble(textAdded);
addedDonation += counter;
Locale locale = new Locale("en", "CA");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
return fmt.format(addedDonation);
}
Ive even tried without the number format just to see if it sends back to the activity but it doesn't. BTW for my class I'm using an older version of android studio.
This is the logcat:
02-15 17:12:28.797 21619-21619/ca.roumani.donations E/AndroidRuntime: FATAL EXCEPTION: main
Process: ca.roumani.donations, PID: 21619
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5197)
at android.view.View$PerformClick.run(View.java:20909)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5197)
at android.view.View$PerformClick.run(View.java:20909)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at ca.roumani.donations.DonationModel.calcDonation(DonationModel.java:20)
at ca.roumani.donations.DonationActivity.computeDonation(DonationActivity.java:24)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5197)
at android.view.View$PerformClick.run(View.java:20909)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
It seems like your textAdded is an empty String, which causes the NumberFormatException and crashes your app. What you can do to prevent this, is to check if your textAdded is not empty before parsing it:
public String calcDonation() {
if (TextUtils.isEmpty(textAdded)) {
//textAdded is empty, so just return an empty String
return "";
}
//We're sure that textAdded has a value, so we can try to parse it
double counter = Double.parseDouble(textAdded);
addedDonation += counter;
Locale locale = new Locale("en", "CA");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
return fmt.format(addedDonation);
}
Or just prevent the method from being invoked before the user has entered a value in the EditText view:
public void computeDonation(View v){
String addedText = ((EditText)findViewById(R.id.AmountText)).getText().toString();
if (TextUtils.isEmpty(textAdded)) {
Toast.makeText(context, "Please enter a valid number", Toast.LENGTH_SHORT).show();
} else {
DonationModel donation = new DonationModel(addedText);
String answer = donation.calcDonation();
((TextView)findViewById(R.id.totalText)).setText(answer);
}
}
I don't see where double addedDonation; is initialized, you can't use += when the variable has no value.
The problem is that you attempt to parse an empty string as a double. You need to verify that the user entered valid data before doing any calculations. One way to help you do this is to separate the calculation from the String passing and formatting. I suggest that you modify calcDonation() so that it only handles numeric data. Another method can worry about converting numbers to and from Strings. This will allow you to validate user input and display an appropriate error message when necessary.