FragmentActivity.getSupportFragmentManager()'在空对象引用

时间:2016-08-13 23:44:33

标签: android android-fragments nullpointerexception null android-fragmentactivity

每次我尝试点击listview转到下一个活动时,我都会得到一个空对象引用。它应该是第31行。所以这一行" 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);
    }}

这是我的错误:

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

2 个答案:

答案 0 :(得分:2)

myContext为null,因此当您调用myContext.getSupportFragmentManager()时,会出现空指针异常。

活动是一个上下文,因此您不需要保存上下文字段。只需拨打.getSupportFragmentManager()即可。

(哦,并完全删除myContext,以便在findViewById()来电时不会发生同样的崩溃。)

答案 1 :(得分:0)

您可以使用类似以下的内容:

AlertDialog.newInstance(stringValue, new Listeners() {
    @Override
    public void success() {

    }
}).show(((AppCompatActivity) myContext).getSupportFragmentManager(), "AlertDialog");