在操作栏中使用带有ProgressBarIndeterminate的自定义视图时,应用程序崩溃了

时间:2013-01-28 05:03:24

标签: android progress-bar android-actionbar

请帮帮我。我有关于customview的问题。如果我的自定义视图中的文本太长,并且显示了ProgressBarIndeterminate,则应用程序崩溃。 这是活动中的onCreateOptionsMenu:

public boolean onCreateOptionsMenu(Menu menu) {
    toggle();
    this.getMenuInflater().inflate(R.menu.main, menu);
    actionBar = getActionBar();
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
    actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_HOME);
    actionBar.setDisplayOptions(0, ActionBar.DISPLAY_USE_LOGO);
    actionBar.setDisplayOptions(0, ActionBar.DISPLAY_HOME_AS_UP);
    View customView = getLayoutInflater().inflate(
            R.layout.action_bar_display_options_custom, null);
    customView.findViewById(R.id.action_home).setOnClickListener(
            new OnClickListener() {

                @Override
                public void onClick(View v) {
                    toggle();
                }
            });
    btnSort = customView.findViewById(R.id.sort_view);
    btnSort.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            onPopupButtonClick(v);
        }
    });
    txtTitle = (TextView) customView.findViewById(R.id.action_title);
    actionBar.setCustomView(customView, new ActionBar.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    actionBar.setDisplayShowCustomEnabled(true);
    // search button
    searchButton = (SearchView) menu.findItem(R.id.action_search)
            .getActionView();
    searchButton.setOnQueryTextListener(this);
    // set title on first
    refreshTitleBar();
    return true;
}

这是自定义视图布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/action_home"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="fitStart"
    android:src="@drawable/btn_home" />

<LinearLayout
    android:id="@+id/sort_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical" >

    <TextView
        android:id="@+id/action_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="start"
        android:maxLines="1"
        android:textColor="@android:color/white" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:src="@drawable/sort_icon" />
</LinearLayout></LinearLayout>

如果我将标题设置得太长,则“setProgressBarIndeterminateVisibility”方法会使应用程序崩溃。这是我的LogCat输出。

01-28 11:50:24.850: W/dalvikvm(29817): threadid=1: thread exiting with uncaught exception (group=0x41f4c300)
01-28 11:50:24.880: E/AndroidRuntime(29817): FATAL EXCEPTION: main
01-28 11:50:24.880: E/AndroidRuntime(29817): java.lang.ArrayIndexOutOfBoundsException: length=61; index=-1
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.text.StaticLayout.calculateEllipsis(StaticLayout.java:756)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.text.StaticLayout.out(StaticLayout.java:720)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.text.StaticLayout.generate(StaticLayout.java:428)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.text.StaticLayout.<init>(StaticLayout.java:140)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.widget.TextView.makeSingleLayout(TextView.java:5884)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.widget.TextView.makeNewLayout(TextView.java:5741)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.widget.TextView.onMeasure(TextView.java:6098)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.View.measure(View.java:15172)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1038)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:576)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.View.measure(View.java:15172)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1038)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:576)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.View.measure(View.java:15172)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at com.android.internal.widget.ActionBarView.onMeasure(ActionBarView.java:974)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.View.measure(View.java:15172)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at com.android.internal.widget.ActionBarContainer.onMeasure(ActionBarContainer.java:173)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.View.measure(View.java:15172)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.View.measure(View.java:15172)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at com.slidingmenu.lib.CustomViewAbove.onMeasure(CustomViewAbove.java:795)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.View.measure(View.java:15172)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.View.measure(View.java:15172)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2431)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.View.measure(View.java:15172)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.Choreographer.doFrame(Choreographer.java:525)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.os.Handler.handleCallback(Handler.java:615)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.os.Looper.loop(Looper.java:137)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at android.app.ActivityThread.main(ActivityThread.java:4931)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at java.lang.reflect.Method.invokeNative(Native Method)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at java.lang.reflect.Method.invoke(Method.java:511)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
01-28 11:50:24.880: E/AndroidRuntime(29817):    at dalvik.system.NativeStart.main(Native Method)
01-28 11:50:28.354: I/Process(29817): Sending signal. PID: 29817 SIG: 9

1 个答案:

答案 0 :(得分:2)

是的,你的问题是android:ellipsize="start"android:maxLines="1"疯狂,即使在Lollipop上这个bug仍然存在。解决方案是设置android:singleLine="true"虽然它已被弃用似乎可以解决问题。

https://code.google.com/p/android/issues/detail?id=33868