无法在片段中显示选项卡

问题描述:

我针对的是平板电脑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> 

请帮我解决这个问题....

不确定您的内容与 新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

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