Android: перемещение вкладок вниз приводит к сбою приложения - PullRequest
0 голосов
/ 09 января 2012

Я использую этот урок http://developer.android.com/resources/tutorials/views/hello-tabwidget.html для создания вкладок, которые открывают новые намерения. однако, вкладки в руководствах расположены сверху, поэтому я следовал этому решению здесь https://stackoverflow.com/a/2710404/301584, чтобы переместить вкладки вниз (в основном, я просто переместил TabWidget, чтобы он был после FrameLayout в xml-файле, измените необходимое layout_height и добавьте layout_weight, как предложено решением по ссылке). проблема в том, что мои приложения всегда будут принудительно закрываться при этом вот ошибка, о которой сообщает 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)

это мой полный код

    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);
    }
}

и мой файл 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>

и мой 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);
     }

}

Ответы [ 3 ]

1 голос
/ 09 января 2012

Попробуйте установить вкладки внизу:

<?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>

И используйте следующий код для основной деятельности (это расширяет 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);
        }
}
0 голосов
/ 09 января 2012
 RelativeLayout rllf = (RelativeLayout) tw.getChildAt(0);

Я думаю, что это должно быть передано в Framelayout в соответствии с вашими журналами.

0 голосов
/ 09 января 2012

Попробуйте это

<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>

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...