10

我的应用程序从启动画面开始,然后打开 listactivity 行,单击任何行将打开一个包含 textview、两个按钮(其中一个打开无限画廊,另一个打开自定义对话框)和菜单项(关于、首选项)的活动, 出口)。

此应用程序在姜饼上完美运行,但在运行 jelly bean 的 Galaxy s3 上进行测试时,它可以正常打开,但是当您单击其中一个 listactivity 行时,它似乎忽略了单击。eclipse 上的日志猫显示:

SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 

但是,没有强制关闭(lis​​tactivity 正常滚动,菜单项工作正常,并且)。

更新:
我注意到了这个应用程序:大约一个月前,我用 eclipse 创建了它,名字可以说( ),今天在姜饼和果冻豆上trip测试( ),它工作得很好。trip.apk

但我想将名称从 to 更改,但我trip没有travel使用Refactor。相反,我创建了一个名为应用程序名称的新项目,travel但所有其他内容都与以前的应用程序相同,trip包括类、res,并且在使用 jelly bean 测试时显示:

SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 

更新 3:

我解决了问题:

我将此行添加到我的应用程序中的所有 TextViews 中:

 android:textIsSelectable="true"

那条线导致行忽略点击,

所以我删除了它,现在新旧应用程序都可以在 API 上正常工作。

更新 4:

以上短语与旧问题相关并且已经解决,

现在的问题是:

我的应用程序可以与所有 API 完美配合,但在测试时:

运行果冻豆的galaxy S3也可以完美运行,

但在 eclipse 日志中 cat 显示错误:

 SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero  length

在不影响应用程序性能的情况下,任何解决该错误的想法。

任何解释将不胜感激,谢谢。

我的项目代码:

菜单 :

public class Menu extends ListActivity {

    String classes[] = { "Introduction", "DreamsTrip", "Day one", "Day Two",
            "Day Three", "Day Four", "Day Five", "Conclusion" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        // create background for whole list as image
        ListView lv = getListView();
        lv.setCacheColorHint(0);
        lv.setSelector(android.R.color.transparent);
        lv.setPadding(30, 0, 30, 0);
        lv.setVerticalScrollBarEnabled(false);
        lv.setBackgroundResource(R.drawable.list_background);
        lv.setDivider(new ColorDrawable(0x00000000));
        setListAdapter(new MyArrayAdapter(this, classes));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        String cheese = classes[position];
        try {
            Intent ourIntent;
            if (position > 1 && position < 25) {
                Class ourClass = Class.forName("com.test.demo.MyDay");
                ourIntent = new Intent(Menu.this, ourClass);
                ourIntent.putExtra("cheese", cheese);
            } else {
                Class ourClass = Class.forName("com.test.demo." + cheese);
                ourIntent = new Intent(Menu.this, ourClass);
            }
            startActivity(ourIntent);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public boolean onCreateOptionsMenu(android.view.Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.cool_menu, menu);
        getLayoutInflater().setFactory(new Factory() {

            public View onCreateView(String name, Context context,
                    AttributeSet attrs) {
                if (name.equalsIgnoreCase(
                        "com.android.internal.view.menu.IconMenuItemView")) {
                    try {
                        LayoutInflater li = LayoutInflater.from(context);
                        final View view = li.createView(name, null, attrs);
                        new Handler().post(new Runnable() {

                            public void run() {
                                view.setBackgroundResource(R.drawable.border3);
                                ((TextView) view).setTextSize(25);
                                ((TextView) view).setTypeface(FontFactory
                                        .getBFantezy(getBaseContext()));
                                ((TextView) view).setTextColor(Color.RED);
                            }
                        });
                        return view;
                    } catch (InflateException e) {
                    } catch (ClassNotFoundException e) {
                    }
                }
                return null;
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.aboutUs:
            Intent i = new Intent("com.test.demo.ABOUT");
            startActivity(i);
            break;
        case R.id.preferences:
            Intent p = new Intent("com.test.demo.PREFS");
            startActivity(p);
            break;
        case R.id.exit:
            finish();
            break;
        }
        return false;
    }
}

我的阵列适配器:

public class MyArrayAdapter extends ArrayAdapter<String> {

    private final Activity context;
    private final String[] classes;
    Typeface tf;

    static class ViewHolder {

        public TextView text;
        public ImageView image;
    }

    public MyArrayAdapter(Activity context, String[] classes) {
        super(context, R.layout.row, classes);
        this.context = context;
        this.classes = classes;
        tf = Typeface.createFromAsset(context.getAssets(), "BFantezy.ttf");
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        if (rowView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.row, null);
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) rowView.findViewById(R.id.row_label);
            viewHolder.image = (ImageView) rowView.findViewById(R.id.row_image);
            viewHolder.text.setTypeface(FontFactory.getBFantezy(getContext()));
            rowView.setTag(viewHolder);
        }
        ViewHolder holder = (ViewHolder) rowView.getTag();
        String s = classes[position];
        holder.text.setText(s);
        if (s.equals("Day one")) {
            holder.image.setImageResource(R.drawable.day1);
        }
        if (s.equals("Day Two")) {
            holder.image.setImageResource(R.drawable.day2);
        }
        if (s.equals("Day Three")) {
            holder.image.setImageResource(R.drawable.day3);
        }
        if (s.equals("Day Four")) {
            holder.image.setImageResource(R.drawable.day4);
        }
        if (s.equals("Day Five")) {
            holder.image.setImageResource(R.drawable.day5);
        }
        if (s.equals("Conclusion")) {
            holder.image.setImageResource(R.drawable.day_concl);
        }
        if (s.equals("DreamsTrip")) {
            holder.image.setImageResource(R.drawable.day_trip);
        }
        if (s.equals("Introduction")) {
            holder.image.setImageResource(R.drawable.day_intr);
        }
        return rowView;
    }
}

我的一天:

public class MyDay extends Activity {

    final Context context = this;
    private Button button;
    TextView tv2, tv3, tv4;
    String day;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        Boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.day);
        if (customTitleSupported) {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                    R.layout.custom_title);
        }
        initializeTextViews();
    }

    private void initializeTextViews() {
        tv2 = (TextView) findViewById(R.id.day_tv1);
        tv2.setTypeface(FontFactory.getBFantezy(getBaseContext()));
        tv3 = (TextView) findViewById(R.id.day_tv3);
        tv3.setTypeface(FontFactory.getDroidNaskh(getBaseContext()));
        day = getIntent().getStringExtra("cheese");
        if (day.equalsIgnoreCase("Day One")) {
            tv2.setText(Html.fromHtml(getString(R.string.beginning)));
            tv3.setText(Html.fromHtml(getString(R.string.day1)));
            button = (Button) findViewById(R.id.city_button);
            button.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    // custom dialog
                    final Dialog dialog = new Dialog(context,
                            R.style.cust_dialog);
                    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    dialog.setContentView(R.layout.custom_dialog);
                    // set the custom dialog components - text, image and button
                    TextView text = (TextView) dialog
                            .findViewById(R.id.dialog_text);
                    text.setTypeface(FontFactory.getBFantezy(getBaseContext()));
                    text.setText(Html
                            .fromHtml(getString(R.string.torusim_places_1)));
                    Button dialogButton = (Button) dialog
                            .findViewById(R.id.dialog_Button);
                    dialogButton.setTypeface(FontFactory
                            .getBFantezy(getBaseContext()));
                    // if button is clicked, close the custom dialog
                    dialogButton.setOnClickListener(new OnClickListener() {

                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
                    dialog.show();
                }
            });
        } else if (day.equalsIgnoreCase("Day Two")) {
            tv2.setText(Html.fromHtml(getString(R.string.beginning)));
            tv3.setText(Html.fromHtml(getString(R.string.day2)));
            button = (Button) findViewById(R.id.city_button);
            button.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    // custom dialog
                    final Dialog dialog = new Dialog(context,
                            R.style.cust_dialog);
                    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    dialog.setContentView(R.layout.custom_dialog);
                    // set the custom dialog components - text, image and button
                    TextView text = (TextView) dialog
                            .findViewById(R.id.dialog_text);
                    text.setTypeface(FontFactory.getBFantezy(getBaseContext()));
                    text.setText(Html
                            .fromHtml(getString(R.string.torusim_places_2)));
                    Button dialogButton = (Button) dialog
                            .findViewById(R.id.dialog_Button);
                    dialogButton.setTypeface(FontFactory
                            .getBFantezy(getBaseContext()));
                    // if button is clicked, close the custom dialog
                    dialogButton.setOnClickListener(new OnClickListener() {

                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
                    dialog.show();
                }
            });
        } else if (day.equalsIgnoreCase("Day Three")) {
            tv2.setText(Html.fromHtml(getString(R.string.beginning)));
            tv3.setText(Html.fromHtml(getString(R.string.day3)));
            button = (Button) findViewById(R.id.city_button);
            button.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    // custom dialog
                    final Dialog dialog = new Dialog(context,
                            R.style.cust_dialog);
                    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    dialog.setContentView(R.layout.custom_dialog);
                    // set the custom dialog components - text, image and button
                    TextView text = (TextView) dialog
                            .findViewById(R.id.dialog_text);
                    text.setTypeface(FontFactory.getBFantezy(getBaseContext()));
                    text.setText(Html
                            .fromHtml(getString(R.string.torusim_places_3)));
                    Button dialogButton = (Button) dialog
                            .findViewById(R.id.dialog_Button);
                    dialogButton.setTypeface(FontFactory
                            .getBFantezy(getBaseContext()));
                    // if button is clicked, close the custom dialog
                    dialogButton.setOnClickListener(new OnClickListener() {

                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
                    dialog.show();
                }
            });
        } else if (day.equalsIgnoreCase("Day Four")) {
            tv2.setText(Html.fromHtml(getString(R.string.beginning)));
            tv3.setText(Html.fromHtml(getString(R.string.day4)));
            button = (Button) findViewById(R.id.city_button);
            button.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    // custom dialog
                    final Dialog dialog = new Dialog(context,
                            R.style.cust_dialog);
                    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    dialog.setContentView(R.layout.custom_dialog);
                    // set the custom dialog components - text, image and button
                    TextView text = (TextView) dialog
                            .findViewById(R.id.dialog_text);
                    text.setTypeface(FontFactory.getBFantezy(getBaseContext()));
                    text.setText(Html
                            .fromHtml(getString(R.string.torusim_places_4)));
                    Button dialogButton = (Button) dialog
                            .findViewById(R.id.dialog_Button);
                    dialogButton.setTypeface(FontFactory
                            .getBFantezy(getBaseContext()));
                    // if button is clicked, close the custom dialog
                    dialogButton.setOnClickListener(new OnClickListener() {

                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
                    dialog.show();
                }
            });
        } else if (day.equalsIgnoreCase("Day Five")) {
            tv2.setText(Html.fromHtml(getString(R.string.beginning)));
            tv3.setText(Html.fromHtml(getString(R.string.day5)));
            button = (Button) findViewById(R.id.city_button);
            button.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    // custom dialog
                    final Dialog dialog = new Dialog(context,
                            R.style.cust_dialog);
                    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    dialog.setContentView(R.layout.custom_dialog); // set the
                    // custom dialog components - text, image and button
                    TextView text = (TextView) dialog
                            .findViewById(R.id.dialog_text);
                    text.setTypeface(FontFactory.getBFantezy(getBaseContext()));
                    text.setText(Html
                            .fromHtml(getString(R.string.torusim_places_5)));
                    Button dialogButton = (Button) dialog
                            .findViewById(R.id.dialog_Button);
                    dialogButton.setTypeface(FontFactory
                            .getBFantezy(getBaseContext()));
                    // if button is clicked, close the custom dialog
                    dialogButton.setOnClickListener(new OnClickListener() {

                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });
                    dialog.show();
                }
            });
        }
    }

    public void handleClick(View v) {
        // Create an intent to start the new activity.
        Intent intent = new Intent();
        intent.setClass(this, DayGallery.class);
        intent.putExtra("dayname", day);
        startActivity(intent);
    }
}

