2017-07-20 49 views
0

我有一个带有TabLayout和几个片段的MainActivity。当创建应用程序时一切正常,但是,当应用程序在后台运行并返回时,我会得到一个空指针异常。我在Fragments的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); 
} 
} 

大多数情况下,当我从后台返回时,Fragments中调用的方法会打印“NULL !!!”。我怎样才能解决这个问题?

+0

什么是myPreferences?你没有显示你是如何宣布它的。你也可以使用debug来确定什么是exactyl。 –

+0

我编辑了这个问题。我现在添加了整个片段类。 – tomhogenkamp

回答

0

android正在清除内存中的数据以节省内存空间和电源。如果操作系统必须清除后台任务(也请参阅,如果您的后台进程限制在设备上的“设置” - >“开发人员设置” - >“后台进程限制”),以便正常工作,则可能会发生更快的速度与前台应用程序,确保它获得足够的内存,等资源。

因此,为了防止这种情况,

  • 仅在范围上需要的地方,像在那里他们将被用来加载图像对象大型物体。
  • 使用savedInstances可使您的活动恢复到之前的状态。
  • 对于使用savedInstances,您必须使您的对象可序列化。
+0

感谢您的回答,那么我必须将碎片保存到savedInstances中吗?也许你可以澄清它? – tomhogenkamp

+0

编号 像碎片之类的东西必须在适配器的getFragment方法中创建。否则无处。 这会让你创建更多的内存更轻的应用程序。 –

+0

这是你应该做的。确定变量,例如应用程序在哪个部分工作。现在使用'OnPause()'方法,将这些变量保存在savedInstances中。在'OnResume()'方法上,android会提供这些变量。因此,恢复活动中的状态。碎片也一样,保存当前状态并恢复。 例如,您在包含listview的第三个选项卡中,并且您正在对第11项进行操作。因此,在saveInstances中保存3,11和工作标识符,并在继续时,您将移动到第3个选项卡,然后告诉片段进入第11个项目,依此类推。 –

相关问题