我的應用程序是輸入金額,計算金額並顯示金額。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.
S:它工作,我把我的所有代碼保存在onClick(View V)中。它工作沒有任何錯誤。非常感謝。 – alapanil
謝謝Marcin S – alapanil
沒問題!請接受答案。 –