无法在片段中显示标签

时间:2012-03-09 08:32:11

标签: android android-fragments android-tabhost

我的目标是平板电脑3.2版。我在屏幕左侧显示listactivity,当用户点击listitem时,屏幕右侧会显示详细信息。因为我点击了每个项目的不同细节布局我在屏幕右侧显示一个片段。我需要在详细信息部分的选项卡式布局上显示。我扩展了一个片段,在oncreateview中用tabhost作为根元素膨胀了一个xml。但不知何故,片段没有膨胀。对于每个选项卡,我有一个列表视图要显示。下面是代码部分...我得到的错误是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>

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

2 个答案:

答案 0 :(得分: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类。

答案 1 :(得分:0)

** CURSOR ADAPTER和VIEW HOLDER PATTERN

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

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


        }       

    });

希望这能为您提供一条成功的道路:)