应用程序崩溃而不是显示空EditText的Toast

时间:2016-09-30 09:23:38

标签: android android-button android-toast

如果用户没有输入任何内容并单击确定按钮,应用程序应显示输入内容的祝酒词。但是,当我启动应用程序并单击确定按钮而不进入时,应用程序崩溃显示此错误:

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

更令人困惑的是,我有一个清除所有按钮,它将所有内容重置为开头。现在,如果我此时点击按钮而没有输入任何内容,那么它会显示吐司。

我无法理解为什么。首次点击EditText中的输入:

enter image description here

点击全部清除后:

enter image description here

enter image description here

还有一件事,如果我尝试在清除所有之后输入内容,那么当点击ok按钮时应用程序再次崩溃。

代码:

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);

        }

    }
}

单击空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

我的xml文件:

<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>

1 个答案:

答案 0 :(得分:0)

你的比较或空检查是错误的。在salaryEntered()

if(sal.contentEquals(" ")){

你应该使用

TextUtils.isEmpty()

代替。例如,如果EditText为空,那么您使用原始条件,它将如下所示:

"" == " "

- 这是FALSE,它将转到else块并尝试通过调用

来解析空值
saly=Integer.parseInt(sal);

给出错误:

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