utkarsh dubey utkarsh dubey - 2 months ago 8
Android Question

App crashing instead of showing toast of empty EditText

If user enters nothing and click on the ok button, the app should show a toast of enter something. But when I start app and click on the ok button without entering, the app crashes showing this error:

09-30 05:06:59.099: E/AndroidRuntime(1656): FATAL EXCEPTION: main
09-30 05:06:59.099: E/AndroidRuntime(1656): Process: com.example.mybalancesheet, PID: 1656
09-30 05:06:59.099: E/AndroidRuntime(1656): java.lang.IllegalStateException: Could not execute method for android:onClick
09-30 05:06:59.099: E/AndroidRuntime(1656): at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
09-30 05:06:59.099: E/AndroidRuntime(1656): at android.view.View.performClick(View.java:4438)
09-30 05:06:59.099: E/AndroidRuntime(1656): at android.view.View$PerformClick.run(View.java:18422)
09-30 05:06:59.099: E/AndroidRuntime(1656): at android.os.Handler.handleCallback(Handler.java:733)
09-30 05:06:59.099: E/AndroidRuntime(1656): at android.os.Handler.dispatchMessage(Handler.java:95)
09-30 05:06:59.099: E/AndroidRuntime(1656): at android.os.Looper.loop(Looper.java:136)
09-30 05:06:59.099: E/AndroidRuntime(1656): at android.app.ActivityThread.main(ActivityThread.java:5001)
09-30 05:06:59.099: E/AndroidRuntime(1656): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 05:06:59.099: E/AndroidRuntime(1656): at java.lang.reflect.Method.invoke(Method.java:515)
09-30 05:06:59.099: E/AndroidRuntime(1656): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-30 05:06:59.099: E/AndroidRuntime(1656): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-30 05:06:59.099: E/AndroidRuntime(1656): at dalvik.system.NativeStart.main(Native Method)
09-30 05:06:59.099: E/AndroidRuntime(1656): Caused by: java.lang.reflect.InvocationTargetException
09-30 05:06:59.099: E/AndroidRuntime(1656): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 05:06:59.099: E/AndroidRuntime(1656): at java.lang.reflect.Method.invoke(Method.java:515)
09-30 05:06:59.099: E/AndroidRuntime(1656): at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
09-30 05:06:59.099: E/AndroidRuntime(1656): ... 11 more


The more confusing thing is that I have a clear all button which resets everything to beginning. Now if I click on the button at this time without entering anything then it shows toast.

I am not able to understand why. First time click on input in EditText:

enter image description here

After click in clear all:

enter image description here

enter image description here

One more thing, if I try to enter something after clear all, then again the app crashes when click on
ok
button.

Code:

package com.example.mybalancesheet;



public class MainActivity extends ActionBarActivity {
TextView tv,tv1;
EditText ed;
String str;
Button bt,btNew;
int amount;


static int c;
String prefs="myPrefs";
String Salaryflag="mySalary";
SharedPreferences sp;
Cursor curs;
String err="";
int saly;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed=(EditText)findViewById(R.id.edEnteredSal);
tv=(TextView)findViewById(R.id.tvSal);
tv1=(TextView)findViewById(R.id.textVProm);
bt=(Button)findViewById(R.id.buttOk);
btNew=(Button)findViewById(R.id.button2);
sp=getSharedPreferences(prefs,Context.MODE_PRIVATE);
c=sp.getInt(Salaryflag, 0);
str=Integer.toString(c);

tv.setText(str);

ItemsDataBase xyx=new ItemsDataBase(this);
xyx.open();
int lola;
lola=xyx.getSum();

xyx.close();
c=c-lola;
str=Integer.toString(c);
tv.setText(str);

}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
sp=getSharedPreferences(prefs,Context.MODE_PRIVATE);
c=sp.getInt(Salaryflag, 0);
str=Integer.toString(c);
ItemsDataBase xyx=new ItemsDataBase(this);
xyx.open();
int lola;
lola=xyx.getSum();

xyx.close();
c=c-lola;
str=Integer.toString(c);
tv.setText(str);



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent i=new Intent(MainActivity.this,SqlView.class);
startActivity(i);

return true;
}
return super.onOptionsItemSelected(item);
}

