我有一个带有TabLayout和几个片段的MainActivity。创建应用程序时一切正常,但是,当应用程序在后台运行并返回时,我得到一个nullpointer异常。我在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中,我在Fragments上面有几个按钮。单击此按钮时,我在片段中调用方法;
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 !!!"。我该如何解决这个问题?
答案 0 :(得分:0)
android正在清理内存中的数据以节省内存空间和电量。如果您的内存设备较少,操作系统必须清除后台任务,那么这可能会发生得更快(如果您的背景设置受限于设置 - >开发人员设置 - >您设备上的后台处理限制。)正确使用前台应用程序,确保它获得足够的内存等资源。
为了防止这种情况,
答案 1 :(得分:0)
我建议将功能从onCreate()移到onResume()方法。 onCreate()方法仅在创建活动时调用,而onResume()在返回活动时始终被调用(对于被销毁的案例,则为exept)。有关详细信息,请参阅此https://developer.android.com/guide/components/activities/activity-lifecycle.html