无法使用Sherlock使用标签

Mag*_*cky 3 tabs android android-fragments actionbarsherlock

好的,所以我正在尝试使用Sherlock显示多个标签,每个标签用于一个片段.我只有4个类:一个用于我的主要活动,两个用于我的片段,一个用于TabListener.一切都应该没问题(没有Sherlcock我有相同的程序,在4.0设备上工作),所以我无法理解为什么我得到NullPointerException.

这是错误的一部分

05-18 17:46:57.197: E/AndroidRuntime(9312): FATAL EXCEPTION: main
05-18 17:46:57.197: E/AndroidRuntime(9312): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.micky.testing/com.micky.testing.SherlockTestActivity}: java.lang.NullPointerException
...
05-18 17:46:57.197: E/AndroidRuntime(9312): Caused by: java.lang.NullPointerException
05-18 17:46:57.197: E/AndroidRuntime(9312):     at com.micky.testing.MyTabListener.onTabSelected(MyTabListener.java:21)
...
05-18 17:46:57.197: E/AndroidRuntime(9312):     at com.micky.testing.SherlockTestActivity.onCreate(SherlockTestActivity.java:39)
Run Code Online (Sandbox Code Playgroud)

这是我的一个片段:
HomeFragment

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.app.SherlockFragment;

public class HomeFragment extends SherlockFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.homefragment, container, false);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的tabListener:
MyTabListener

public class MyTabListener implements TabListener {

public SherlockFragment fragment;

MyTabListener(SherlockFragment fr) {
    Log.d("MYTAG", "Creating a fragmentListener w/ " + fr);
    this.fragment = fr;
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
    Log.d("TAG", "" + fragment);
    ft.replace(R.id.fragment_container, fragment);
}
Run Code Online (Sandbox Code Playgroud)

}

而我的主要活动是:
SherlockTestActivity

public class SherlockTestActivity extends SherlockActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //We take the support actionbar
    ActionBar ab = getSupportActionBar();
    //We set to navigationmode with tabs
    ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    //we create the tabs
    ActionBar.Tab homeTab = ab.newTab().setText("Home");
    ActionBar.Tab tagsTab = ab.newTab().setText("Tags");

    //We create the fragments
    SherlockFragment homeFragment = new HomeFragment();
    SherlockFragment tagsFragment = new TagFragments();

    //And we set the tabsListener;
    homeTab.setTabListener(new MyTabListener(homeFragment));
    tagsTab.setTabListener(new MyTabListener(tagsFragment));

    Log.d("","" + homeTab);
    ab.addTab(homeTab);
    ab.addTab(tagsTab);
}
Run Code Online (Sandbox Code Playgroud)

好的,所以当我将标签添加到我的操作栏时,似乎会抛出错误.当我没有将TabListener添加到选项卡时,没有错误.代码ft.replace(R.id.fragment_container, fragment);(MyTabListener)似乎是问题,但我无法理解为什么.fragment不为null(在实例化一个新的tabListener时初始化),并且没有理由fragment_container是错误的.

所以,如果有人能设法帮助我在这里!谢谢 !

dym*_*meh 8

你应该扩展SherlockFragmentActivity而不是SherlockActivity.在管理片段时使用片段活动.