我的应用程序是输入金额,计算金额并显示金额。
我正在使用 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" />
我在日志 cat 中收到以下错误
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.