无法识别MainActivity中的通知片段

时间:2014-09-04 15:01:23

标签: android android-fragments

我想显示由MeSSAGE,ORDERS和PRODUCT组成的tabactivity, 这是主要的代码,我只是不知道为什么它适用于Home片段而不是NotificationsFragment

 private void displayView(int position) {
    // update the main content by replacing fragments
    android.app.Fragment fragment = null;
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;
    case 1:
        fragment = new NotificationsFragment();
        break;
    default:
        break;
    }
if (fragment != null) {
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.frame_container, fragment).commit();

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    mDrawerList.setSelection(position);
    setTitle(navMenuTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
} else {
    // error in creating fragment
    Log.e("MainActivity", "Error in creating fragment");
  }
}

这是Notification Fragment的代码 不知道这个错了什么 请需要一些帮助。

@SuppressWarnings("deprecation")
public class NotificationsFragment extends TabActivity {
    public NotificationsFragment(){}
    // TabSpec Names
   private static final String INBOX_SPEC = "Messages";
   private static final String OUTBOX_SPEC = "Orders";
   private static final String PROFILE_SPEC = "Products";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notifications_fragment);

    TabHost tabHost = getTabHost();

    //Messages Tab
    TabSpec msgSpec = tabHost.newTabSpec(INBOX_SPEC);
    // Tab Icon
    msgSpec.setIndicator(INBOX_SPEC, getResources().getDrawable(R.drawable.msg));
    Intent msgIntent = new Intent(this, MessagesFragment.class);
    // Tab Content
    msgSpec.setContent(msgIntent);

    //Orders Tab
    TabSpec ordersSpec = tabHost.newTabSpec(OUTBOX_SPEC);
    ordersSpec.setIndicator(OUTBOX_SPEC, getResources().getDrawable(R.drawable.order));
    Intent orderIntent = new Intent(this, OrdersFragment.class);
    ordersSpec.setContent(orderIntent);

    //Products Tab
    TabSpec productsSpec = tabHost.newTabSpec(PROFILE_SPEC);
    productsSpec.setIndicator(PROFILE_SPEC, getResources().getDrawable(R.drawable.cart));
    Intent productIntent = new Intent(this, ProductsFragment.class);
    productsSpec.setContent(productIntent);

    //Adding all TabSpec to TabHost
    tabHost.addTab(msgSpec); // Adding Message tab
    tabHost.addTab(ordersSpec); // Adding Orders tab
    tabHost.addTab(productsSpec); // Adding Products tab
  }
}

这是我的Home Fragment只是显示listview

public class HomeFragment extends Fragment{

        public HomeFragment(){}
        ProgressBar feeds_progress;

        LinearLayout llGroupHead, llAddPost;
        Button btnAddPost;

        View createdView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        createdView = inflater.inflate(R.layout.home_fragment, container, false);

        feeds_progress = (ProgressBar) createdView.findViewById(R.id.pbarCenter);

        return createdView;
    }

1 个答案:

答案 0 :(得分:0)

您的NotificationsFragment()正由TabActivity扩展,您正在尝试将其初始化为片段。

private void displayView(int position) {
// update the main content by replacing fragments
android.app.Fragment fragment = null;
switch (position) {
case 0:
    fragment = new HomeFragment();
    break;
case 1:
    fragment = new NotificationsFragment();
    break;
default:
    break;
}

让你的NotificationsFragment扩展片段或你在HomeFragment中做的事情。