Страница 1 из 1

Динамическое создание TabHost

Добавлено: 27 июн 2014, 06:34
delphiec
Пока было все в пределах главного Activity, проблем не было, решил сделать чуть сложнее и понеслось...

Пытаюсь создать во втором активити динамически tabhost, но приложение сдало падать при запуске

Код: Выделить всё

TabHost tabHost = new TabHost(this);  
		    tabHost.setup();
			TabHost.TabSpec tabSpec; 
			tabSpec = tabHost.newTabSpec("tag1");
			tabSpec.setIndicator("123 1");
			tabSpec.setContent(R.id.title);
			tabHost.addTab(tabSpec);
			tabSpec = tabHost.newTabSpec("tag2");
			tabSpec.setIndicator("123 2");
			tabSpec.setContent(R.id.title);
			tabHost.addTab(tabSpec);
			tabSpec = tabHost.newTabSpec("tag3");
			tabSpec.setIndicator("123 3");
			tabSpec.setContent(R.id.title);
			tabHost.addTab(tabSpec);

		    llgeneral.addView(tabHost);
llgeneral это основной linearlayout на активити.

Код: Выделить всё

06-27 12:28:40.540: E/AndroidRuntime(6761): FATAL EXCEPTION: main
06-27 12:28:40.540: E/AndroidRuntime(6761): Process: com.example.abc_harmful, PID: 6761
06-27 12:28:40.540: E/AndroidRuntime(6761): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abc_harmful/com.example.abc_harmful.content_activity}: java.lang.RuntimeException: Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'
06-27 12:28:40.540: E/AndroidRuntime(6761): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
06-27 12:28:40.540: E/AndroidRuntime(6761): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
06-27 12:28:40.540: E/AndroidRuntime(6761): 	at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-27 12:28:40.540: E/AndroidRuntime(6761): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-27 12:28:40.540: E/AndroidRuntime(6761): 	at android.os.Handler.dispatchMessage(Handler.java:102)
06-27 12:28:40.540: E/AndroidRuntime(6761): 	at android.os.Looper.loop(Looper.java:136)
06-27 12:28:40.540: E/AndroidRuntime(6761): 	at android.app.ActivityThread.main(ActivityThread.java:5001)
06-27 12:28:40.540: E/AndroidRuntime(6761): 	at java.lang.reflect.Method.invokeNative(Native Method)
06-27 12:28:40.540: E/AndroidRuntime(6761): 	at java.lang.reflect.Method.invoke(Method.java:515)
06-27 12:28:40.540: E/AndroidRuntime(6761): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-27 12:28:40.540: E/AndroidRuntime(6761): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-27 12:28:40.540: E/AndroidRuntime(6761): 	at dalvik.system.NativeStart.main(Native Method)
06-27 12:28:40.540: E/AndroidRuntime(6761): Caused by: java.lang.RuntimeException: Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'
06-27 12:28:40.540: E/AndroidRuntime(6761): 	at android.widget.TabHost.setup(TabHost.java:128)
06-27 12:28:40.540: E/AndroidRuntime(6761): 	at com.example.abc_harmful.content_activity.onCreate(content_activity.java:59)
06-27 12:28:40.540: E/AndroidRuntime(6761): 	at android.app.Activity.performCreate(Activity.java:5231)
06-27 12:28:40.540: E/AndroidRuntime(6761): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-27 12:28:40.540: E/AndroidRuntime(6761): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
06-27 12:28:40.540: E/AndroidRuntime(6761): 	... 11 more

Это кусок XML

Код: Выделить всё

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

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

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" 
                 android:orientation="vertical" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                 android:orientation="vertical"  >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                 android:orientation="vertical"  >
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>

</TabHost>	

С помощью дебага стало ясно, что вылетает на tabHost.setup(); , что может не нравится ему и причем тут TabWidget, в XML он присутствует.

Re: Динамическое создание TabHost

Добавлено: 27 июн 2014, 08:55
delphiec

Код: Выделить всё

TextView text1 = new TextView(this);
		    TextView text2 = new TextView(this);
		    TextView text3 = new TextView(this);
		    text1.setText(MainActivity.narko_about_selected);
		    text2.setText(MainActivity.narko_action_selected);
		    text3.setText(MainActivity.narko_ingridient_selected);
		    TabHost tabHost = new TabHost(this);
		    TabWidget tabWidget = new TabWidget(this);
		    tabWidget.setId(android.R.id.tabs);
		    FrameLayout flout = new FrameLayout(this);
		    flout.setId(android.R.id.tabcontent);
		    LinearLayout lltab = new LinearLayout(this);
		    tabHost.addView(flout);
		    tabHost.addView(tabWidget);
		    tabHost.setup();
			TabHost.TabSpec tabSpec; 
			tabSpec = tabHost.newTabSpec("tag1");
			tabSpec.setIndicator("Описание");
			Log.d("123", "12313212313");
			
			tabSpec.setContent();
			Log.d("123", "123123");
			tabHost.addTab(tabSpec);
			tabSpec = tabHost.newTabSpec("tag2");
			tabSpec.setIndicator("Действие");
			tabSpec.setContent(text2.getId());
			tabHost.addTab(tabSpec);
			tabSpec = tabHost.newTabSpec("tag3");
			tabSpec.setIndicator("Состав");
			tabSpec.setContent(text3.getId());
			tabHost.addTab(tabSpec);
			
		    
		    llgeneral.addView(tabHost);

tabSpec.setContent(); Как сюда засунуть id от динамически созданного text1 getID() не работает, так как динамически создан, да и setID сначала если делать, то тоже не помогает