我針對的是平板電腦3.2版本。我在屏幕左側顯示一個列表活動,當用戶點擊列表項時,細節顯示在屏幕的右側。因爲我對每個項目點擊不同的細節佈局,我在屏幕右側顯示一個片段。我有要求顯示在細節部分的選項卡式佈局。我擴展了一個片段,並在oncreateview充滿了一個XML作爲根元素的tabhost。但不知何故片段沒有被誇大。爲每個選項卡我有一個列表視圖display.below是代碼部分...我得到的錯誤是NullPointerException。無法在片段中顯示選項卡
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
public class Projects extends Fragment implements OnTabChangeListener {
private int mCurrentTab;
private TabHost mTabHost;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View tablayout = inflater.inflate(R.layout.projects, null);
mTabHost = (TabHost)tablayout.findViewById(android.R.id.tabhost);
final View v1 = tablayout.findViewById(R.id.webprojects);
final View v2 = tablayout.findViewById(R.id.androidprojects);
final View v3 = tablayout.findViewById(R.id.iphoneprojects);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("WEB").setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
// TODO Auto-generated method stub
return v1;
}
}));
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("ANDROID").setContent(new TabHost.TabContentFactory() {
@Override
public View createTabContent(String tag) {
// TODO Auto-generated method stub
return v2;
}
}));
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("IPHONE").setContent(new TabHost.TabContentFactory() {
@Override
public View createTabContent(String tag) {
// TODO Auto-generated method stub
return v3;
}
}));
return tablayout;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
setRetainInstance(true);
mTabHost.setOnTabChangedListener(this);
mTabHost.setCurrentTab(mCurrentTab);
}
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
FragmentTransaction FrgTra = getFragmentManager().beginTransaction();
if(tabId.equals("WEB"))
{
if(getFragmentManager().findFragmentByTag(tabId) == null)
{
FrgTra.replace(R.id.webprojects, new ProjectsListFragment("WEB"), tabId).commit();
}
}
if(tabId.equals("ANDROID"))
{
if(getFragmentManager().findFragmentByTag(tabId) == null)
{
FrgTra.replace(R.id.androidprojects, new ProjectsListFragment("ANDROID"), tabId).commit();
}
}
if(tabId.equals("IPHONE"))
{
if(getFragmentManager().findFragmentByTag(tabId) == null)
{
FrgTra.replace(R.id.iphoneprojects, new ProjectsListFragment("IPHONE"), tabId).commit();
}
}
}
}
和我使用的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="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0">
<FrameLayout android:id="@+id/webprojects"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</FrameLayout>
<FrameLayout
android:id="@+id/androidprojects"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</FrameLayout>
<FrameLayout
android:id="@+id/iphoneprojects"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</FrameLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
請幫我解決這個問題....
對不起solarsheriff,即使我已經改變我的listfragment列表活動,並試圖它無法正常工作。我已按照你的建議進行了所有更改。我仍然有同樣的錯誤。在android文檔中它說我必須使用tabactivity。但現在已被棄用。好吧忘了我的代碼。我準備改變它。所有我需要的是當我點擊列表中的項目(這是在屏幕的左側)我需要在屏幕右側的選項卡式佈局)我的目標是一個選項卡和版本3.2。如果你可以請建議我笏我應該做到實現它 – vivek 2012-03-11 04:20:08
我不確定如何幫助該vivek,以編程方式設置選項卡沒有爲我工作(版本2.2)。我的建議是爲每個標籤佈局做簡單的hello world示例。從列表中選擇一個項目會使片段在單個活動/佈局中可見,那麼您可以直接模擬tabhost接口而不用直接使用它。 – TheSolarSheriff 2012-03-11 12:09:38
在您自己的CursorAdapter中使用視圖設計模式是一種方法。一個viewholder只是一個數據結構,它不需要調用findViewById()。此外,在構建列表時,可以使用.setTag()將對象分配給包含在列表項目佈局中的視圖。當你處理onListItemClick事件時,List和layout之間的鏈接就會發生。您從列表項目佈局和使用中識別視圖。 getTag()。這有助於根據所點擊的項目定製行爲。 – TheSolarSheriff 2012-03-11 12:16:53