单例方法在 OnResum 中给出错误 (Android)

Singleton Method Gives Error in OnResum (Android)

我有一个问题,我不明白为什么会这样。

我有一个片段,在片段中我有如下的 on resume 方法:

@Override
public void onResume(){
    super.onResume();

    System.out.println("number 1");

    int s = ((StarterApplication) this.getActivity().getApplication()).getRefreshNotificationVar();

    System.out.println("number 2");
    s = s + 1;

    System.out.println("number 3");
    ((StarterApplication) this.getActivity().getApplication()).setRefreshNotificationVar(s);

    System.out.println("number 4");
    System.out.println(s);


}

我的单例如下:

private int refreshNotificationVar;

public int getRefreshNotificationVar() {

    System.out.println("check out here");
    return refreshNotificationVar;
}

public void setRefreshNotificationVar(int refreshNotificationVar) {
    this.refreshNotificationVar = refreshNotificationVar;
    System.out.println(refreshNotificationVar+" is my number");

}

我在 activity 的 oncreate 中调用了这个片段一次,如下所示,效果很好。单例方法和 else.

newsFeedFragment fragment2 = new newsFeedFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction2 = getSupportFragmentManager().beginTransaction();
fragmentTransaction2.add(R.id.fragment_container, fragment2, "news_feed_fragment");
fragmentTransaction2.addToBackStack(null);
fragmentTransaction2.commit();

然后我用一个按钮再次调用这个片段,如下所示。但是在这里,当单例方法启动时,我开始出现错误。

newsFeedFragment fragment = new newsFeedFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.hide(getSupportFragmentManager().findFragmentById(R.id.fragment_container));
fragmentTransaction.hide(getSupportFragmentManager().findFragmentByTag("notifications_fragment"));
fragmentTransaction.show(getSupportFragmentManager().findFragmentByTag("news_feed_fragment"));
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
fragment.onResume();

第一次加载片段时,它工作正常,但是当我显示片段并手动启动 onResume 方法时,我开始收到如下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method for android:onClick
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
    at android.view.View.performClick(View.java:3511)
    at android.view.View$PerformClick.run(View.java:14105)
    at android.os.Handler.handleCallback(Handler.java:605)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
    at android.view.View.performClick(View.java:3511) 
    at android.view.View$PerformClick.run(View.java:14105) 
    at android.os.Handler.handleCallback(Handler.java:605) 
    at android.os.Handler.dispatchMessage(Handler.java:92) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:4424) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
    at dalvik.system.NativeStart.main(Native Method) 
 Caused by: java.lang.NullPointerException
    at com.my_last.mylast.newsFeedFragment.onResume(newsFeedFragment.java:223)
    at com.my_last.mylast.MainActivity.go_to_main_view(MainActivity.java:565)
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
    at android.view.View.performClick(View.java:3511) 
    at android.view.View$PerformClick.run(View.java:14105) 
    at android.os.Handler.handleCallback(Handler.java:605) 
    at android.os.Handler.dispatchMessage(Handler.java:92) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:4424) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
    at dalvik.system.NativeStart.main(Native Method) 

有人知道吗?

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        System.out.println("number 1");
        int s = ((MainApplication) this.getActivity().getApplication()).getRefreshNotificationVar();
        System.out.println("number 2");
        s = s + 1;
        System.out.println("number 3");
        ((MainApplication) this.getActivity().getApplication()).setRefreshNotificationVar(s);
        System.out.println("number 4");
        System.out.println(s);
    }

看起来 Activity 实例为空。而不是 onresume 在 onActivityCreated Method 上执行相同的操作。请参考上面的代码。