日志猫:

D/AbsListView(14159): Get MotionRecognitionManager
D/dalvikvm(14159): GC_FOR_ALLOC freed 81K, 9% free 12164K/13315K, paused 13ms, total  
13ms
I/dalvikvm-heap(14159): Grow heap (frag case) to 14.306MB for 1555216-byte allocation
D/dalvikvm(14159): GC_CONCURRENT freed 2K, 8% free 13681K/14855K, paused 12ms+1ms, 
total 20ms
D/dalvikvm(14159): GC_FOR_ALLOC freed 0K, 8% free 13681K/14855K, paused 10ms, total 
10ms
I/dalvikvm-heap(14159): Grow heap (frag case) to 16.941MB for 2764816-byte allocation
D/dalvikvm(14159): GC_CONCURRENT freed 0K, 7% free 16381K/17607K, paused 12ms+2ms, 
total 23ms
D/libEGL(14159): loaded /system/lib/egl/libEGL_mali.so
D/libEGL(14159): loaded /system/lib/egl/libGLESv1_CM_mali.so
D/libEGL(14159): loaded /system/lib/egl/libGLESv2_mali.so
D/(14159): Device driver API match
D/(14159): Device driver API version: 10
D/(14159): User space API version: 10 
D/(14159): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
D/OpenGLRenderer(14159): Enabling debug mode 0
D/dalvikvm(14159): GC_FOR_ALLOC freed 1732K, 16% free 15672K/18439K, paused 19ms, 
total 19ms
E/SpannableStringBuilder(14159): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero 
length
E/SpannableStringBuilder(14159): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero 
length
E/SpannableStringBuilder(14159): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero 
length
E/SpannableStringBuilder(14159): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero 
length
D/dalvikvm(14159): GC_CONCURRENT freed 691K, 13% free 16102K/18439K, paused 13ms+2ms, 
total 27ms
E/SpannableStringBuilder(14159): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero 
length
E/SpannableStringBuilder(14159): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero 
length
E/SpannableStringBuilder(14159): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero 
length
E/SpannableStringBuilder(14159): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero 
length
W/OpenGLRenderer(14159): Shape round rect too large to be rendered into a texture 
(680x12472, max=4096x4096)
W/OpenGLRenderer(14159): Shape round rect too large to be rendered into a texture 
(688x12480, max=4096x4096)
E/SpannableStringBuilder(14159): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero 
length
E/SpannableStringBuilder(14159): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero 
length
E/SpannableStringBuilder(14159): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero 
length
4

