Ok, so I'm trying to use Sherlock to display multiple tabs, each for one snippet. I have only 4 classes: one for my main activity, two for my fragments and one for TabListener. Everything should be fine (I have exactly the same program without Sherlcock, running on 4.0 devices), so I can not understand why I get this NullPointerException.
Here is part of the error
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)
Here is one of my snippets:
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) {
return inflater.inflate(R.layout.homefragment, container, false);
}
}
Here is my 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) {
Log.d("TAG", "" + fragment);
ft.replace(R.id.fragment_container, fragment);
}
}
And my main activity:
SherlockTestActivity
public class SherlockTestActivity extends SherlockActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar ab = getSupportActionBar();
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab homeTab = ab.newTab().setText("Home");
ActionBar.Tab tagsTab = ab.newTab().setText("Tags");
SherlockFragment homeFragment = new HomeFragment();
SherlockFragment tagsFragment = new TagFragments();
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), , , , . ( tabListener), , fragment_container .
, - ! !