检查EditText的内容?

时间:2016-12-16 17:18:33

标签: android android-edittext

我试图在两个EditText主题EMPTY时显示Toast,以阻止应用崩溃,因为点击Button>>

时,将空值传递给下一个活动

这是我的方法(Button的方法):

    public void mods(View view) {

    dNewBuy = Double.parseDouble(newBuy.getText().toString());
    dNewSell = Double.parseDouble(newSell.getText().toString());

    String BuyVal = newBuy.getText().toString();
    String SellVal = newSell.getText().toString();

    if((BuyVal.isEmpty()) || (SellVal.isEmpty())) {
        Toast.makeText(this, "ZERO", Toast.LENGTH_SHORT).show();
    } else {
        Intent intent = new Intent(Values.this, calculator.class);
        Bundle b = new Bundle();
        b.putDouble("valueSell", dNewSell);
        b.putDouble("valueBuy", dNewBuy);
        b.putString("flag", flag);
        intent.putExtras(b);
        startActivity(intent);
    }

newSellnewBuy是两个EditTexts ..

} else {}部分工作正常:/

但是,当值为空时,它会给我这个..

java.lang.IllegalStateException: Could not execute method of the activity

Manifest ::

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".calculator" />
    <activity android:name=".About" />
    <activity android:name=".Values"
              android:theme="@android:style/Theme.Holo.Light.Dialog.NoActionBar">
    </activity>
</application>

3 个答案:

答案 0 :(得分:0)

 if((newBuy.isEmpty()) || (newSell.isEmpty())) {
    Toast.makeText(this, "ZERO", Toast.LENGTH_SHORT).show();
}

答案 1 :(得分:0)

public void mods(View view) {

dNewBuy = Double.parseDouble(newBuy.getText().toString());
dNewSell = Double.parseDouble(newSell.getText().toString());

String BuyVal = newBuy.getText().toString();
String SellVal = newSell.getText().toString();
if(BuyVal !=null && SellVal !=null ){
if((BuyVal.isEmpty()) || (SellVal.isEmpty())) {
    Toast.makeText(this, "ZERO", Toast.LENGTH_SHORT).show();
} else {
    Intent intent = new Intent(Values.this, calculator.class);
    Bundle b = new Bundle();
    b.putDouble("valueSell", dNewSell);
    b.putDouble("valueBuy", dNewBuy);
    b.putString("flag", flag);
    intent.putExtras(b);
    startActivity(intent);
}

}

试试这个,这可能是因为字符串为空

答案 2 :(得分:0)

因为空值不能转换为双倍。

&#34; dNewBuy = Double.parseDouble(newBuy.getText()。toString());

dNewSell = Double.parseDouble(newSell.getText()。toString());&#34;

所以请修改你的代码。

public void mods(View view) {

String BuyVal = newBuy.getText().toString();
String SellVal = newSell.getText().toString();

if((BuyVal.isEmpty()) || (SellVal.isEmpty())) {
    Toast.makeText(this, "ZERO", Toast.LENGTH_SHORT).show();
} else {

 dNewBuy = Double.parseDouble(newBuy.getText().toString());
 dNewSell = Double.parseDouble(newSell.getText().toString());

    Intent intent = new Intent(Values.this, calculator.class);
    Bundle b = new Bundle();
    b.putDouble("valueSell", dNewSell);
    b.putDouble("valueBuy", dNewBuy);
    b.putString("flag", flag);
    intent.putExtras(b);
    startActivity(intent);

}
}