3 回答 3

4

要了解这里发生了什么,一切都始于关于TextView应该和不应该是什么的设计决策。根据TextView 的android来源

/**
 * Displays text to the user and optionally allows them to edit it.  A TextView
 * is a complete text editor, however the basic class is configured to not
 * allow editing; see {@link EditText} for a subclass that configures the text
 * view for editing.

这意味着,即使您所做的只是将一段文本放在屏幕上,隐藏在其后面的是全文突出显示、选择和编辑功能。这里发生的情况是,解决其中问题的小调整似乎干扰了其他一些功能,并创建了一些日志行。例如, Nexus 7 错误“SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length”赋予了这种情况更多的权重,其中似乎在某些 android 版本上,可以通过禁用文本(输入)字段中的自动完成来解决这种情况:

View.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS );

这也可以在 XML 中使用 a 的相应标签来完成TextView

android:inputType="none"

有问题的行似乎已经到达 Android 4.1 (Jelly Bean) 的setSpan功能SpannableStringBuilder(例如2.2.3 source vs 4.1.1 source)。日志行由尝试在零长度字符串内设置跨度触发。这就是为什么设置它以便不需要选择文本的(子)部分应该避免此错误,无论是通过上述提示还是使用:

android:textIsSelectable="false"
android:editable="false"

一个更具推测性的解决方案可能是TextView通过将字符“\u200b”(一个未显示的零宽度空白字符)添加到您放入TextViews 中的每个字符串,或者通过覆盖setText(简单地将其添加到字符串的末尾)在自定义中TextView或将其放入您的代码中,以用于发送到TextViews 的每个字符串。

对于那些喜欢深入研究 android 系统并覆盖它的部分的人来说,根据这个 android 源代码,它看起来可以通过以下方式解决这个问题。但是,绝对不能保证它会起作用。真正勇敢的(傻逼)可以为自己写一个完整的 SpannableStringBuilder ....

  1. 创建您自己的形式SpannableStringBuilder来捕获条件。这个例子只是安静地停止,而不是像原来的那样大声停止:

    import android.text.SpannableStringBuilder;
    
    public class MySpannableStringBuilder extends SpannableStringBuilder {
        // Taken from SpannableStringBuilder.java
        private static final int MARK = 1;
        private static final int POINT = 2;
        private static final int START_MASK = 0xF0;
        private static final int END_MASK = 0x0F;
        private static final int START_SHIFT = 4;
    
        MySpannableStringBuilder() {
            super();
        }
    
        MySpannableStringBuilder(CharSequence text) {
            super(text);
        }
    
        MySpannableStringBuilder(CharSequence text, int start, int end) {
            super(text, start, end);
        }
    
        @Override
        public void setSpan(Object what, int start, int end, int flags) {
            // Determine if the error is going to be triggered, if so fail silently
            int flagsStart = (flags & START_MASK) >> START_SHIFT;
            int flagsEnd = flags & END_MASK;
            if (flagsStart == POINT && flagsEnd == MARK && start == end) {
                return;
            }
    
            // All good, so call the real routine
            super.setSpan(what, flagsStart, end, flagsEnd);
        }
    }
    
  2. 创建MyEditableFactory extends Editable.Factory并设置它以返回您的 SpannableStringBuilder:

    import android.text.Editable;
    
    public class MyEditableFactory extends Editable.Factory {
        private static MyEditableFactory sInstance = new MyEditableFactory();
    
        /**
          * Returns this Editable Factory.
          */
        public static Editable.Factory getInstance() {
            return sInstance;
        }
    
        public Editable newEditable(CharSequence source) {
            return new MySpannableStringBuilder(source);
        }
    }
    

