问题
I have a problem that I could not understand why it is happening.
I have a fragment, and in the fragment I have the on resume method as below:
@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);
}
And my singletons are as below:
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");
}
And I call this fragment once in the oncreate of the activity as below, which works fine. The singleton methods and 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();
And I call this fragment again with a button as below. However here I start to get the error when the singleton method starts.
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();
When the fragment loads for the first time, it works fine, but when I show the fragment and start the onResume method manually, I start to get the error as below:
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)
Does anyone has any idea about this?
回答1:
@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);
}
Looks Like Activity Instance is null . Instead of onresume do the same operation on onActivityCreated Method . Please refer the above code.
来源:https://stackoverflow.com/questions/36617328/singleton-method-gives-error-in-onresum-android