public void salaryEntered(View v){



String sal=ed.getText().toString();

if(sal.contentEquals(" ")){
Toast.makeText(getBaseContext(), "Put salary here: Don't leave it empty ", Toast.LENGTH_LONG).show();
}else{

saly=Integer.parseInt(sal);
sp=getSharedPreferences(prefs,Context.MODE_PRIVATE);
Editor ed1=sp.edit();
ed1.putInt(Salaryflag, saly);
ed1.commit();
Toast.makeText(getBaseContext(), "Salary Saved", Toast.LENGTH_SHORT).show();
c=sp.getInt(Salaryflag, 0);
str=Integer.toString(c);

tv.setText(str);
ed.setText(" ");
ed.setVisibility(View.INVISIBLE);
tv1.setVisibility(View.INVISIBLE);
bt.setVisibility(View.INVISIBLE);
}


}
public void goTo(View v){
Intent i=new Intent(this,EnteredData.class);
startActivity(i);

}
public void clear(View v){

boolean worked=false;
try{

ItemsDataBase xyx=new ItemsDataBase(this);
xyx.open();
xyx.delete();
xyx.close();
sp=getSharedPreferences(prefs,Context.MODE_PRIVATE);
sp.edit().remove(Salaryflag).commit();
tv.setText("0");

worked=true;




}catch(Exception e){
worked=false;
err=e.toString();

}finally{
if(worked==true){
Toast.makeText(getBaseContext(), "Data is deleted completely !", Toast.LENGTH_LONG).show();
}else if(worked==false){
Toast.makeText(getBaseContext(), "Error: "+err,Toast.LENGTH_LONG ).show();
}


btNew.setVisibility(View.INVISIBLE);
ed.setVisibility(View.VISIBLE);
tv1.setVisibility(View.VISIBLE);
bt.setVisibility(View.VISIBLE);

}

}
}


This error is shown when I click on empty edittext :

09-30 05:10:37.069: E/AndroidRuntime(1741): FATAL EXCEPTION: main
09-30 05:10:37.069: E/AndroidRuntime(1741): Process: com.example.mybalancesheet, PID: 1741
09-30 05:10:37.069: E/AndroidRuntime(1741): java.lang.IllegalStateException: Could not execute method for android:onClick
09-30 05:10:37.069: E/AndroidRuntime(1741): at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
09-30 05:10:37.069: E/AndroidRuntime(1741): at android.view.View.performClick(View.java:4438)
09-30 05:10:37.069: E/AndroidRuntime(1741): at android.view.View$PerformClick.run(View.java:18422)
09-30 05:10:37.069: E/AndroidRuntime(1741): at android.os.Handler.handleCallback(Handler.java:733)
09-30 05:10:37.069: E/AndroidRuntime(1741): at android.os.Handler.dispatchMessage(Handler.java:95)
09-30 05:10:37.069: E/AndroidRuntime(1741): at android.os.Looper.loop(Looper.java:136)
09-30 05:10:37.069: E/AndroidRuntime(1741): at android.app.ActivityThread.main(ActivityThread.java:5001)
09-30 05:10:37.069: E/AndroidRuntime(1741): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 05:10:37.069: E/AndroidRuntime(1741): at java.lang.reflect.Method.invoke(Method.java:515)
09-30 05:10:37.069: E/AndroidRuntime(1741): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-30 05:10:37.069: E/AndroidRuntime(1741): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-30 05:10:37.069: E/AndroidRuntime(1741): at dalvik.system.NativeStart.main(Native Method)
09-30 05:10:37.069: E/AndroidRuntime(1741): Caused by: java.lang.reflect.InvocationTargetException
09-30 05:10:37.069: E/AndroidRuntime(1741): at java.lang.reflect.Method.invokeNative(Native Method)
09-30 05:10:37.069: E/AndroidRuntime(1741): at java.lang.reflect.Method.invoke(Method.java:515)
09-30 05:10:37.069: E/AndroidRuntime(1741): at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
09-30 05:10:37.069: E/AndroidRuntime(1741): ... 11 more
09-30 05:10:37.069: E/AndroidRuntime(1741): Caused by: java.lang.NumberFormatException: Invalid int: ""
09-30 05:10:37.069: E/AndroidRuntime(1741): at java.lang.Integer.invalidInt(Integer.java:137)
09-30 05:10:37.069: E/AndroidRuntime(1741): at java.lang.Integer.parseInt(Integer.java:358)
09-30 05:10:37.069: E/AndroidRuntime(1741): at java.lang.Integer.parseInt(Integer.java:331)
09-30 05:10:37.069: E/AndroidRuntime(1741): at com.example.mybalancesheet.MainActivity.salaryEntered(MainActivity.java:116)
09-30 05:10:37.069: E/AndroidRuntime(1741): ... 14 more


My xml file :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textVProm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter ur Salary"
android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
android:id="@+id/edEnteredSal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >

<requestFocus />
</EditText>

<Button
android:id="@+id/buttOk"
android:layout_gravity="center"
android:onClick="salaryEntered"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
<TextView
android:id="@+id/tvSal"
android:layout_width="wrap_content"
android:layout_marginTop="34dp"
android:layout_gravity="center"

android:layout_height="wrap_content"
android:text="0"
android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="23dp"
android:onClick="goTo"
android:layout_height="wrap_content"
android:text="New Entry" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:onClick="clear"
android:layout_height="wrap_content"
android:text="Clear All" />


</LinearLayout>

AL. AL.
Answer

Your comparization or null checker is wrong. In your salaryEntered():

if(sal.contentEquals(" ")){

You should make use of

TextUtils.isEmpty()

instead. For example, if the EditText is empty, then you use your original condition, it will look like this:

"" == " "

-- which is FALSE, which will make it go to the else block and try to parse the null value by calling

saly=Integer.parseInt(sal);

that gives the error:

Caused by: java.lang.NumberFormatException: Invalid int: ""