简体   繁体   English

没有输入任何输入且警报对话框无法修复错误时程序退出

[英]Program exiting when no input given and alert dialog not fixing the error

Here's the Program, it works and all but when I Pull the seek bar when there is not number in the input box the program exits and the alert dialog is not doing its job even though i put a try and catch on the code. 这是该程序,它可以正常工作,但是当我在输入框中没有数字的情况下拉出搜索栏时,该程序将退出,并且即使我尝试抓住代码,警报对话框也无法正常工作。

 public class MainActivity extends Activity {

    private SeekBar sbCash;
    private Button btnten,btntwenty,btnthirty;
    private TextView tvShwProg,tvfinal;
    private EditText etCash;
    final Context context = this;


    @Override
    public void onCreate(Bundle savedInstanceState)
    {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etCash = (EditText)findViewById(R.id.etCash);
        sbCash = (SeekBar)findViewById(R.id.sbCash);
        tvShwProg = (TextView)findViewById(R.id.tvShwProg);
        tvfinal = (TextView)findViewById(R.id.tvFinal);
        btnten = (Button)findViewById(R.id.btnten);
        btntwenty = (Button)findViewById(R.id.btntwenty);
        btnthirty = (Button)findViewById(R.id.btnthirty);

        etCash.addTextChangedListener(new TextWatcher(){

            public void afterTextChanged(Editable arg0) 
            {


            }
            public void beforeTextChanged(CharSequence arg0, int arg1,int arg2, int arg3) 
            {

            }
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {

                try{
                guiChanged();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                    errormessage();
                }
            }


        });


        SeekBar.OnSeekBarChangeListener sbListener = new SeekBar.OnSeekBarChangeListener() 
        {
            public void onStopTrackingTouch(SeekBar seekBar) {}
            public void onStartTrackingTouch(SeekBar seekBar) {}
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
            {
                try{
                guiChanged();
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                    errormessage();

                }
            }


        };
        sbCash.setOnSeekBarChangeListener(sbListener);

        dosomethig();
        dosomethig2();
        dosomething3(); 
    }

    public AlertDialog errormessage()
    {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setMessage("Fire Missile?")
               .setPositiveButton("Exit!", new DialogInterface.OnClickListener() 
               {
                   public void onClick(DialogInterface dialog, int id) 
                   {
                       // FIRE ZE MISSILES!
                   }
               })
               .setNegativeButton("Fix", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }


    public void guiChanged()
    {
            try{
                DecimalFormat fmt = new DecimalFormat ("0.00");

                int CashMoney = sbCash.getProgress();

                tvShwProg.setText("Current Percentage " +CashMoney+ "%");

                double amount = Double.parseDouble(etCash.getText().toString());

                double moneydue = (amount * (CashMoney/100.0));

                double totalamount = (moneydue + amount);

                tvfinal.setText("Tip " + fmt.format(moneydue)+ " Total "+ fmt.format(totalamount));
            }
            catch(Exception e)
            {
                errormessage();
                e.printStackTrace();

                etCash.setText("Bro enter a number here");
            }

    }            


    public void dosomethig()
    {

        btnten.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                sbCash.setProgress(10);


            }
        });
    }
    public void dosomethig2()
    {

        btntwenty.setOnClickListener(new View.OnClickListener() 
        {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                sbCash.setProgress(15);
            }


        });
    }
    public void dosomething3()
    {
        btnthirty.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                sbCash.setProgress(20);

            }
        });
    }