除了编写操作系统的自定义版本并将其放在手机上之外,我不确定还有什么可能。

欢迎所有改进建议,以及在各种操作系统情况下使用此代码的反馈。

于 2013-05-22T07:14:29.677 回答
1

您为 textview 使用哪个键盘?对于某些键盘(不是原生的 Andorid),您可能会遇到这个问题。

请参阅:http: //support.swiftkey.net/forums/116693-2-bug-reports/suggestions/2994580-span-exclusive-exclusive-spans-cannot-have-a-zero-

于 2013-05-22T07:13:43.363 回答
1

编译器可能无法解析/使用阿拉伯语(如果我错了,请纠正我)文本。

您可以通过显式设置String以下各项来尝试UTF-8

String[] classes = 
    {
        new String("example1".getBytes(), "UTF-8"),
        new String("example2".getBytes(), "UTF-8")
    };

equals对_onListItemClick

String cheese = new String(classes[position].getBytes(), "UTF-8");

    if(cheese.equals(new String("المقدمة".getBytes(), "UTF-8")))
    {
        cheese = new String("Introduction".getBytes(), "UTF-8");
    }

更好的是,只需制定一个方法。

private Object stringUTF8(final Object string)
{
    try
    {
        return new String(((String) string).getBytes(), "UTF-8");
    }
    catch(UnsupportedEncodingException e)
    {
        e.printStackTrace();

        return "error";
    }
}
于 2013-05-26T21:55:54.030 回答