Tutorial from google android "HelloTabWidget" has a null pointer error - android

When I view the following xml file in the layout view (eclispse), I got NullPointerException:null message, but when I run the code, it works, can anyone explain this to me why?
<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"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"/>
</LinearLayout>
</TabHost>

The problem is just that the Eclipse Layout view does not support TabHost controls yet.

Related

android:tabStripEnabled="false" doesn't work

I was using tabhost and trying to disable the tab border line. I used this android:tabStripEnabled="false"
in my XML but it doesn't seem to be working, the line was still there, and I try other way like making changes in the style.xml(which I found from stackoverflow) but it doesn't work either. Any idea?
my tab xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<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" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#android:id/tabs" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#FFFFFF"
android:tabStripEnabled="false"
android:layout_gravity="bottom" />
</RelativeLayout>
</TabHost>
</LinearLayout>
You can remove the bottom strip of tabs and divider between tabs by using following statement.
TabHost t;
t.getTabWidget().setStripEnabled(false);
t.getTabWidget().setDividerDrawable(R.drawable.ic_launcher);
It requires minSDK 8 (2.2) at least... Review your manifest.
Be sure that there insn't a line, in your code, setting tab's background (I had this problem ...) ;)

Android tabcontent contains scrollview which hides content behind tabs

I have a layout which contains a Tabhost. The tabs are at the bottom of the screen. The first tab starts an ActivityGroup. The first activity in that ActivityGroup contains a scrollview. When the contents are displayed in the scrollview and you scroll all the way down, the bottom of the scrollview is hidden behind the tabs. Any ideas how to fix this?
When I start the activitygroup, I use this code:
Window window = getLocalActivityManager().startActivity(pActivityClass.getName(), intent);
setContentView(window.getDecorView());
Is the decor view the entire screen? I just want the view to be the tabcontent.
Here is the main layout:
<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">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_alignParentTop="true"/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</TabHost>
Here is the view I want in the tabcontent:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout android:id="#+id/myLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="#+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"/>
... a bunch of other components ...
</RelativeLayout>
</ScrollView>
Fix your layout so the content view fits above the tab widget, instead of filling the entire parent container, which tells it to lay out as the full size of the RelativeLayout and then lay the TabWidget on top. Something more like this:
<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">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#android:id/tabs"/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</TabHost>
I also removed some of the unnecessary items like android:orientation which does nothing inside of a RelativeLayout.
HTH
Optimal result.
android:minWidth= Increase the value of...
<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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<TabWidget
**android:minWidth="480dp"**
android:id="#android:id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TabWidget>
</HorizontalScrollView>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</TabHost>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#android:id/tabs"/>
Here, the key line is,
android:layout_above="#android:id/tabs"
I know there might be a "correct" way to do this, but I just added
<LinearLayout android:layout_width="fill_parent"
android:layout_height="50dp"></LinearLayout>
to the bottom of the xml file I was loading into the tab.
Also user1060374 is right the FrameLayout needs to be above the Tabwidget or the scrollview will hide(be on top of) the tabbar if the contents needs scrolled.
But just doing this won't answer your question, so I added the padding.

top border on FrameLayout when using tabs

I have a screen that uses tabs in my android application. There is a line (like a border) that shows between the tabwidget and the framelayout. I want to get rid of that line, but cant seem to find what it is. I have determined that it is part of the FrameLayout by setting its visibilty to gone. Please refer to the code below:
<?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"
android:background="#fb8c10"
>
<ImageView
android:id="#+id/img_confirm_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/confirm_header"
/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dip"
android:background="#fb8c10"
android:tabStripEnabled="false"
/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/tab_bg"
/>
</LinearLayout>
</TabHost>
This is a link of the current layout and what I'm trying to do
http://img441.imageshack.us/g/screenshot20110116at513.png/
The orange layout is my app, and the gray is what I'm trying to do.
Notice how in the gray screenshot the tab flows into the frame.
Apologies for the links, I can't post images yet :p
Thanks!
I managed to solve this by manipulating the z-index using relative layout. I was able to position the tabs slightly over the frame layout. Refer to code below:
<?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"
android:background="#fb8c10"
>
<ImageView
android:id="#+id/img_confirm_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/tab_header"
/>
<RelativeLayout
android:id="#+id/widget77"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
android:layout_marginTop="33dip"
/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fb8c10"
android:tabStripEnabled="true"
android:layout_above="#android:id/tabcontent"
android:layout_marginBottom="40dip"
/>
</RelativeLayout>
</LinearLayout>
</TabHost>
Just now I came across this issue. However, I also tried to remove the top border of FrameLayout. I did it. but I don't think it is a way to remove the top border. anyway FYI,
Just place an TextView with background white on Border and use AbsoluteLayout.
Eg:
<AbsoluteLayout>
<TabHost
android:layout_x="0dip"
android:layout_y="5dip" ...>
<AbsoluteLayout android:padding="5dip" android:background="#ffffff">
<TabWidget/>
<FrameLayout ....
android:layout_x="0dip" android:layout_y="65dip" />
</AbsoluteLayout>
</TabHost>
<TextView
android:layout_width="fill_parent" android:layout_height="25dip"
android:layout_x="0dip" android:layout_y="65dip"
android:background="#ffffff"/>
</AbsoluteLayout>

Tabhost tutorial broken? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Anyone know what this error mean for android tabs?
I have been trying to do the TabHost tutorial in the SDK but for some reason, is appears to break. On Step 4 I copy and paste the xml code, I get an error that prevents it from inflating properly. the error is:
Error in an XML file: aborting build.
This is in the graphical layout of the xml file.
Error during post inflation process:
TabHost requires a TabWidget with id "android:id/tabs",
View found with id "tabs" is 'com.android.layoutlib.bridge.MockView'
The xml file Itself has this in it.
<?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"
android:padding="5dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" />
</LinearLayout>
</TabHost>
Am I doing something wrong?
Use this template:
<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:id="#+id/tab" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:id="#+id/tab1" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"/>
<LinearLayout android:id="#+id/tab2" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"/>
</FrameLayout>
</LinearLayout>
</TabHost>
See step 5 , your Activity must extend TabActivity, not Activity
Now open HelloTabWidget.java and make it extend TabActivity:
Switch to android target 3.0 or 3.1 in the graphical layout itself. You can find the drop down list on the top right corner
You can use the updated rendering library found here: Android Tools.
Just extract the directories found in \layoutlib\[platform version] to \[sdk dir]\platforms\[platform version] and let it override the layoutlib.

TabHost NullPointerException in layout

I been following the Tab example provided by Google. I am trying to use the XML layout provided to setup a tab layout.
I use this XML layout # http://developer.android.com/guide/tutorials/views/hello-tabwidget.html
<?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">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a tab" />
<TextView
android:id="#+id/textview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is another tab" />
<TextView
android:id="#+id/textview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a third tab" />
</FrameLayout>
</LinearLayout>
</TabHost>
When ever I switch the Layout tab in the Eclipse layout designer I get a NullPointerException: null error inside my Eclipse.
This happens also when I try to drag and drop a TabHost, and then a TabWidget into an empty layout file.
What am I doing wrong ? this seems pretty simple.
Found it has been recorded as an issue already
http://code.google.com/p/android/issues/detail?id=2021
Thanks anyway

Categories

Resources