Logcat: Logcat:

   05-12 19:44:27.144: E/ActivityManager(71): ANR in com.Shashank.nchs.Final_exam.hayes (com.Shashank.nchs.Final_exam.hayes/.MainActivity)
    05-12 19:44:27.144: E/ActivityManager(71): Reason: keyDispatchingTimedOut
    05-12 19:44:27.144: E/ActivityManager(71): Load: 0.57 / 0.57 / 0.27
    05-12 19:44:27.144: E/ActivityManager(71): CPU usage from 21865ms to -1ms ago:
    05-12 19:44:27.144: E/ActivityManager(71):   12% 431/com.Shashank.nchs.Final_exam.hayes: 10% user + 1.9% kernel / faults: 1929 minor 1 major
    05-12 19:44:27.144: E/ActivityManager(71):   0.4% 176/logcat: 0.2% user + 0.2% kernel
    05-12 19:44:27.144: E/ActivityManager(71):   1.3% 41/adbd: 0.1% user + 1.1% kernel
    05-12 19:44:27.144: E/ActivityManager(71):   1.3% 71/system_server: 1% user + 0.2% kernel / faults: 102 minor
    05-12 19:44:27.144: E/ActivityManager(71):   0% 124/jp.co.omronsoft.openwnn: 0% user + 0% kernel / faults: 22 minor
    05-12 19:44:27.144: E/ActivityManager(71):   0.4% 141/com.android.launcher: 0.2% user + 0.1% kernel / faults: 50 minor
    05-12 19:44:27.144: E/ActivityManager(71):   0.3% 199/android.process.acore: 0.3% user + 0% kernel / faults: 39 minor
    05-12 19:44:27.144: E/ActivityManager(71):   0.2% 255/com.android.quicksearchbox: 0.1% user + 0% kernel / faults: 315 minor
    05-12 19:44:27.144: E/ActivityManager(71):   0.2% 267/com.android.defcontainer: 0.2% user + 0% kernel / faults: 37 minor
    05-12 19:44:27.144: E/ActivityManager(71):   0% 31/surfaceflinger: 0% user + 0% kernel
    05-12 19:44:27.144: E/ActivityManager(71):   0% 115/com.android.systemui: 0% user + 0% kernel
    05-12 19:44:27.144: E/ActivityManager(71):   0% 136/com.android.phone: 0% user + 0% kernel / faults: 3 minor
    05-12 19:44:27.144: E/ActivityManager(71): 34% TOTAL: 24% user + 9.1% kernel + 0.5% softirq
    05-12 19:44:27.144: E/ActivityManager(71): CPU usage from 1008ms to 1562ms later:
    05-12 19:44:27.144: E/ActivityManager(71):   53% 431/com.Shashank.nchs.Final_exam.hayes: 48% user + 5% kernel / faults: 265 minor
    05-12 19:44:27.144: E/ActivityManager(71):     32% 431/inal_exam.hayes: 30% user + 2.5% kernel
    05-12 19:44:27.144: E/ActivityManager(71):     8.7% 434/GC: 8.7% user + 0% kernel
    05-12 19:44:27.144: E/ActivityManager(71):     2.5% 437/Compiler: 2.5% user + 0% kernel
    05-12 19:44:27.144: E/ActivityManager(71):     1.2% 433/HeapWorker: 1.2% user + 0% kernel
    05-12 19:44:27.144: E/ActivityManager(71):   5.4% 71/system_server: 3.6% user + 1.8% kernel / faults: 5 minor
    05-12 19:44:27.144: E/ActivityManager(71):     5.4% 95/InputDispatcher: 1.8% user + 3.6% kernel
    05-12 19:44:27.144: E/ActivityManager(71):     1.8% 76/Compiler: 1.8% user + 0% kernel
    05-12 19:44:27.144: E/ActivityManager(71):   5.4% 176/logcat: 3.6% user + 1.8% kernel
    05-12 19:44:27.144: E/ActivityManager(71):   3.7% 371/com.svox.pico: 3.7% user + 0% kernel / faults: 26 minor
    05-12 19:44:27.144: E/ActivityManager(71):     2.4% 371/com.svox.pico: 2.4% user + 0% kernel
    05-12 19:44:27.144: E/ActivityManager(71):   3.6% 41/adbd: 1.8% user + 1.8% kernel
    05-12 19:44:27.144: E/ActivityManager(71):     7.2% 41/adbd: 3.6% user + 3.6% kernel
    05-12 19:44:27.144: E/ActivityManager(71):     3.6% 161/adbd: 1.8% user + 1.8% kernel
    05-12 19:44:27.144: E/ActivityManager(71):     1.8% 162/adbd: 0% user + 1.8% kernel
    05-12 19:44:27.144: E/ActivityManager(71):   1.1% 267/com.android.defcontainer: 1.1% user + 0% kernel
    05-12 19:44:27.144: E/ActivityManager(71): 100% TOTAL: 85% user + 14% kernel

when no input given and alert dialog not fixing the error 当未提供任何输入并且警报对话框无法修复错误时

because currently you are returning AlertDialog.Builder instance from errormessage() method and not calling AlertDialog.show() for showing Dialog. 因为当前您是从errormessage()方法返回AlertDialog.Builder实例,而不是调用AlertDialog.show()来显示Dialog。 so you will need to alert.show() to show AlertDialog . 因此,您需要alert.show()来显示AlertDialog do it as: 这样做:

      try{
          guiChanged();
        }
        catch(Exception e)
        {
            e.printStackTrace();
            AlertDialog alert =errormessage();
             alert.show(); //<<< show AlertDialog here
        }

There are better ways to get sure that there is an input at Your editText etCash. 有更好的方法来确保您的editText etCash有输入。 I don´t know exactly what You are trying to do, but if the user should give some input into the etCash, You can get sure, that there is only a number input by setting this attributes to Your etCash inside xml layout. 我不知道您到底想做什么,但是如果用户应该向etCash中输入一些信息,则可以确定,通过将此属性设置为XML布局内的Your etCash,只有数字输入。 For example: 例如:

        android:inputType="number"

or 要么

        android:inputType="numberDecimal"

or 要么

        android:inputType="numberSigned"

this depends on what You want. 这取决于您想要什么。 I think in Your case, "numberDecimal" is the best way. 我认为就您而言,“ numberDecimal”是最好的方法。

Also, You can check before parsing: 另外,您可以在解析之前检查以下内容:

     if(etCash.getText().toString().length()<1){
              //show warning
     }

If you want to check if input is a number: 如果要检查输入是否为数字:

    private boolean isNumeric(String str){

for (char c : str.toCharArray())
    {
    if (!Character.isDigit(c)) return false;
     }
   return true;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM