0

I am completely new to Android programming. I am having some issues with the first ever app I am making. Every time I run the app, a message pops up saying "Unfortunately, MyNewApp3 has stopped" on my mobile phone (which I am using to test and debug my app). This error doesn't even let my app open. How do I solve this problem?

Here is my code:

package newapp.com.bipl.mynewapp3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class Page2 extends AppCompatActivity {

RadioGroup Rg;
RadioButton B;
RadioButton O;
RadioButton H;
EditText Dec;
TextView Ans;
int Num;
String Result;
int b;
int c;
char z;
String s;
String d;


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

    Toast.makeText(getBaseContext(),"Successful Login",Toast.LENGTH_LONG).show();

    Rg = (RadioGroup) findViewById(R.id.RGroup);
    B = (RadioButton) findViewById(R.id.Binary_rb);
    O = (RadioButton) findViewById(R.id.Octal_rb);
    H = (RadioButton) findViewById(R.id.Hexa_rb);
    Dec = (EditText) findViewById(R.id.NumEntry_txt);
    Ans = (TextView) findViewById(R.id.Answer_lbl);
    Num = Integer.parseInt(Dec.getText().toString());
    //Num = 161;
    if(B.isSelected())
        Binary(Num);
    if(O.isSelected())
        Octal(Num);
    if (H.isSelected())
        Hexal(Num);

}

/* public void Conversion(){
    if(B.isSelected())
        Binary(Num);
    if(O.isSelected())
        Octal(Num);
    if (H.isSelected())
        Hexal(Num);
}*/

public void Binary(int a){

    while(b > 0){
        c = a % 2;
        b = a/2;
        d = "" + c;
        s = d.concat(s);
        a = b;
    }
    Result = s;
    Toast.makeText(getBaseContext(), "" + Result, Toast.LENGTH_SHORT).show();
    Ans.setText("" + Result);

}

public void Octal(int a){

    while(b > 0){
        c = a % 8;
        b = a/8;
        d = "" + c;
        s = d.concat(s);
        a = b;
    }
    Result = s;
    Toast.makeText(getBaseContext(), "" + Result, Toast.LENGTH_SHORT).show();
    Ans.setText("" + Result);

}

public void Hexal(int a){

    while(b > 0){
        c = a % 16;
        b = a/16;

        if(c>9){
            switch (c){
                case 10 : z = 'A';
                          break;
                case 11 : z = 'B';
                          break;
                case 12 : z = 'C';
                          break;
                case 13 : z = 'D';
                          break;
                case 14 : z = 'E';
                          break;
                case 15 : z = 'F';
                          break;
            }

            d = "" + z;

        }
        else
            d = "" + c;

        s = d.concat(s);
        a = b;
    }

    Result = s;
    Toast.makeText(getBaseContext(), "" + Result, Toast.LENGTH_SHORT).show();
    Ans.setText("" + Result);


  }
}

My LogCat output (this I have posted after having seen people advise others to put the Logcat output; I don't know what it is used for) :

01-12 11:45:52.559 15441-15441/newapp.com.bipl.mynewapp3 V/TextView: stopSelectionActionMode() 01-12 11:45:52.572 15441-15441/newapp.com.bipl.mynewapp3 D/AndroidRuntime: Shutting down VM 01-12 11:45:52.573 15441-15441/newapp.com.bipl.mynewapp3 E/AndroidRuntime: FATAL EXCEPTION: main Process: newapp.com.bipl.mynewapp3, PID: 15441 java.lang.RuntimeException: Unable to start activity ComponentInfo{newapp.com.bipl.mynewapp3/newapp.com.bipl.mynewapp3.Page2}: java.lang.NumberFormatException: Invalid int: "" at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595) at android.app.ActivityThread.access$800(ActivityThread.java:178) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:194) at android.app.ActivityThread.main(ActivityThread.java:5631) 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:959) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 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 newapp.com.bipl.mynewapp3.Page2.onCreate(Page2.java:41) at android.app.Activity.performCreate(Activity.java:6092) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595)  at android.app.ActivityThread.access$800(ActivityThread.java:178)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)  at android.os.Handler.dispatchMessage(Handler.java:111)  at android.os.Looper.loop(Looper.java:194)  at android.app.ActivityThread.main(ActivityThread.java:5631)  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:959)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)  01-12 11:50:52.616 15441-15441/newapp.com.bipl.mynewapp3 I/Process: Sending signal. PID: 15441 SIG: 9

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

7 Answers7

0

You have to check for null every time you display or use any variable to convert here the issue is you are not checking the string to be null, and in your case it is null. so perform

if(Dec != null){
    if(Dec.getText() != null){
        Num = Integer.parseInt(Dec.getText().toString());
    }
}

It should work fine!

Amit
  • 577
  • 5
  • 7
0

Declare and assign zero to your 'Num" Variable as follows

int Num = 0;

Then first Get your EditText Value in String Format and then parse That

String mynum = Dec.getText().toString();
Num = Integer.parseInt(mynum);

Here you can Check your myNum is null or not.

Saurabh Bhandari
  • 2,438
  • 4
  • 26
  • 33
0

You can add try-catch to convert String to int. Won't crash if it gets anything like empty string which can't possible to convert.

