问题
I got a problem with my android app. I got a button and and event associated , but when I click the first time an error appears
"spans cannot have zero lenght".
. But when I click the second time, the event onclick runs well.. look at my java code:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button selectAltitude = (Button) findViewById(R.id.buttonAltitude1);
final Button selectAltitude2 = (Button) findViewById(R.id.buttonAltitude2);
selectAltitude2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
String tempAlt = selectAltitude2.getText().toString();
selectAltitude2.setText(selectAltitude.getText().toString());
selectAltitude.setText(tempAlt);
}
});
}
The xml code with the two buttons (a deprecated warning occurs):
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/black">
<Button
android:id="@+id/buttonAltitude1"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="39dp"
android:layout_height="42dp"
android:layout_marginLeft="250dp"
android:layout_x="257dp"
android:layout_y="419dp"
android:background="@drawable/drawable1"
android:focusableInTouchMode="true"
android:gravity="center_vertical"
android:includeFontPadding="false"
android:linksClickable="true"
android:minHeight="10dp"
android:minWidth="64dp"
android:paddingLeft="10dp"
android:paddingRight="5dp"
android:scrollHorizontally="true"
android:text="@string/button_meters"
android:textColor="@android:color/black"
android:textSize="22sp"
android:textStyle="bold" />
<Button
android:id="@+id/buttonAltitude2"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="65dp"
android:layout_height="42dp"
android:layout_marginLeft="250dp"
android:layout_x="295dp"
android:layout_y="419dp"
android:background="@drawable/drawable2"
android:clickable="true"
android:focusableInTouchMode="true"
android:gravity="center_vertical"
android:includeFontPadding="false"
android:linksClickable="true"
android:minHeight="10dp"
android:minWidth="64dp"
android:paddingLeft="15dp"
android:paddingRight="5dp"
android:scrollHorizontally="true"
android:text="@string/button_feet"
android:textColor="@android:color/black"
android:textSize="25sp"
android:textStyle="bold" />
When I click the first time Log cat interacts with:
04-06 20:01:39.865: I/Sensors(6946): sendDelay --- 200000000 04-06
20:01:39.865: D/SensorManager(6946): JNI - sendDelay 04-06
20:01:39.865: I/SensorManager(6946): Set normal delay = true 04-06
20:01:39.890: E/SpannableStringBuilder(6946): SPAN_EXCLUSIVE_EXCLUSIVE
spans cannot have a zero length 04-06 20:01:39.890:
E/SpannableStringBuilder(6946): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot
have a zero length
can you help ne?;)
回答1:
I have run into the same error entries in LogCat. In my case it's caused by the 3rd party keyboard I am using. When I change it back to Android keyboard, the error entry does not show up any more.
This is the most probable cause of this issue as there is also a bug report for SwiftKey:
http://support.swiftkey.net/forums/116693-2-bug-reports/suggestions/2994580-span-exclusive-exclusive-spans-cannot-have-a-zero-
回答2:
To try to debug this error, first go to your android terminal / console and execute this command:
ps | grep THE_ERROR_PID_YOU_GET_(IT_IS_A_NUMBER)
then if the output comes out as your app... it is your app causing the error. Try to look for empty Strings
that you pass into the layout
.
I had this exact same problem and it was my fault as I was passing an empty String
into my layout. After changing the ""
to " "
this error went away.
If you don't get your app from the console output, then it is something else causing it (probably, as others said, the android keyboard)
回答3:
I know I'm a little late. In case someone still needs help with having to click the second time for the the event onclick to run, I removed this:
android:focusableInTouchMode="true"
The onClick event will now work on the first click.
来源:https://stackoverflow.com/questions/15839237/error-span-exclusive-exclusive-spans-cannot-have-a-zero-length