I have an app that works consist of simple Drawer Navigation with list of menu. Inside one of the menu, I want to add a page with Tab layout - Swipeable view found on this --this app is running without any errors.
I decide to put a button inside one of the menu, which when its pressed will direct the user into the TabLayout's MainActivity.
Now on my Main Project I have two MainActivity classes, the one is MainActivity.java (this is the NavDrawer's) and MainActivity2.java (this is the TabLayout's) and also I manage to copy-pasted all the required files into my Main Project including the adapters.
I put the button in my HomeFragment.java as follows:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
Button mButton = (Button) rootView.findViewById(R.id.button1);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// here you set what you want to do when user clicks your button,
Intent intent = new Intent(getActivity(), MainActivity2.class);
startActivity(intent);
}
});
return rootView;
}
}
But unfortunately when I click on the button the app crashed showing this logcat
04-08 05:58:52.106: E/AndroidRuntime(1317): FATAL EXCEPTION: main
04-08 05:58:52.106: E/AndroidRuntime(1317): Process: my.jlm, PID: 1317
04-08 05:58:52.106: E/AndroidRuntime(1317): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.jlm/my.jlm.MainActivity2}: java.lang.NullPointerException
04-08 05:58:52.106: E/AndroidRuntime(1317): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
04-08 05:58:52.106: E/AndroidRuntime(1317): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
04-08 05:58:52.106: E/AndroidRuntime(1317): at android.app.ActivityThread.access$700(ActivityThread.java:135)
04-08 05:58:52.106: E/AndroidRuntime(1317): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
04-08 05:58:52.106: E/AndroidRuntime(1317): at android.os.Handler.dispatchMessage(Handler.java:102)
04-08 05:58:52.106: E/AndroidRuntime(1317): at android.os.Looper.loop(Looper.java:137)
04-08 05:58:52.106: E/AndroidRuntime(1317): at android.app.ActivityThread.main(ActivityThread.java:4998)
04-08 05:58:52.106: E/AndroidRuntime(1317): at java.lang.reflect.Method.invokeNative(Native Method)
04-08 05:58:52.106: E/AndroidRuntime(1317): at java.lang.reflect.Method.invoke(Method.java:515)
04-08 05:58:52.106: E/AndroidRuntime(1317): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-08 05:58:52.106: E/AndroidRuntime(1317): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-08 05:58:52.106: E/AndroidRuntime(1317): at dalvik.system.NativeStart.main(Native Method)
04-08 05:58:52.106: E/AndroidRuntime(1317): Caused by: java.lang.NullPointerException
04-08 05:58:52.106: E/AndroidRuntime(1317): at my.jlm.MainActivity2.onCreate(MainActivity2.java:32)
it says the error might caused by MainActivity2.java at line 32 but I don't know what to do with it
this is line 32
viewPager.setAdapter(mAdapter);
here's my complete MainActivity2
public class MainActivity2 extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
Thank you for your kind time.
viewPager
is null
. You initialize it like this:
viewPager = (ViewPager) findViewById(R.id.pager);
findViewById
returns the view if found and null
otherwise.
public View findViewById (int id) Added in API level 1
Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle). Returns
The view if found or null otherwise.
Your problem is that the view is not found, maybe because it was not created (yet).