-1

I have a bunch of EditTexts and I want to pass data gotten from the EditTexts to another Activity using Intents. Whenever I run the app, it gets a runtime error or something like that and crashes. The Logcat says my application is doing too much work on the thread. Is there any way around this?

Here's my code:

@Override
public void onClick(View v) {

    final Intent a = new Intent (getApplicationContext(), Cooling.class);
    a.putExtra("value15", 15f * Double.valueOf(edit15watt.getText().toString()));
    a.putExtra("value20", 20f * Double.valueOf(edit20watt.getText().toString()));
    a.putExtra("value40", 40f * Double.valueOf(edit40watt.getText().toString()));
    a.putExtra("value60", 60f * Double.valueOf(edit60watt.getText().toString()));
    a.putExtra("value75", 75f * Double.valueOf(edit75watt.getText().toString()));
    a.putExtra("value100", 100f * Double.valueOf(edit100watt.getText().toString()));
    a.putExtra("value11", 11f * Double.valueOf(edit11watt.getText().toString()));
    a.putExtra("value18", 18f * Double.valueOf(edit18watt.getText().toString()));
    a.putExtra("value23", 23f * Double.valueOf(edit23watt.getText().toString()));
    a.putExtra("value50", 50f * Double.valueOf(edit50watt.getText().toString()));
    a.putExtra("value90", 90f * Double.valueOf(edit90watt.getText().toString()));
    a.putExtra("value200", 200f * Double.valueOf(edit200watt.getText().toString()));
    a.putExtra("value250", 250f * Double.valueOf(edit250watt.getText().toString()));
    startActivity(a);
}

this is what the logcat says

 04-17 08:20:57.197: E/AndroidRuntime(1102): FATAL EXCEPTION: main

04-17 08:20:57.197: E/AndroidRuntime(1102): Process: com.emma.finalyearproject, PID: 1102

04-17 08:20:57.197: E/AndroidRuntime(1102): java.lang.NumberFormatException: Invalid double: ""

04-17 08:20:57.197: E/AndroidRuntime(1102):     at java.lang.StringToReal.invalidReal(StringToReal.java:63)

04-17 08:20:57.197: E/AndroidRuntime(1102):     at java.lang.StringToReal.parseDouble(StringToReal.java:248)

04-17 08:20:57.197: E/AndroidRuntime(1102):     at java.lang.Double.parseDouble(Double.java:295)

04-17 08:20:57.197: E/AndroidRuntime(1102):     at java.lang.Double.valueOf(Double.java:332)

04-17 08:20:57.197: E/AndroidRuntime(1102):     at com.emma.finalyearproject.Lighting$1.onClick(Lighting.java:43)

04-17 08:20:57.197: E/AndroidRuntime(1102):     at android.view.View.performClick(View.java:4424)

04-17 08:20:57.197: E/AndroidRuntime(1102):     at android.view.View$PerformClick.run(View.java:18383)

04-17 08:20:57.197: E/AndroidRuntime(1102):     at android.os.Handler.handleCallback(Handler.java:733)

04-17 08:20:57.197: E/AndroidRuntime(1102):     at android.os.Handler.dispatchMessage(Handler.java:95)

04-17 08:20:57.197: E/AndroidRuntime(1102):     at android.os.Looper.loop(Looper.java:137)

04-17 08:20:57.197: E/AndroidRuntime(1102):     at android.app.ActivityThread.main(ActivityThread.java:4998)

04-17 08:20:57.197: E/AndroidRuntime(1102):     at java.lang.reflect.Method.invokeNative(Native Method)

04-17 08:20:57.197: E/AndroidRuntime(1102):     at java.lang.reflect.Method.invoke(Method.java:515)

04-17 08:20:57.197: E/AndroidRuntime(1102):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)

04-17 08:20:57.197: E/AndroidRuntime(1102):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)

04-17 08:20:57.197: E/AndroidRuntime(1102):     at dalvik.system.NativeStart.main(Native Method)
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • This is not about number of extras you are adding, but work you are doing on a UI thread. OnClick() runs on a UI thread, and if you need to perform long running operation you must do same in separate thread! – Kanak Sony Apr 17 '15 at 11:48
  • check this link:http://stackoverflow.com/questions/12496700/maximum-length-of-intent-putextra-method-force-close – prakash Apr 17 '15 at 11:50
  • Please show the content of the LogCat – Manuel Allenspach Apr 17 '15 at 11:53
  • 1
    @KanakSony where do you see "long running" operation here? – pskink Apr 17 '15 at 11:54
  • @pskink whatever he is doing inside a click it seems me heavy processing as per my understanding, as in each single putExtra, he is getting text-> conversion to string -> Conversion to double -> multiplication to some number. These are even expensive if being tested in an emulator. – Kanak Sony Apr 17 '15 at 12:06
  • @KanakSony so i did a little measurement on the emulator: first i used `System.currentTimeMillis()` and it turned out that it took `0` (zero) milliseconds, so i used `System.nanoTime()`, this time the results were ~`300000` - `400000` nanoseconds (0.3-0.4 milliseconds), do you think that 0.4 millisecond is a "long operation"? – pskink Apr 17 '15 at 12:23
  • Comments like "long running operation", "lengthy" etc are not relevant. If you look in the logcat, you can see that there's a conversion/cast going wrong: `java.lang.NumberFormatException: Invalid double: ""`. OP, check out [this thread](http://stackoverflow.com/a/9588167/2111834). – Edwin Lambregts Apr 17 '15 at 13:10
  • @EdwinLambregts the problem with OP (and more or less 50% posters here) is they don't even bother to post a trace back (let alone analyze it before asking) OP decided to post it 30 minutes after asking his question – pskink Apr 17 '15 at 13:25
  • ok i will...im new to android. im hoping to improve soon. thank you all for your comments – Emmanuel Uloko Apr 17 '15 at 13:32
  • @Edwin Lamberts, I checked out the thread. I'll try it out. but I want to understand this....is creating and intent to carry another intent the same as creating a new thread? – Emmanuel Uloko Apr 17 '15 at 13:37
  • @EmmanuelUloko No, the intent will run on the same thread. – Edwin Lambregts Apr 17 '15 at 13:38
  • and Also, what happens when the user does not input anything in one or two of the Edit texts – Emmanuel Uloko Apr 17 '15 at 13:39
  • I finally found out what was wrong...Whenever I do not input any value in any of the editexts, the program crashed, giving the number exception error. so I made all the edittexts to have a default value of zero. this solved my problem – Emmanuel Uloko Apr 22 '15 at 11:25

2 Answers2

0

you can pass data upto 1mb but log says its due to NumberFormatException

amodkanthe
  • 4,345
  • 6
  • 36
  • 77
-2

As such there is no limit on the number of extras defined. You can use the number of extras you are currently using. But the issue is related to NumberFormatException. This is not related to Android at all. Check the line of your program on which exception is happening. Line number 43 of your program is causing issue. As "" is not a valid double so this exception is happening.

Ankit Jain
  • 2,230
  • 1
  • 18
  • 25