2015-03-02 107 views
0

我試圖在Android Studio中構建一個2貨幣轉換器應用程序(GNConverter)。我用onClickLister()創建了一個按鈕來轉換值或顯示Toast消息。當我在手機上運行時,收到消息「不幸GNConverter已停止」。Android Studio - 不幸的是,「appName」已停止

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:paddingTop="30dp" 
    android:paddingBottom="16dp" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/PromptTextView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Nigeria Naira (NGN)." 
     android:textSize="20sp" 
     android:layout_marginBottom="20dp"/> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="numberDecimal" 
     android:layout_marginBottom="20dp" 
     android:hint="0" 
     android:textSize="50sp"/> 

    <TextView 
     android:id="@+id/PromptTextView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Ghanaian New Cedi (GHS)." 
     android:textSize="20sp" 
     android:layout_marginBottom="20dp"/> 

    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/PromptTextView" 
     android:inputType="numberDecimal" 
     android:layout_marginBottom="20dp" 
     android:hint="0" 
     android:textSize="50sp"/> 

    <Button 
     android:id="@+id/btnConvert" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="100dp" 
     android:text="Convert"/> 

</LinearLayout> 

我的活動課:

public class GNConverter extends ActionBarActivity implements View.OnClickListener{ 
    private EditText text1; 
    private EditText text2; 
    private Button btnConvert; 

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

    text1 = (EditText) findViewById(R.id.editText1); 
    text2 = (EditText) findViewById(R.id.editText2); 
    btnConvert = (Button) findViewById(R.id.btnConvert); 

    btnConvert.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    if (v.getId()==R.id.btnConvert){ 
     double a = Double.parseDouble(text1.getText().toString()); 
     if (a>0){ 
      double b = a * 0.016694; 
      text2.setText(String.valueOf(b)); 
     } 
     else if (a<=0) { 
      Toast.makeText(GNConverter.this, "Enter a Value!", Toast.LENGTH_LONG).show(); 

      } 

     } 
    } 
} 
+3

我們可以得到堆棧跟蹤嗎?編輯:youre可能不會在edittext中輸入有效的雙倍數。我建議您先將其過濾掉。 – jvrodrigues 2015-03-02 13:54:44

+0

@jvrodrigues我是新的android工作室,請如何獲得堆棧跟蹤。此外,該應用程序正在運行,但是當該字段爲空時,我按下按鈕 - ***轉換*** ,.它崩潰而不是顯示吐司消息。 – StephAn 2015-03-02 15:36:36

回答

0

閱讀您的意見,我明白了事情的原委後。首先,要獲得堆棧跟蹤,如果您使用的是Android Studio,則必須按Alt-6才能打開logcat並閱讀應用程序中正在發生的事情。或者,您也可以單擊下方欄上的Android按鈕,然後選擇標記爲logcat的選項卡,您將可以更輕鬆地進行調試。

關於你的錯誤 - 你得到一個空指針異常。空指針在程序期望變量被設置時發生,但不是。這個特殊的空指針會發生,因爲當你點擊按鈕的時候EditText中沒有值 - 它是空的 - 那麼你將null值傳遞給方法Double.parseDouble(),它需要一個Double。

要試圖解析它之前避免這種情況,你需要檢查,如果確實有東西在編輯文本:

@Override 
public void onClick(View v) { 
    if (v.getId()==R.id.btnConvert){ 

     if (text1.getText().toString() != null){ 
      try{ 
       double a = Double.parseDouble(text1.getText().toString()); 

       double b = a * 0.016694; 
       text2.setText(String.valueOf(b)); 
      }catch(NumberFormatException nfe){ 
        Toast.makeText(GNConverter.this, "Enter a valid Value!",   Toast.LENGTH_LONG).show(); 
      } 
     }else if (a<=0) { 
      Toast.makeText(GNConverter.this, "Enter a Value!", Toast.LENGTH_LONG).show(); 

     } 

    } 
} 

我還添加了一個try/catch檢查,在一個醜陋的和原始的方式,如果EditText中的值實際上是一個數字,因爲如果試圖解析「hello world」這個詞,你會得到一個NumberFormatException。

希望這會有所幫助。

+0

太棒了!你可以標記它作爲答案,以幫助其他人有同樣的問題。 – jvrodrigues 2015-03-04 16:20:55

相關問題