try
{
    if(str != null){
      Num = Integer.parseInt(Dec.getText().toString());
    }
    else{
      Num = 0;
    }
}
catch (NumberFormatException e)
{
    Num = 0;
}

This implementation will stop crashing for any wrong input conversion.

Exigente05
  • 2,161
  • 3
  • 22
  • 42
0

when the EditText init in onCreate() method,there are nothing typed in it,so Dec.getText().toString is "",of course, you can't invoke Integer.parseInt("") so that you got the NumberFormatException.

As a result,you should add a checking before parseInt()

if (!"".equal(Dec.getText().toString)){
    Num = Integer.parseInt(Dec.getText().toString());
}
木大白易
  • 684
  • 1
  • 7
  • 16
0

The problem is that you try to convert an empty string to integer using parse Int as soon as the app starts. See this stackoverflow post for more info. So you just check if the editText is returning a null string or you can also try to catch the exception.

Also logcat is very useful, it helps you to identify which part or line of code is causing the problem. Check the logcat-

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 newapp.com.bipl.mynewapp3.Page2.onCreate(Page2.java:41)

It clearly tells you that parseInt method is throwing an exception and clicking on 41 in below line even takes you to that piece of code.

newapp.com.bipl.mynewapp3.Page2.onCreate(Page2.java:41)

Try this code-

package newapp.com.bipl.mynewapp3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class Page2 extends AppCompatActivity {

RadioGroup Rg;
RadioButton B;
RadioButton O;
RadioButton H;
EditText Dec;
TextView Ans;
int Num;
String Result;
int b;
int c;
char z;
String s;
String d;


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

    Toast.makeText(getBaseContext(),"Successful Login",Toast.LENGTH_LONG).show();

    Rg = (RadioGroup) findViewById(R.id.RGroup);
    B = (RadioButton) findViewById(R.id.Binary_rb);
    O = (RadioButton) findViewById(R.id.Octal_rb);
    H = (RadioButton) findViewById(R.id.Hexa_rb);
    Dec = (EditText) findViewById(R.id.NumEntry_txt);
    Ans = (TextView) findViewById(R.id.Answer_lbl);

    //Num = 161;

if ("".equals(Dec.getText().toString()))
 {   if(B.isSelected() || O.isSelected() || H.isSelected())
       Ans.setText("Empty field");     }
else{if(B.isSelected())
            Binary(Num);
        if(O.isSelected())
            Octal(Num);
        if (H.isSelected())
            Hexal(Num);}


    }

/* public void Conversion(){
    if(B.isSelected())
        Binary(Num);
    if(O.isSelected())
        Octal(Num);
    if (H.isSelected())
        Hexal(Num);
}*/

public void Binary(int a){
    Num = Integer.parseInt(Dec.getText().toString());    
    while(b > 0){
        c = a % 2;
        b = a/2;
        d = "" + c;
        s = d.concat(s);
        a = b;
    }
    Result = s;
    Toast.makeText(getBaseContext(), "" + Result, Toast.LENGTH_SHORT).show();
    Ans.setText("" + Result);

}

public void Octal(int a){
    Num = Integer.parseInt(Dec.getText().toString());
    while(b > 0){
        c = a % 8;
        b = a/8;
        d = "" + c;
        s = d.concat(s);
        a = b;
    }
    Result = s;
    Toast.makeText(getBaseContext(), "" + Result, Toast.LENGTH_SHORT).show();
    Ans.setText("" + Result);

}

public void Hexal(int a){
    Num = Integer.parseInt(Dec.getText().toString());
    while(b > 0){
        c = a % 16;
        b = a/16;

        if(c>9){
            switch (c){
                case 10 : z = 'A';
                          break;
                case 11 : z = 'B';
                          break;
                case 12 : z = 'C';
                          break;
                case 13 : z = 'D';
                          break;
                case 14 : z = 'E';
                          break;
                case 15 : z = 'F';
                          break;
            }

            d = "" + z;

        }
        else
            d = "" + c;

        s = d.concat(s);
        a = b;
    }

    Result = s;
    Toast.makeText(getBaseContext(), "" + Result, Toast.LENGTH_SHORT).show();
    Ans.setText("" + Result);


  }
}
Community
  • 1
  • 1
Rohan Bhatia
  • 1,870
  • 2
  • 15
  • 31
0

Check for Null exception:

if (TextUtils.isEmpty(Dec.getText().toString())){
        //value is empty
}else {
       Num = Integer.parseInt(Dec.getText().toString());
}
if(B.isSelected())
    Binary(Num);
if(O.isSelected())
    Octal(Num);
if (H.isSelected())
    Hexal(Num);

           ......
          .............
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
  • *"Check for Null exception"* is wrong. OP doesn't check for `null` or catches an exception. – Tom Jan 15 '17 at 09:35
0

You have to create some button or some action listener like edittext ontextchangeListener and then you have to put your code as below:

If you are using button:

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(!Dec.getText.toString.equals(""))
              Num = Integer.parseInt(Dec.getText().toString());
        }
    });

If you are using edittext textchangelistener:

TextWatcher textWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        if(s.length() > 0)
           Num = Integer.parseInt(s.getText().toString());
    }
};

The problem with your code is edittext doesn't have any value when you are parsing it. So to parse it, ParseInt needs some value. So better approach will be to take it on some button action.

Siraj Sumra
  • 934
  • 1
  • 11
  • 28