亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

單擊按鈕后,我的應用程序不斷崩潰

單擊按鈕后,我的應用程序不斷崩潰

慕桂英546537 2022-06-08 16:45:52
我一直在自己編寫應用程序,但是每次單擊一個按鈕時都會遇到一些問題(因為問題出在其 OnCreate 方法中)。我已將其范圍縮小到我認為的原因,似乎是這條線:int desitky_tom = Integer.parseInt(et_tom.getText().toString());我想將String我收到的 et_tom 變量從 aneditText更改為 an int。這可能是原因嗎?如果是,我該如何更改它以使應用程序不再崩潰?這是我的 Logcat 錯誤日志:2019-02-07 16:51:16.272 2794-2794/? E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.example.mariaspocitadlo, PID: 2794java.lang.NumberFormatException: For input string: ""    at java.lang.Integer.parseInt(Integer.java:620)    at java.lang.Integer.parseInt(Integer.java:643)    at com.example.mariaspocitadlo.MainActivity$1.onClick(MainActivity.java:41)    at android.view.View.performClick(View.java:6897)    at android.widget.TextView.performClick(TextView.java:12693)    at android.view.View$PerformClick.run(View.java:26101)    at android.os.Handler.handleCallback(Handler.java:789)    at android.os.Handler.dispatchMessage(Handler.java:98)    at android.os.Looper.loop(Looper.java:164)    at android.app.ActivityThread.main(ActivityThread.java:6944)    at java.lang.reflect.Method.invoke(Native Method)    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)這是其余的代碼:final Button spocitat = (Button) findViewById(R.id.button);    spocitat.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            //editext Toma na desítky Toma            EditText et_tom = (EditText)findViewById(R.id.editText);            int desitky_tom = Integer.parseInt(et_tom.getText().toString());
查看完整描述

2 回答

?
瀟瀟雨雨

TA貢獻1833條經驗 獲得超4個贊

從您的 LogCat 來看,輸入的字符串是"". (LogCat 中的第 3 行)。因此它不是一個數字,不能轉換為int。您應該確保輸入一個數字,以阻止崩潰圍繞您的代碼,try例如catch:


try {

 EditText et_tom = (EditText)findViewById(R.id.editText);

 int desitky_tom = Integer.parseInt(et_tom.getText().toString());

} catch (Exception e) { System.out.println(e.toString()); }

除了循環輸入并確保輸入的文本僅來自數字數組。


查看完整回答
反對 回復 2022-06-08
?
慕萊塢森

TA貢獻1810條經驗 獲得超4個贊

您需要將整數解析包含在 try/catch 塊中以避免NumberFormatException


例如:


int desitky_tat = 0;


try {

     desitky_tat = Integer.parseInt(et_tat.getText().toString());

} catch (NumberFormatException e) {

     //handle exception - if required

}

它崩潰的原因是因為EditText按下按鈕時您可能有空文本或非數字文本,從而導致應用程序崩潰。


查看完整回答
反對 回復 2022-06-08
  • 2 回答
  • 0 關注
  • 96 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號