java - FATAL exception, application stopped error -
i getting fatal exception error in logcat , application stopping when run.
java
package com.example.parth.editcal; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.view; import android.view.menu; import android.view.menuitem; import android.widget.edittext; import android.widget.button; import android.widget.textview; public class mainactivity extends appcompatactivity { public static int a,b,x,t1,t2 ; public static edittext tt ; public static textview ans ; /* int p = { , b ,x } if (p[4] == 0 && p[5] == 0){ p[1] = p[3]; p[4] = 1; } else if(p[4] == 1 && p[5] ==0 ){ p[1] = p[3]; p[5] = 1; } if (p[4] == 1 && p[5] == 1){ p[2] = p[3] ; p[1] = p[1] + p[2] ; p[5] = 2; } p[3] = 0 ; */ public static int logica(){ if (t1 == 0 && t2 == 0){ = x; t1 = 1; } else if(t1 == 1 && t2 ==0 ){ = x; t2 = 1; } if (t1 == 1 && t2 == 1){ b = x ; = + b ; t2 = 2 ; } x = 0; return a; } public static int logics(){ if (t1 == 0 && t2 == 0){ = x; t1 = 1; } else if(t1 == 1 && t2 ==0 ){ = x; t2 = 1; } if (t1 == 1 && t2 == 1){ b = x ; = - b ; t2 = 2 ; } x = 0; return a; } public static int logicm(){ if (t1 == 0 && t2 == 0){ = x; t1 = 1; } else if(t1 == 1 && t2 ==0 ){ = x; t2 = 1; } if (t1 == 1 && t2 == 1){ b = x ; = * b ; t2 = 2 ; } x = 0; return a; } public static int logicd(){ if (t1 == 0 && t2 == 0){ = x; t1 = 1; } else if(t1 == 1 && t2 ==0 ){ = x; t2 = 1; } if (t1 == 1 && t2 == 1){ b = x ; = / b ; t2 = 2 ; } x = 0; return a; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button add = (button) findviewbyid(r.id.add); button sub = (button) findviewbyid(r.id.sub); button mul = (button) findviewbyid(r.id.mul); button div = (button) findviewbyid(r.id.div); button eq = (button) findviewbyid(r.id.eq); tt = (edittext) findviewbyid(r.id.tt); ans = (textview) findviewbyid(r.id.ans); if (x == 0){ x = integer.parseint(tt.gettext().tostring()); } add.setonclicklistener( new button.onclicklistener(){ public void onclick(view v){ logica(); x = 0; } } ); sub.setonclicklistener( new button.onclicklistener(){ public void onclick(view v){ logics(); x = 0; } } ); mul.setonclicklistener( new button.onclicklistener() { public void onclick(view v) { logicm(); x = 0; } } ); div.setonclicklistener( new button.onclicklistener() { public void onclick(view v) { logicd(); x = 0; } } ); eq.setonclicklistener( new button.onclicklistener() { public void onclick(view v) { ans.settext(string.valueof(a)); } } ); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <edittext android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputtype="numberdecimal" android:ems="10" android:id="@+id/tt" android:layout_alignparenttop="true" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_alignparentright="true" android:layout_alignparentend="true" android:layout_margintop="50dp"/> <button style="?android:attr/buttonstylesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:id="@+id/add" android:layout_margintop="73dp" android:layout_below="@+id/tt" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_marginleft="27dp" android:layout_marginstart="27dp" /> <button style="?android:attr/buttonstylesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:id="@+id/sub" android:layout_aligntop="@+id/add" android:layout_toleftof="@+id/mul" android:layout_tostartof="@+id/mul" android:layout_marginright="42dp" android:layout_marginend="42dp" /> <button style="?android:attr/buttonstylesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="x" android:id="@+id/mul" android:layout_marginright="46dp" android:layout_marginend="46dp" android:layout_aligntop="@+id/sub" android:layout_toleftof="@+id/div" android:layout_tostartof="@+id/div" /> <button style="?android:attr/buttonstylesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="/" android:id="@+id/div" android:layout_aligntop="@+id/mul" android:layout_alignparentright="true" android:layout_alignparentend="true" android:layout_marginright="25dp" android:layout_marginend="25dp" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="=" android:id="@+id/eq" android:layout_centervertical="true" android:layout_toleftof="@+id/mul" android:layout_tostartof="@+id/mul" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancelarge" android:text="ans" android:id="@+id/ans" android:layout_below="@+id/tt" android:layout_alignleft="@+id/eq" android:layout_alignstart="@+id/eq" /> </relativelayout>
logcat
10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: fatal exception: main 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: process: com.example.parth.editcal, pid: 17077 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: java.lang.runtimeexception: unable start activity componentinfo{com.example.parth.editcal/com.example.parth.editcal.mainactivity}: java.lang.numberformatexception: invalid int: "" 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ android.app.activitythread.performlaunchactivity(activitythread.java:2548) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ android.app.activitythread.handlelaunchactivity(activitythread.java:2607) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ android.app.activitythread.access$900(activitythread.java:174) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ android.app.activitythread$h.handlemessage(activitythread.java:1325) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ android.os.handler.dispatchmessage(handler.java:102) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ android.os.looper.loop(looper.java:146) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ android.app.activitythread.main(activitythread.java:5756) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ java.lang.reflect.method.invokenative(native method) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ java.lang.reflect.method.invoke(method.java:515) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1291) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1107) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ dalvik.system.nativestart.main(native method) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: caused by: java.lang.numberformatexception: invalid int: "" 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ java.lang.integer.invalidint(integer.java:137) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ java.lang.integer.parseint(integer.java:358) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ java.lang.integer.parseint(integer.java:331) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ com.example.parth.editcal.mainactivity.oncreate(mainactivity.java:190) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ android.app.activity.performcreate(activity.java:5619) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1093) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ android.app.activitythread.performlaunchactivity(activitythread.java:2512) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ android.app.activitythread.handlelaunchactivity(activitythread.java:2607) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ android.app.activitythread.access$900(activitythread.java:174) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ android.app.activitythread$h.handlemessage(activitythread.java:1325) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ android.os.handler.dispatchmessage(handler.java:102) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ android.os.looper.loop(looper.java:146) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ android.app.activitythread.main(activitythread.java:5756) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ java.lang.reflect.method.invokenative(native method) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ java.lang.reflect.method.invoke(method.java:515) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1291) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1107) 10-27 22:36:08.318 17077-17077/com.example.parth.editcal e/androidruntime: @ dalvik.system.nativestart.main(native method)
you need check if string empty - otherwise cannot parse integer it:
if (x == 0 && !tt.gettext().tostring().isempty()){ x = integer.parseint(tt.gettext().tostring()); }
Comments
Post a Comment