java为什么当我返回应用程序时这些对象是空的?
我有一个主要活动,有一个表格和几个片段。创建应用程序后,一切正常,但是,当应用程序在后台运行并返回时,会出现nullpointer异常。我在片段的OnCreateView方法中创建的对象为null
这是我的FragmentPagerAdapter课程:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private FragmentStrobe fragmentStrobe = null;
private FragmentFlashLight fragmentFlashLight = null;
private FragmentDisco fragmentDiscoLight = null;
private FragmentShaking fragmentShaking = null;
private FragmentPoliceLights fragmentPoliceLights = null;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
if(fragmentDiscoLight == null){
fragmentDiscoLight = new FragmentDisco();
}
return fragmentDiscoLight;
case 1:
if(fragmentStrobe == null){
fragmentStrobe = new FragmentStrobe();
}
return fragmentStrobe;
case 2:
if(fragmentFlashLight == null){
fragmentFlashLight = new FragmentFlashLight();
}
return fragmentFlashLight;
case 3:
if(fragmentShaking == null){
fragmentShaking = new FragmentShaking();
}
return fragmentShaking;
case 4:
if(fragmentPoliceLights == null){
fragmentPoliceLights = new FragmentPoliceLights();
}
return fragmentPoliceLights;
default:
return null;
}
}
@Override
public int getCount() {
return 5;
}
@Override
public CharSequence getPageTitle(int position) {
return null;
}
}
在我的MainActivity中,我在片段顶部有几个按钮。单击此按钮时,我正在调用片段中的一个方法
private void initializeButtonStart(){
buttonStart = (Button) findViewById(R.id.start);
buttonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myPreferences.setBoolPreferences(MyPreferences.MY_PREFS_START,
!myPreferences.getBoolPreferences(MyPreferences.MY_PREFS_START, MyPreferences.MY_PREFS_START_DEFAULT));
setIcon();
((FragmentInterface) mSectionsPagerAdapter.getItem(tabLayout.getSelectedTabPosition())).onRunPermissionChanged();
}
});
}
这是一段代码
public class FragmentDisco extends Fragment implements FragmentInterface {
private static final double IMAGE_RATIO = 1.08158;
private View viewBackground;
private View rootView;
private ImageView imageViewDiscoBall;
private Bitmap bitmap;
private Disco disco;
private MyPreferences myPreferences;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_discolight, container, false);
disco = new Disco(getContext());
myPreferences = new MyPreferences(getContext());
bitmap = BitmapFactory.decodeResource(getContext().getResources(),R.drawable.disco_start);
initializeView();
initializeImageView();
return rootView;
}
private void initializeView(){
viewBackground = getActivity().findViewById(R.id.viewMain);
}
private void initializeImageView(){
imageViewDiscoBall = (ImageView) rootView.findViewById(R.id.discoBall);
imageViewDiscoBall.setImageBitmap(bitmap);
imageViewDiscoBall.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
imageViewDiscoBall.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int width = imageViewDiscoBall.getWidth();
int resultHeight = (int)((float) width * IMAGE_RATIO);
ViewGroup.LayoutParams layoutParams = imageViewDiscoBall.getLayoutParams();
layoutParams.height = resultHeight;
imageViewDiscoBall.setLayoutParams(layoutParams);
}
});
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onStop() {
super.onStop();
stopDisco();
}
@Override
public void onTabSelected() {
if(myPreferences == null || imageViewDiscoBall == null || viewBackground == null || disco == null){
return;
}
if(myPreferences.getBoolPreferences(MyPreferences.MY_PREFS_START, MyPreferences.MY_PREFS_START_DEFAULT) &&
(myPreferences.getIntPreferences(MyPreferences.MY_PREFS_CURRENT_FRAGMENT, MyPreferences.MY_PREFS_CURRENT_FRAGMENT_DEFAULT) == MainActivity.FRAGMENT_DISCO)){
disco.start(viewBackground, imageViewDiscoBall);
}
}
@Override
public void onTabUnselected() {
stopDisco();
}
@Override
public void onRunPermissionChanged() {
if(myPreferences == null || imageViewDiscoBall == null || viewBackground == null || disco == null){
System.out.println("NULLL !!!!!!");
return;
}
if(myPreferences.getBoolPreferences(MyPreferences.MY_PREFS_START, MyPreferences.MY_PREFS_START_DEFAULT)){
disco.start(viewBackground, imageViewDiscoBall);
} else {
stopDisco();
}
}
private void stopDisco(){
if(disco == null || imageViewDiscoBall == null || viewBackground == null){
return;
}
disco.stop();
viewBackground.setBackgroundColor(Color.TRANSPARENT);
imageViewDiscoBall.setImageBitmap(bitmap);
}
}
大多数时候,当我从后台返回时,片段中调用的方法会打印“NULL!!!”。我怎样才能解决这个问题
# 1 楼答案
我建议将功能从onCreate()移到onResume()方法。onCreate()方法仅在创建活动时调用,而onResume()始终在返回活动时调用(已销毁的案例除外)。有关更多信息,请参见此https://developer.android.com/guide/components/activities/activity-lifecycle.html
# 2 楼答案
android正在清除内存中的数据以节省内存空间和电源。如果您的设备内存较低,操作系统必须清除后台任务,则这种情况可能会发生得更快(另请参阅,如果您的后台任务限制在设置中->;开发人员设置->;设备上的后台进程限制中)要正确使用前台应用程序,请确保它有足够的内存等资源
所以为了防止这个,