Mickey Evers Mickey Evers - 3 months ago 59
Android Question

FragmentActivity.getSupportFragmentManager()' on a null object reference

I am getting a null object reference every time I try to click on my listview to go to my next activity. It should be arround line 31. So this line "adapter = new ViewPagerAdapter(myContext.getSupportFragmentManager(),TitlesAuto,Numboftabs);"

package android.evers.mickey.final_eindwerk_android;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

/**
* Created by Mickey on 22/01/16.
*/
public class AutoSelected extends Activity {
ViewPagerAdapter adapter;
ViewPager pager;
SlidingTabLayout tabs;
CharSequence TitlesAuto[]={"Info","Picture", "Sound"};
int Numboftabs = 3;
public FragmentActivity myContext;


@Nullable
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.auto_main_selected);

// Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
adapter = new ViewPagerAdapter(myContext.getSupportFragmentManager(),TitlesAuto,Numboftabs);
pager = (ViewPager) myContext.findViewById(R.id.pagerAuto);

// Assigning ViewPager View and setting the adapter
pager = (ViewPager) myContext.findViewById(R.id.pagerAuto);
pager.setAdapter(adapter);

// Assiging the Sliding Tab Layout View

tabs = (SlidingTabLayout) myContext.findViewById(R.id.tabsAuto);
tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

// Setting Custom Color for the Scroll bar indicator of the Tab View
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.colorAccent);
}
});

// Setting the ViewPager For the SlidingTabsLayout
tabs.setViewPager(pager);
}}


and this is my error:

08-14 01:27:49.230 23389-23389/? I/art: Not late-enabling -Xcheck:jni (already on)
08-14 01:27:49.273 23389-23389/? W/System: ClassLoader referenced unknown path: /data/app/android.evers.mickey.final_eindwerk_android-2/lib/x86
08-14 01:27:49.280 23389-23396/? E/art: Failed writing handshake bytes (-1 of 14): Broken pipe
08-14 01:27:49.280 23389-23396/? I/art: Debugger is no longer active
08-14 01:27:49.384 23389-23403/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
08-14 01:27:49.467 23389-23403/? I/OpenGLRenderer: Initialized EGL, version 1.4
08-14 01:27:49.483 23389-23403/? W/EGL_emulation: eglSurfaceAttrib not implemented
08-14 01:27:49.483 23389-23403/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabf3f100, error=EGL_SUCCESS
08-14 01:27:54.069 23389-23389/android.evers.mickey.final_eindwerk_android W/PathParser: Points are too far apart 4.000000596046461
08-14 01:27:55.273 23389-23389/android.evers.mickey.final_eindwerk_android W/PathParser: Points are too far apart 4.000000596046461
08-14 01:27:55.479 23389-23389/android.evers.mickey.final_eindwerk_android I/System.out: {"id":"1","naam":"test","info":"dit is een test","merk":"porshe","bouwjaar":"0000-00-00","foto":"http:\/\/dtsl.ehb.be\/~mickey.evers\/android\/images\/auto1.png"}
08-14 01:27:55.479 23389-23389/android.evers.mickey.final_eindwerk_android I/System.out: http://dtsl.ehb.be/~mickey.evers/android/images/auto1.png
08-14 01:27:55.479 23389-23389/android.evers.mickey.final_eindwerk_android I/System.out: test
08-14 01:27:55.479 23389-23389/android.evers.mickey.final_eindwerk_android I/System.out: {"id":"2","naam":"testfxfjj","info":"test2","merk":"testen","bouwjaar":"0000-00-00","foto":"http:\/\/dtsl.ehb.be\/~mickey.evers\/android\/images\/auto1.png"}
08-14 01:27:55.481 23389-23389/android.evers.mickey.final_eindwerk_android I/System.out: http://dtsl.ehb.be/~mickey.evers/android/images/auto1.png
08-14 01:27:55.481 23389-23389/android.evers.mickey.final_eindwerk_android I/System.out: testfxfjj
08-14 01:27:55.481 23389-23389/android.evers.mickey.final_eindwerk_android I/System.out: [test, testfxfjj]
08-14 01:27:56.205 23389-23389/android.evers.mickey.final_eindwerk_android D/AndroidRuntime: Shutting down VM
08-14 01:27:56.205 23389-23389/android.evers.mickey.final_eindwerk_android E/AndroidRuntime: FATAL EXCEPTION: main
Process: android.evers.mickey.final_eindwerk_android, PID: 23389
java.lang.RuntimeException: Unable to start activity ComponentInfo{android.evers.mickey.final_eindwerk_android/android.evers.mickey.final_eindwerk_android.AutoSelected}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentManager android.support.v4.app.FragmentActivity.getSupportFragmentManager()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.app.FragmentManager android.support.v4.app.FragmentActivity.getSupportFragmentManager()' on a null object reference
at android.evers.mickey.final_eindwerk_android.AutoSelected.onCreate(AutoSelected.java:31)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
08-14 01:32:56.222 23389-23389/? I/Process: Sending signal. PID: 23389 SIG: 9

Answer

myContext is null, so when you call myContext.getSupportFragmentManager() you get a null pointer exception.

An Activity is a Context so you don't need to hold a context field. Just call .getSupportFragmentManager() instead.

(oh, and remove myContext completely so that you don't have the same crash at the findViewById() calls.)