简体   繁体   English

Android:将标签移到底部会导致应用程序崩溃

[英]Android: Moving tabs to bottom causes the app to crash

i use this tutorial http://developer.android.com/resources/tutorials/views/hello-tabwidget.html to create tabs which opens new intents.我使用本教程http://developer.android.com/resources/tutorials/views/hello-tabwidget.html创建打开新意图的选项卡。 however, the tabs in the tutorials are located on the top, so I followed this solution here https://stackoverflow.com/a/2710404/301584 to move the tabs to bottom (basically i just move the TabWidget to be after FrameLayout in the xml file, modify the neccessary layout_height and add layout_weight, as suggested by the solution on the link).然而,教程中的选项卡位于顶部,所以我在这里按照这个解决方案https://stackoverflow.com/a/2710404/301584将选项卡移动到底部(基本上我只是将 TabWidget 移动到 FrameLayout 之后xml 文件,修改必要的 layout_height 并添加 layout_weight,如链接上的解决方案所建议的那样)。 the problem is, my apps will always forced close when I did this.问题是,当我这样做时,我的应用程序总是会被强制关闭。 here's the error reported by logcat这是logcat报告的错误

01-09 04:30:09.838: ERROR/AndroidRuntime(336): FATAL EXCEPTION: main
01-09 04:30:09.838: ERROR/AndroidRuntime(336): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.geoflex.trymasak/com.geoflex.trymasak.TryMasaktTabActivity}: java.lang.ClassCastException: android.widget.FrameLayout
01-09 04:30:09.838: ERROR/AndroidRuntime(336): Caused by: java.lang.ClassCastException: android.widget.FrameLayout
01-09 04:30:09.838: ERROR/AndroidRuntime(336):     at com.geoflex.trymasak.TryMasaktTabActivity.onCreate(TryMasaktTabActivity.java:34)

this is my full code这是我的完整代码

    package com.geoflex.trymasak;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;

public class TryMasaktTabActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Reusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, Tab1.class);
        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("tabOne");  
        spec.setContent(intent);  
        spec.setIndicator("Tab One");  
        tabHost.addTab(spec);
        // Squish the tab a little bit horizontally
        tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 40;
        // Bump the text size up
        LinearLayout ll = (LinearLayout) tabHost.getChildAt(0);
        android.widget.TabWidget tw = (android.widget.TabWidget) ll.getChildAt(0);
        RelativeLayout rllf = (RelativeLayout) tw.getChildAt(0);
        TextView lf = (TextView) rllf.getChildAt(1);
        lf.setTextSize(20);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, Tab2.class);
        spec = tabHost.newTabSpec("tabTwo");  
        spec.setContent(intent);  
        spec.setIndicator("Tab Two");
        tabHost.addTab(spec);
        tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 40;
        RelativeLayout rlrf = (RelativeLayout) tw.getChildAt(1);
        TextView rf = (TextView) rlrf.getChildAt(1);
        rf.setTextSize(20);

        tabHost.setCurrentTab(0);
    }
}

and my main.xml file和我的 main.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_weight="1" 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />  
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_weight="0" 
            android:layout_width="fill_parent"
            android:layout_alignParentBottom="true"
            android:layout_height="wrap_content" />

    </LinearLayout>
</TabHost>

and my tab1.java (tab2.java is the same)和我的 tab1.java (tab2.java 是一样的)

package com.geoflex.trymasak;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Tab1 extends Activity {

    @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      TextView tv = new TextView(this);
      tv.setText("This is tab 1");
      setContentView(tv);
     }

}

Try this to set Tabs in the bottom :试试这个在底部设置标签:

<?xml version="1.0" encoding="utf-8"?>
<TabHost 
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android">

 <RelativeLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/bg_main"
        >

         <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@android:id/tabs"
             />

    </RelativeLayout>
</TabHost>

And use the following code for your main activity (thats extends the TabActivity):并为您的主要活动使用以下代码(即扩展 TabActivity):

public class TabTestActivity extends TabActivity {
    /** Called when the activity is first created. */
     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            TabHost tabHost = getTabHost();  // The activity TabHost
            TabHost.TabSpec spec;  // Reusable TabSpec for each tab
            Intent intent;  // Reusable Intent for each tab

                intent = new Intent().setClass(this,Tab1.class);


            spec = tabHost.newTabSpec("projects").setIndicator("Projects",
                              getResources().getDrawable(R.drawable.ic_launcher))
                          .setContent(intent);
            tabHost.addTab(spec);


            intent = new Intent().setClass(this,Tab2.class);

            spec = tabHost.newTabSpec("news").setIndicator("News",
                    getResources().getDrawable(R.drawable.ic_launcher))
                          .setContent(intent);
            tabHost.addTab(spec);


            tabHost.setCurrentTab(0);
        }
}

set in fragment在片段中设置

if(getActivity() != null && isAdded) {

 //do your operation

}

Try this试试这个

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/tabhost" 
android:paddingBottom="@dimen/tab_space_top"
android:layout_width="fill_parent" android:paddingLeft="@dimen/tab_space_gap"
android:layout_height="fill_parent" android:paddingRight="@dimen/tab_space_gap" >
<LinearLayout android:id="@+id/tab_relative_layout" android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/background">
    <FrameLayout android:id="@android:id/tabcontent" android:layout_weight="1" 
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:layout_below="@android:id/tabs"></FrameLayout>
    <TabWidget android:id="@android:id/tabs"
        android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0"></TabWidget>

</LinearLayout>

 RelativeLayout rllf = (RelativeLayout) tw.getChildAt(0);

我认为这应该根据您的日志投射到 Framelayout 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM