私は簡単なアンドロイドアプリを開発しました。私がアプリケーションに戻ったとき、なぜgetActivityがnullを返すのですか?
活動:
public class MainActivity extends AppCompatActivity {
ArrayList<Fragment> fgs = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fgs.add(new MyFragment1());
fgs.add(new MyFragment2());
ViewPager viewPager = (ViewPager) findViewById(R.id.container);
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return fgs.get(position);
case 1:
return fgs.get(position);
default:
return null;
}
}
@Override
public int getCount() {
return fgs.size();
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
((MyFragment1) fgs.get(0)).display();
return;
}
}
@Override
public void onPageScrollStateChanged(int state) {}
});
}
}
Fragment1
public class MyFragment1 extends Fragment {
String mString;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.my_fragment, container, false);
mString = "MyFragment1";
((TextView)v.findViewById(R.id.text)).setText(mString);
return v;
}
public void display()
{
Toast.makeText(getActivity(), mString, Toast.LENGTH_SHORT).show();
}
}
Fragment2
public class MyFragment2 extends Fragment {
String mString;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.my_fragment, container, false);
mString = "MyFragment2";
((TextView)v.findViewById(R.id.text)).setText(mString);
return v;
}
}
私はそれのメモリはOSによって解放されている状況の下で、このアプリのbehavirをテストした(私は "作られました。活動をしないでください」)
このような状況の下、このアプリを閉じてアプリに戻ってFragment1にスクロールすると、NullPointerExceptionが発生しました。
私はアプリに戻ると、すべて正常に動作すると思っていました。アプリに戻ると、アプリがやり直すからです。
なぜこれが起こりますか?どのように私はこの問題を解決する?
のErrorLog:
Exception in MessageQueue callback: handleReceiveCallback java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.widget.Toast.<init>(Toast.java:102)
at android.widget.Toast.makeText(Toast.java:260)
at knkj28.com.experiment.MyFragment1.display(MyFragment1.java:33)
at knkj28.com.experiment.MainActivity$2.onPageSelected(MainActivity.java:52)
at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1967)
at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:685)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:669)
at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2284)
at android.view.View.dispatchTouchEvent(View.java:9991)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2663)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2344)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:413)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1810)
at android.app.Activity.dispatchTouchEvent(Activity.java:3102)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:375)
at android.view.View.dispatchPointerEvent(View.java:10211)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4444)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4312)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3859)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3912)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3878)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4005)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3886)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4062)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3859)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3912)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3878)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3886)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3859)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6220)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6194)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6155)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6323)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:331)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:6217)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.
Shutting down VM
--------- beginning of crash
FATAL EXCEPTION: main
Process: knkj28.com.experiment, PID: 6025
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.widget.Toast.<init>(Toast.java:102)
at android.widget.Toast.makeText(Toast.java:260)
at knkj28.com.experiment.MyFragment1.display(MyFragment1.java:33)
at knkj28.com.experiment.MainActivity$2.onPageSelected(MainActivity.java:52)
at android.support.v4.view.ViewPager.dispatchOnPageSelected(ViewPager.java:1967)
at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:685)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:669)
at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2284)
at android.view.View.dispatchTouchEvent(View.java:9991)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2663)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2344)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2669)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2358)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:413)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1810)
at android.app.Activity.dispatchTouchEvent(Activity.java:3102)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:375)
at android.view.View.dispatchPointerEvent(View.java:10211)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4444)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4312)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3859)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3912)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3878)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4005)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3886)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4062)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3859)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3912)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3878)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3886)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3859)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6220)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6194)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6155)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6323)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:331)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:6217)
at java.lang.reflect.Method.invoke(Native Method)
08-16 13:32:16.701 6025-6025/knkj28.com.experiment E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
私はそれを得ました。しかし、なぜ私のコードはフラグメントをアクティビティに関連付けることができませんでしたか?最初に正しく が正常に動作します。 OSがアプリケーションを実行しても、アクティビティとフラグメントの両方が再び作成され、ライフサイクルが最初のように再び開始されるため、正常に動作するはずです。私はsomethigを誤解していますか? – user1692121
これは、フラグメントをアクティビティにバインドする方法によって異なります。それがあなたのコードでどこで起こるかを見てください。また、getActivity()をデバッグし、そこに返されたnullを参照してください。あなたが何か間違っていることがあっても、通常、フラグメント内で言えば、あなたのホストの活動がいつでも(システムによって)殺されるという保証はないし、依然として一部のフラグメントのコード(コールバックなど)が遅れて実行される可能性があります。 – Alessio