我有一个问题,我无法理解为什么会这样。
我有一个片段,在片段中我有如下的恢复方法:
@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");
}
我在活动的oncreate中将此片段调用一次,如下所示,效果很好。单身方法和其他方法。
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)
有没有人对此有任何想法?
答案 0 :(得分: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);
}
看起来活动实例为空。而不是onresume对onActivityCreated方法执行相同的操作。请参考上面的代码。