我开发了一个简单的Android应用程序。
的活动:
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) {}
});
}
}
片段1
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;
}
}
我在操作系统释放内存的情况下测试了这个应用程序的行为。(我检查了“不要保持活动”。)
在这个情节下,当我关闭这个应用程序并返回应用程序并滚动到Fragment1时,发生了NullPointerException。
我想当我回到应用程序时,一切正常。因为当我回到应用程序时,应用程序重新开始。
为什么会这样?我是如何解决这个问题的?
错误日志:
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)
答案 0 :(得分:0)
你知道如何阅读代码,对吧?当您的代码调用Toast.makeText()初始化Toast类时,堆栈跟踪的前3行告诉您Context.getResources()应用于空引用。看看来源:
public Toast(Context context) {
mContext = context;
mTN = new TN();
mTN.mY = context.getResources().getDimensionPixelSize(
com.android.internal.R.dimen.toast_y_offset);
所以这意味着你将空上下文传递给它。在你身边你打电话:
Toast.makeText(getActivity()
这意味着getActivity()为null,这对我来说并不是一个大惊喜,因为你要求系统强制执行以杀死所有活动。因此,您应该准备好代码,使主机活动为空。
另外,如果您阅读文档:
/**
* Return the {@link FragmentActivity} this fragment is currently associated with.
* May return {@code null} if the fragment is associated with a {@link Context}
* instead.
*/
这意味着您没有将片段绑定到主机活动。请务必阅读properly attach the fragment to the activity的方法,以及here。
答案 1 :(得分:0)
public void display() {
if (mString.isEmpty()) {
mString = getActivity().getClass().getSimpleName();
}
Toast.makeText(getActivity(), mString, Toast.LENGTH_SHORT).show();
}