2012-10-24 113 views
2

我的應用程序是輸入金額,計算金額並顯示金額。Android:按鈕點擊不起作用

我使用的Android SDK 16.當我進入量,並計算按鈕點擊我收到

SPAN_EXCLUSIVE_EXCLUSIVE跨度不能有長度爲零。

我的動態類

public class ChapterTwo extends Activity { 

public static final String tag = "Chapter 2"; 

static{ 
    StrictMode.ThreadPolicy policy = new  
     StrictMode.ThreadPolicy.Builder().permitAll().build(); 

    StrictMode.setThreadPolicy(policy); 
} 




@SuppressWarnings("unused") 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_chapter_two); 

    final EditText mealpricefield = (EditText) findViewById(R.id.mealprice); 
    final TextView answerfield = (TextView) findViewById(R.id.answer); 
    final Button button = (Button) findViewById(R.id.calculate); 

      button.setOnClickListener(new Button.OnClickListener() { 

      public void onClickView(View v) { 
      try { 
       Log.i(tag, "onClick Invoked"); 
       // grab the meal price from UI 
       String mealprice = mealpricefield.getText().toString(); 
       Log.i(tag, "meal price is [" + mealprice + "]"); 
       String answer = ""; 
       // check to see if meal price contains $ 
       if (mealprice.indexOf("$") == -1) { 
        mealprice = "$" + mealprice; 
       } 

       float fmp = 0.0F; 
       // get currency formatter; 
       NumberFormat nf = java.text.NumberFormat 
         .getCurrencyInstance(); 
       // grab the input meal price 
       fmp = nf.parse(mealprice).floatValue(); 
       // let's give a nice tip 
       fmp *= 1.2; 
       Log.i(tag, " Total meal price Unformatted is [" + fmp + "]"); 
       // format our result 
       answer = " Full Price, Including the 20% Tip is " 
         + nf.format(fmp); 
       answerfield.setText(answer); 
       Log.i(tag, " On Click Complete"); 

      } catch (java.text.ParseException e) { 

       Log.i(tag, "Parse Exception caught"); 
       answerfield.setText("Failed to Parse the Exception"); 
      } 

      catch (Exception e) { 
       Log.e(tag, "Failed To calculate the tip" + e.getMessage()); 
       e.printStackTrace(); 
       answerfield.setText(e.getMessage()); 

      } 

     } 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 

     } 
    }); 
}} 

我的佈局是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Chapter 2 Android Tip Calculator" 
    tools:ignore="HardcodedText" /> 

<EditText 
    android:id="@+id/mealprice" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:inputType="text"> 

    <requestFocus /> 
</EditText> 

<Button 
    android:id="@+id/calculate" 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Calculate Tip" /> 

<EditText 
    android:id="@+id/answer" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" /> 

我收到以下錯誤日誌貓

10-23 20:19:26.232: E/Trace(699): error opening trace file: No such file or directory (2) 
10-23 20:19:27.092: D/gralloc_goldfish(699): Emulator without GPU emulation detected. 
10-23 20:19:33.262: W/IInputConnectionWrapper(699): showStatusIcon on inactive InputConnection 
10-23 20:24:22.106: I/Choreographer(699): Skipped 30 frames! The application may be doing too much work on its main thread. 
10-23 20:27:52.824: I/Choreographer(699): Skipped 37 frames! The application may be doing too much work on its main thread. 
10-23 20:31:13.323: E/SpannableStringBuilder(699): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
10-23 20:31:13.323: E/SpannableStringBuilder(699): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
10-23 20:31:32.552: I/Choreographer(699): Skipped 35 frames! The application may be doing too much work on its main thread. 
10-23 20:39:44.723: I/Choreographer(699): Skipped 63 frames! The application may be doing too much work on its main thread. 

回答

1

你應該申報所有Ť他認爲在主類和的onCreate初始化它們()方法:

public class ChapterTwo extends Activity { 

public static final String tag = "Chapter 2"; 
EditText mealpricefield; 
TextView answerfield; 
Button button; 
. 
. 
. 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_chapter_two); 

    mealpricefield = (EditText) findViewById(R.id.mealprice); 
    answerfield = (TextView) findViewById(R.id.answer); 
    button = (Button) findViewById(R.id.calculate); 
. 
. 
. 
} 

而且你已經放置在onClickView(視圖V)應該是內部的onClick(視圖v),你有下面的代碼的代碼:

@Override 
public void onClick(View v) { 
     // TODO Auto-generated method stub 

    } 
+0

S:它工作,我把我的所有代碼保存在onClick(View V)中。它工作沒有任何錯誤。非常感謝。 – alapanil

+0

謝謝Marcin S – alapanil

+0

沒問題!請接受答案。 –

0

一眼望去,這可能是你的問題。

Log.i(tag,「Total meal price Unformatted is [」+ fmp +「]」);

fmp是一個浮點數......你將它添加到一個字符串中。

註釋此行,看看它是否工作。

1

除了您發佈的代碼之外是否還有其他代碼?

這裏有幾件事情,我注意到:

  1. 當你按下按鈕,因爲你正如上文所指出都沒有做在onclick(視圖V)方法什麼您的代碼不會做任何事情。你有一個沒有人打電話的onClickView(View v)函數。你應該將你的代碼從onClickView()移動到onClick(),然後擺脫onClickView()方法(或者你可以從onClick函數調用onClickView)。

  2. 如果您移動代碼如上所述,您的代碼將工作,我甚至現在就自己對其進行測試以進行仔細檢查。 (您在上面的複製粘貼代碼中缺少</LinearLayout >標記,但我認爲您已經擁有了它)。

  3. 您發佈的錯誤日誌顯示來自SpannableStringBuilder的錯誤。但是,您並沒有在代碼中的任何位置使用SpannableStringBuilder,因此a)您有其他代碼或b)此錯誤與您的代碼無關。

當您按下按鈕時會發生什麼?應用程序崩潰了嗎?當你看到問題時,你使用什麼值進行測試?日誌中是否有其他日誌中有您的標記或應用程序名稱?您發佈的日誌不會顯示任何強制關閉日誌或任何與您的應用程序相關的錯誤。

第二個想法,你是說你的問題是,當你按下計算按鈕,什麼都沒有發生?所以你認爲你有一個錯誤,只是認爲你在日誌中看到的錯誤是你的代碼無法工作的原因?如果是這樣的話,您只需將onClickView()中的代碼塊移入onClick()並擺脫onClickView()即可。錯誤日誌來自與您的代碼無關的其他程序。