3

好的,所以我正在尝试使用 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)

这是我的片段之一:
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);
    }
}

这是我的 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);
}

}

我的主要活动:
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);
}

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

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

4

1 回答 1

8

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

于 2012-05-18T16:17:31.463 回答