2016-12-16 79 views
0

我試圖表明舉杯當兩個EditText一個主題的是EMPTY防止應用程序從崩潰因的傳遞一個空值到下一個活動Button >>檢查EditText的內容?

這裏,當點擊是我的方法(巴頓的方法):

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 

清單::

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

+0

請向我們顯示您的清單。 –

+0

@PedroOliveira完成 –

+0

請提供完整的堆棧跟蹤。似乎還有另一個錯誤,您可能會從日誌中錯過它。 –

回答

0

這是因爲空值不能被轉換爲double。

「dNewBuy = Double.parseDouble(newBuy.getText()的toString());

dNewSell = Double.parseDouble(newSell.getText()的toString());」

所以修改你的代碼是這樣的。

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

} 
} 
+0

謝謝..這適用於我>> –

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

不工作的人 –

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

}

試試這個,這也許是因爲字符串是空

+0

同樣的結果!!!! –