簡體   English   中英

無法將字符串轉換為int-應用程序停止

[英]Failed to convert string to int - Application stops

我需要很大的幫助..

我正在使用一個簡單的代碼將String轉換為Int,並且當我模擬應用程序停止的語音時,我不明白原因,我嘗試了幾種方式進行轉換,但是當我運行模擬器或在智能手機上運行該應用程序時甚至沒有打開。

package development.idea.tisco.reformaprevidencia;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    // DECLARANDO COMPONENTES

    private RadioGroup grupo;
    private EditText idade;
    private EditText inss;
    private TextView pontos;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // VINCULANDO COMPONENTES

        grupo = (RadioGroup) findViewById(R.id.radio_group_id);
        idade = (EditText) findViewById(R.id.idade_id);
        inss = (EditText) findViewById(R.id.inss_id);
        pontos = (TextView) findViewById(R.id.pontos_id);


        // CAPTURANDO STRING

        String textoIdade = idade.getText().toString();
        String textoInss = inss.getText().toString();

        // CONVERTENDO PARA NUMERO

        int valorIdade = Integer.parseInt(textoIdade);
        int valorInss = Integer.parseInt(textoInss);


    }
}

我希望你能幫助我。

非常感謝。

我敢打賭,當您將一個空String傳遞給Integer.parseInt() ,您會遇到NumberFormatException 您可以通過使用TextUtils.isDigitsOnly()檢查來包裝parseInt語句來避免這種情況。

例如:

int valorIdade = 0;
int valorInss = 0;

if (TextUtils.isDigitsOnly(textoIdade)) {
    valorIdade = Integer.parseInt(texttoIdade);
}

if (TextUtils.isDigitsOnly(textoInss)) {
    valorInss = Integer.parseInt(textoInss);
}

因此,您的完整課程如下:

package development.idea.tisco.reformaprevidencia;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.text.TextUtils;

public class MainActivity extends AppCompatActivity {

    // DECLARANDO COMPONENTES

    private RadioGroup grupo;
    private EditText idade;
    private EditText inss;
    private TextView pontos;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // VINCULANDO COMPONENTES

        grupo = (RadioGroup) findViewById(R.id.radio_group_id);
        idade = (EditText) findViewById(R.id.idade_id);
        inss = (EditText) findViewById(R.id.inss_id);
        pontos = (TextView) findViewById(R.id.pontos_id);


        // CAPTURANDO STRING

        String textoIdade = idade.getText().toString();
        String textoInss = inss.getText().toString();

        // CONVERTENDO PARA NUMERO
        int valorIdade = 0;
        int valorInss = 0;

        if (TextUtils.isDigitsOnly(textoIdade)) {
            valorIdade = Integer.parseInt(texttoIdade);
        }

        if (TextUtils.isDigitsOnly(textoInss)) {
            valorInss = Integer.parseInt(textoInss);
        }
    }
}

嘗試這個

 int valorIdade,valorInss;
   if(!TextUtils.isEmpty(textoIdade)){
      int valorIdade = Integer.parseInt(textoIdade);
    }
          if(!TextUtils.isEmpty(textoInss)){
      valorInss = Integer.parseInt(textoInss);
    }

我建議您檢查android studio中的“ Android Monitor”選項卡,以查看您的應用程序引發了什么異常並導致應用程序停止。
另外,我猜問題出在您使用Integer.parseInt()的地方。 此方法不解析不是全數字的字符串。 例如:

Integer.parseInt("some text"); // throws java.lang.NumberFormatException
Integer.parseInt("1245");  // this one works without error

順便說一句,最好是將代碼的一部分包裝起來,這樣會在try / catch中拋出異常並在某處(例如android監視器)記錄錯誤,以防止應用程序崩潰

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM