某些设备上的TabHost应用程序崩溃仅适用于Android 4.0.3-4.0.4

时间:2015-06-18 17:39:07

标签: android android-layout android-tabhost crash

应用仅针对Android 4.0.3-4.0.4在部分设备上标记。 IDE Eclipse。 日志在下面。 如果设定高度最大为30dp,我可以防止崩溃。

layout\my_space_tabbed.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/anudroid"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@drawable/fond"
              android:gravity="center_horizontal"
              android:orientation="vertical">

    <LinearLayout
            android:id="@+id/top_menu"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="@drawable/header">
    </LinearLayout>

    <RelativeLayout android:layout_width="match_parent"
                    android:layout_height="match_parent">

        <include layout="@layout/no_license"/>

        <TabHost
                android:id="@android:id/tabhost"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                <LinearLayout android:layout_width="match_parent"
                              android:layout_height="48dp"
                              android:background="@drawable/header"
                              android:gravity="center">
                    <TabWidget
                            android:id="@android:id/tabs"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"/>
                </LinearLayout>

                <FrameLayout
                        android:id="@android:id/tabcontent"
                        android:layout_width="match_parent"
                        android:layout_height="0dip"
                        android:layout_weight="1" />

            </LinearLayout>
        </TabHost>
    </RelativeLayout>
</LinearLayout>

带标签的代码:

 public class MySpaceTabbedPanelActivity extends TabActivity {    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_space_tabbed);
        // tabs

        addTab(getString(R.string.Index), IndexMySpaceGroup.class);
        addTab(getString(R.string.Tees), MySpaceTeesGroup.class);
        addTab(getString(R.string.Friendly_games), FriendlyGamesGroup.class);

        final int white = MySpaceTabbedPanelActivity.this.getResources().getColor(R.color.WHITE_COLOR);
        TextView tv = (TextView) getTabHost().getTabWidget().getChildAt(0).findViewById(R.id.title);
        tv.setTextColor(white);
        getTabHost().setOnTabChangedListener(new OnTabChangeListener() {

            @Override
            public void onTabChanged(String tabId) {
                TabHost tabHost = getTabHost();
                for(int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
                    TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(R.id.title); //unselected
                    tv.setTextColor(MySpaceTabbedPanelActivity.this.getResources().getColor(R.color.black_color));
                }

                int tabIndex = 0;
                if(tabId.equals("tab" + getString(R.string.Index))) {
                    tabIndex = 0;
                } else if(tabId.equals("tab" + getString(R.string.Tees))) {
                    tabIndex = 1;
                } else if(tabId.equals("tab" + getString(R.string.Friendly_games))) {
                    tabIndex = 2;
                }
                TextView tv = (TextView) tabHost.getTabWidget().getChildAt(tabIndex).findViewById(R.id.title);
                tv.setTextColor(white);
            }
        });

    }


    @Override
    protected void onResume() {
        super.onResume();

        // >> topMenu
        int[] topMenuButtons;
        if (Content.from(this).hasLogonPlayer()){
            findViewById(android.R.id.tabhost).setVisibility(View.VISIBLE);
            findViewById(R.id.no_license1).setVisibility(View.INVISIBLE);
            topMenuButtons = new int[]{R.id.UserButton, R.id.updateButton, R.id.infoButton};
        }else{
            findViewById(android.R.id.tabhost).setVisibility(View.INVISIBLE);
            findViewById(R.id.no_license1).setVisibility(View.VISIBLE);
            topMenuButtons = new int[]{R.id.signupButton, R.id.infoButton};
        }

        LinearLayout topMenu = (LinearLayout)findViewById (R.id.top_menu);
        topMenu.removeAllViews();
        topMenu.addView(new TopMenuComponent(this, topMenuButtons) {
            @Override
            public void onUpdate() {
                new AsyncTask<Object, Object, Object>() {
                    private ProgressDialog progressDialog;
                    @Override
                    protected void onPreExecute() {
                        progressDialog = ProgressDialog.show(getTabGroup(), "", getString(R.string.update));
                    }
                    @Override
                    protected Object doInBackground(Object... objects) {
                        getContent().queryPlayerInfo();
                        getContent().queryFriendlyGames();
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Object o) {
                        getContent().onPostExecute();
                        progressDialog.dismiss();
                    }
                }.execute();
            }
        });
        // <

    }

    private void addTab(String labelId, Class<? extends Activity> c){
        TabHost tabHost = getTabHost();
        Intent intent = new Intent(this, c);
        TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator_my_space, getTabWidget(), false);
        TextView title = (TextView) tabIndicator.findViewById(R.id.title);
        title.setText(labelId);

        spec.setIndicator(tabIndicator);
        spec.setContent(intent);
        tabHost.addTab(spec);
    }



    private TabGroup getTabGroup() {
        return getIntent().getParcelableExtra("tabGroup");
    }

    private Content getContent(){
        return Content.from(this);
    }       
}

记录崩溃:

java.lang.StackOverflowError
at android.graphics.Paint.getTextRunAdvances(Paint.java:1785)
at android.graphics.Paint.getTextRunAdvances(Paint.java:1758)
at android.text.TextLine.handleText(TextLine.java:930)
at android.text.TextLine.handleRun(TextLine.java:1153)
at android.text.TextLine.measureRun(TextLine.java:589)
at android.text.TextLine.measure(TextLine.java:453)
at android.text.TextLine.metrics(TextLine.java:427)
at android.text.Layout.getLineExtent(Layout.java:943)
at android.text.Layout.draw(Layout.java:406)
at android.text.BoringLayout.draw(BoringLayout.java:400)
at android.widget.TextView.onDraw(TextView.java:5307)
at android.view.View.draw(View.java:11120)
at android.view.View.getDisplayList(View.java:10559)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.draw(View.java:11123)
at android.view.View.getDisplayList(View.java:10559)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.draw(View.java:11123)
at android.view.View.getDisplayList(View.java:10559)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.getDisplayList(View.java:10557)
at android.view.ViewGroup.drawChild(ViewGroup.java:2864)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2503)
at android.view.View.draw(View.java:11123)
at android.widget.FrameLayout.draw(FrameLayout.java:450)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2276)
at android.view.View.getDisplayList(View.java:10559)
at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:876)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2092)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1816)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4517)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

当正在运行的程序的堆栈深度超过某个平台或VM特定限制时,抛出

java.lang.StackOverflowError。通常情况下,这只会在程序变为无限递归时发生,但也可能在正确编写(但深度递归)的程序中发生。

  1. 更改onPreExecute()方法:

    progressDialog = ProgressDialog.show(getTabGroup(), "", getString(R.string.update));
    
  2. progressDialog = ProgressDialog.show(MySpaceTabbedPanelActivity.this, "", getString(R.string.update));
    
    1. 在您的代码中,您在asyncTask方法中调用了onResume(),因此可能会出现递归发生和VM抛出错误。 进行一次asynctask调用并从onResume()方法中分离asynctask。