-3

I am new to Android and Java programming and for some reason (I cant point out) my app wont even open.It says "Unfortunately 'app name' has crashed". It has no compile-time errors in it?

Here is the Logcat:

08-19 04:54:07.024  24170-24170/com.elie.billsplitter E/AndroidRuntime﹕FATAL EXCEPTION: main
Process: com.elie.billsplitter, PID: 24170
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.elie.billsplitter/com.elie.billsplitter.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        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:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.NumberFormatException: Invalid int: ""
        at java.lang.Integer.invalidInt(Integer.java:138)
        at java.lang.Integer.parseInt(Integer.java:358)
        at java.lang.Integer.parseInt(Integer.java:334)
        at com.elie.billsplitter.MainActivity.<init>(MainActivity.java:11)
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.Class.newInstance(Class.java:1606)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            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:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
08-19 04:54:09.781  24170-24177/com.elie.billsplitter W/art﹕ Suspending all threads took: 793.743ms
08-19 04:54:36.935  24170-24170/com.elie.billsplitter I/Process﹕ Sending signal. PID: 24170 SIG: 9

Here is the Java file:

public class MainActivity extends Activity {
public int x = Integer.parseInt("");
public int y = Integer.parseInt("");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Button
    Button btn = (Button) findViewById(R.id.button);


    //EditText
    EditText nop = (EditText) findViewById(R.id.editText);
    EditText cob = (EditText) findViewById(R.id.editText2);

    x = Integer.parseInt(nop.getText().toString());
    y = Integer.parseInt(cob.getText().toString());
    final TextView tv = (TextView) findViewById(R.id.textView);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int z = x / y;
            tv.setText(z);
        }
    });

}
}
  • 3
    `java.lang.NumberFormatException: Invalid int: ""` – Blackbelt Aug 19 '15 at 10:32
  • need some code buddy but Invalid int: "" may be a pointer to what's going on – Ben Aug 19 '15 at 10:32
  • 1
    "It has no errors in it". You mean it has no compile-time errors, but nobody cares about those. You've got plenty of errors in your code, buddy. – Kayaman Aug 19 '15 at 10:41
  • 1
    What does this say `Caused by: java.lang.NumberFormatException: Invalid int: ""` It clearly says that some where you are trying to parse a blank string as integer. – Rohit5k2 Aug 19 '15 at 10:42
  • `It has no errors in it`... So, how do you call this one `Caused by: java.lang.NumberFormatException: Invalid int: ""`? I call it an **error**. – Phantômaxx Aug 19 '15 at 10:45
  • @Rohit5k2 i completely took away all of the the empty integers and it still says that same error – user5242437 Aug 19 '15 at 11:53

4 Answers4

1

Most probably you are parsing an empty string into the integer.

x = Integer.parseInt(nop.getText().toString());
y = Integer.parseInt(cob.getText().toString());

before getting the text from the Edittext check that whether it is empty or not. You are parsing an empty string probably.

you can make a check like this:

if(!(nop.toString().trim().equalsIgnoreCase("") && cob.toString().trim().equalsIgnoreCase(""))){
      x = Integer.parseInt(nop.getText().toString());
      y = Integer.parseInt(cob.getText().toString());
}

and also your are making the initilization the integers in an incorrect way:

May be these are the lines where you are getting exceptions. You cannot parse blank strings to integer. instead of this:

public int x = Integer.parseInt("");
public int y = Integer.parseInt("");

write this:

public int x = 0;
public int y = 0;

or

public int x = Integer.parseInt("0");
public int y = Integer.parseInt("0");
Pravesh
  • 822
  • 1
  • 8
  • 20
0

Before converting to a number you can check if it is a numeric string. For some ideas read this thread on stackoverflow: How to check if a String is numeric in Java

Community
  • 1
  • 1
Thomas R.
  • 7,988
  • 3
  • 30
  • 39
0

Somewhere in your code you are converting a invalid string or a empty string into a number, which is causing the NumberFormatException.

String x = "abc";
int num = Integer.parseInt(x);

How do I solve it?

try
{
    String x = "abc";
    int num = Integer.parseInt(x);
}
catch(NumberFormatException ne)
{
    System.out.println("Invalid Number!");
}
Uma Kanth
  • 5,659
  • 2
  • 20
  • 41
0

In your code, replace:

public int x = Integer.parseInt("");
public int y = Integer.parseInt("");

with

public int x;
public int y;

The default values of x and y will be 0. You don't have to add that.

Reason of Error: Integer.parseInt() converts the string inside it to an integer. You have tried to convert "" to an integer, which is not even a number... So NumberFormatException occurred.

Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57