2012-03-09 70 views
1

我針對的是平板電腦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> 

請幫我解決這個問題....

回答

0

不確定您的內容與 新TabHost.TabContentFactory正確初始化( )撥打 。

我發現填充Tab需要一個活動,可能需要額外提取您的信息,例如光標ID。然後,您可能需要設置選項卡上顯示的文本以及圖標或圖標選擇器,以便選項卡選擇更適合您的用戶體驗。

Resources _res = app.getResources(); 
// Create an Intent to launch an Activity for the tab (to be reused) 
Intent _intent = new Intent().setClass(this, WEB_TabActivity.class); 
    _intent.putExtra("whenNeeded", whenNeeded); 

// Initialize a TabSpec for each tab and add it to the TabHost 
tabHost.addTab(tabHost.newTabSpec("WEB") 
    .setIndicator(_res.getString(R.string.WEB_Displayed_Tab_Text), 
     _res.getDrawable(R.drawable.WEB_icon_selector)) 
    .setContent(_intent)); 

實施例選擇是:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- When selected, use color--> 
    <item android:drawable="@drawable/WEB_ICON_color" 
      android:state_selected="true" /> 
    <!-- When not selected, use grey --> 
    <item android:drawable="@drawable/WEB_ICON_Grey" /> 
</selector> 

你然後代碼WEB_TabActivity任何Activity類,將顯示您的所需的數據。

+0

對不起solarsheriff,即使我已經改變我的listfragment列表活動,並試圖它無法正常工作。我已按照你的建議進行了所有更改。我仍然有同樣的錯誤。在android文檔中它說我必須使用tabactivity。但現在已被棄用。好吧忘了我的代碼。我準備改變它。所有我需要的是當我點擊列表中的項目(這是在屏幕的左側)我需要在屏幕右側的選項卡式佈局)我的目標是一個選項卡和版本3.2。如果你可以請建議我笏我應該做到實現它 – vivek 2012-03-11 04:20:08

+0

我不確定如何幫助該vivek,以編程方式設置選項卡沒有爲我工作(版本2.2)。我的建議是爲每個標籤佈局做簡單的hello world示例。從列表中選擇一個項目會使片段在單個活動/佈局中可見,那麼您可以直接模擬tabhost接口而不用直接使用它。 – TheSolarSheriff 2012-03-11 12:09:38

+0

在您自己的CursorAdapter中使用視圖設計模式是一種方法。一個viewholder只是一個數據結構,它不需要調用findViewById()。此外,在構建列表時,可以使用.setTag()將對象分配給包含在列表項目佈局中的視圖。當你處理onListItemClick事件時,List和layout之間的鏈接就會發生。您從列表項目佈局和使用中識別視圖。 getTag()。這有助於根據所點擊的項目定製行爲。 – TheSolarSheriff 2012-03-11 12:16:53

0

** CURSOR適配器和查看HOLDER格局

/** 
* Adapter that allows us to match product items to layout fragment<p/> 
* 
* @author The Solar Sheriff 
*/ 
public class MyItemListAdapter extends CursorAdapter { 


public final static String TAG = "MyItemListAdapter"; 

/** 
* Constructor from a list of items 
*/ 
    private LayoutInflater _li; 
    private Context   _context; 
    private Cursor   _in; 
    private int   _productName, 
        _count; 

    public OrderItemListAdapter(Context _Context, Cursor _In) { 
     super(_Context, _In, true); 

     _context   = _Context; 
     _li     = LayoutInflater.from(_context); 
     _in     = _In; 

     _productName  = _in.getColumnIndex("product_name");   
     _count    = _in.getCount(); 


} 


@Override 
public Object getItem(int position) { 
    return position; 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

@Override 
public boolean hasStableIds() { 
    return true; 
} 

@Override 
public int getCount() { 
    return _in.getCount(); 
} 

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
    View _view = _li.inflate(R.layout.list_item_layout, null); 
    return _view; 
} 

private ViewHolder mapItemViewHolder(View _v){ 
    ViewHolder _itemHolder = new ViewHolder(); 

    //Map the views to the ViewHolder 
    _itemHolder.productName = (TextView) _v.findViewById(R.id.itemProductName); 

return _itemHolder; 
} 

@Override 
public void bindView(View _cView, final Context _context, final Cursor _c) { 
    ViewHolder _itemHolder; 

    // When convertView is not null, we can reuse it directly, there is no need 
    // to reinflate it. We only inflate a new View when the convertView supplied 
    // by ListView is null. 
    if (_cView == null) { 
     _cView = _li.inflate(R.layout.list_item_layout, null);   
     _itemHolder = mapItemViewHolder(_cView); 
    _cView.setTag(_itemHolder); 
    } 
    else { 
     // Get the ViewHolder back to get fast access 
     // Can also get a view with no tag so check for null 
     if ((_itemHolder = (ViewHolder) _cView.getTag()) == null) { 
       _itemHolder = mapItemViewHolder(_cView); 
       _cView.setTag(_itemHolder); 
     }   
    } 



//*** TEXT VIEWS *** 

    // Bind the data using the holder. 
    _itemHolder.productName.setText(_c.getString(_productName)); 

    for(String _display : TabLayouts) { 
    if (_display.equals("WEB")) _itemHolder.productName.setTag(Integer.toString(R.layout.WEB_layout)); 
    if (_display.equals("IPHONE")) _itemHolder.productName.setTag(Integer.toString(R.layout.IPHONE_layout)); 
    if (_display.equals("ANDROID")) _itemHolder.productName.setTag(Integer.toString(R.layout.ANDROID_layout)); 

    } 

}  

static String TabLayouts = { "WEB","IPHONE","ANDROID"}; 

static class ViewHolder 
{ 
    TextView  productName; 
} 
} 

//您onSelectedItem監聽器

MyList.setOnItemSelectedListener(new OnItemSelectedListener() {    

     @Override 
     public void onItemSelected(AdapterView<?> _item, View _View, int _selection, long _id) { 


     ViewGroup _parent = (ViewGroup)_item.getParent();   
     TextView _productName = (TextView)_parent.findViewById(R.id.itemProductName);  

     int _showLayout = Integer.valueOf(_productName.getTag()); 

     //*** e.g. Inflate your fragment layout *** 


     }  

    }); 

希望這給你一個成功的路子走對了:)

+0

你讓我的要求錯了。我需要我的屏幕右側的選項卡式佈局。當我點擊列表視圖中的一個項目時,該項目位於我的屏幕左側。笏永遠你說我已經實施當我點擊其他列表項目 – vivek 2012-03-11 17:22:05

+0

好吧,好運氣改變標籤沒有用戶事件... :) – TheSolarSheriff 2012-03-12 06:15:01

+0

嘿solarsheriff我在我的執行做了一個愚蠢的錯誤,我dint調用設置( )在tabhost上的功能。現在沒事了。我得到了它的工作。尼韋斯謝謝 – vivek 2012-03-15 12:13:48

相關問題