Question

Just want to let everyone know this is the first app I am trying to create. So if I ask any stupid questions I apologize. I am trying to create an alert dialog that will return a number for use later in the app. The dialog displays on the emulator, but upon selection of one of the options the app crashes.

01-22 17:37:43.925: I/Process(443): Sending signal. PID: 443 SIG: 9
01-22 17:52:47.395: D/dalvikvm(480): GC_EXTERNAL_ALLOC freed 42K, 53% free 2550K/5379K, external 1625K/2137K, paused 48ms
01-22 17:53:01.145: W/KeyCharacterMap(480): No keyboard for id 0
01-22 17:53:01.145: W/KeyCharacterMap(480): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
01-22 17:53:02.205: D/AndroidRuntime(480): Shutting down VM
01-22 17:53:02.205: W/dalvikvm(480): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-22 17:53:02.225: E/AndroidRuntime(480): FATAL EXCEPTION: main
01-22 17:53:02.225: E/AndroidRuntime(480): java.lang.NumberFormatException: unable to parse 'null' as integer
01-22 17:53:02.225: E/AndroidRuntime(480):  at java.lang.Integer.parseInt(Integer.java:356)
01-22 17:53:02.225: E/AndroidRuntime(480):  at java.lang.Integer.parseInt(Integer.java:332)
01-22 17:53:02.225: E/AndroidRuntime(480):  at com.ciltild.mtgcardsandrules.PlayerTools.setAlertVar(PlayerTools.java:81)
01-22 17:53:02.225: E/AndroidRuntime(480):  at com.ciltild.mtgcardsandrules.PlayerTools$1.onClick(PlayerTools.java:41)
01-22 17:53:02.225: E/AndroidRuntime(480):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
01-22 17:53:02.225: E/AndroidRuntime(480):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-22 17:53:02.225: E/AndroidRuntime(480):  at android.os.Looper.loop(Looper.java:123)
01-22 17:53:02.225: E/AndroidRuntime(480):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-22 17:53:02.225: E/AndroidRuntime(480):  at java.lang.reflect.Method.invokeNative(Native Method)
01-22 17:53:02.225: E/AndroidRuntime(480):  at java.lang.reflect.Method.invoke(Method.java:507)
01-22 17:53:02.225: E/AndroidRuntime(480):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-22 17:53:02.225: E/AndroidRuntime(480):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-22 17:53:02.225: E/AndroidRuntime(480):  at dalvik.system.NativeStart.main(Native Method)

The following is the code is for the alert dialog:

    @Override
protected Dialog onCreateDialog(int id){
    switch(id){

    case playerLifeDialog:
        LayoutInflater factory = LayoutInflater.from(this);
        final View playerSetLifeView = factory.inflate(R.layout.toolslifedialog, null);
        return new AlertDialog.Builder(PlayerTools.this)
            .setTitle("Set Life to:")
            .setView(playerSetLifeView)
            .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int addButton) {
                    setAlertVar(toolsDialogText);
                    tempLifeVar = alertVar + playerLifeVar;
                }
            })
            .setNegativeButton("Subtract", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int setButton) {
                    setAlertVar(toolsDialogText);
                    tempLifeVar = playerLifeVar - alertVar;
                }
            })
            .setNeutralButton("Set", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int subButton) {
                    setAlertVar(toolsDialogText);
                    tempLifeVar = alertVar;
                }
            })
            .create();
        default:
    }
    return null;
}

I have spent a week looking over questions others have asked and on the Google developers doc to try to figure this out. If I knew more about Java I'm sure this wouldn't be so hard. But I have been stuck on this for too long. Also, this is my second attempt at creating this app. Any help would be very much appreciated.

This is the setAlertVar code:

public void setAlertVar(String toolsDialogText) throws NumberFormatException{
    alertVar = Integer.parseInt(toolsDialogText);
}
Was it helpful?

Solution

where are setting the value for toolsDialogText before passing it to setAlertVar(toolsDialogText);

So if you are not setting it,it is having a null value which cannot be parsed by the Integer class...Better check for

` if(!TextUtil.isEmpty(toolsDialogText)) {

alertVar = Integer.parseInt(toolsDialogText);

}

return alertVar;`

OTHER TIPS

Your problem is in your setAlertVar() method, at line 81 (it's hard to say anything else without the setAlertVar() code):

 at com.ciltild.mtgcardsandrules.PlayerTools.setAlertVar(PlayerTools.java:81)

If you know you might have invalid numbers, try surrounding the code that is throwing the exception above:

try {
  int b = Integer.parseInt(string);
} catch (Exception e) {
  //handle the exception if necessary
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top