Ich habe ein Problem, dass ich nicht verstehen konnte, warum es passiert.Singleton-Methode gibt Fehler in OnResum (Android)
Ich habe ein Fragment, und in dem Fragment Ich habe das bei der Wiederaufnahme Methode wie folgt:
@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);
}
Und meine Singletons sind wie folgt:
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");
}
Und ich nenne das Fragment einmal in das Erstellen der Aktivität wie unten, was gut funktioniert. Die Singleton-Methoden und sonst.
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();
Und ich nenne dieses Fragment wieder mit einer Schaltfläche wie folgt. Allerdings beginne ich hier den Fehler zu bekommen, wenn die Singleton-Methode startet.
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();
Wenn das Fragment lädt zum ersten Mal, es funktioniert gut, aber wenn ich manuell das Fragment und starten Sie die onResume Methode zeigen, beginne ich, wie unten um den Fehler zu erhalten:
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 Hat jemand eine Ahnung davon?
Was gibt es in newsFeedFragment.java auf Linie 223? In dieser Zeile haben Sie NullPointerException –
Ist 'this.getActivity()' '' '' zurück? – tianyu
Ja, die Zeile ist 'int s = ((StarterApplication) this.getActivity(). GetApplication()). GetRefreshNotificationVar();' – saner