Android AppcompatActivity with fragments 在从后台加载后崩溃(需要等待几分钟)

Android AppcompatActivity with fragments crashes after bringing from background(need to wait couple minutes)

您好,我遇到了带有片段的 AppCompatActivity 问题

我已经使用处理片段的 AppCompatActivity 扩展了一个 class,当用户发送应用程序后台并等待几分钟然后将应用程序带到前台时会出现问题。除了那种情况,它根本不会崩溃。 (一些不必要的代码还在开发中,请不要关注它们)

任何帮助将不胜感激,我已经在这一点上停留了几天。

如果还有其他需要,我可以更新问题并添加它们

public class SurveyMainFragment extends AppCompatActivity{

    private Toolbar toolbar;
    private Fragment surveyListFragment;
    private Logger Log;


    private SurveyResponseObj surveyResponseObj;


    @Bind(R.id.RelativeSurveyMainFragmentSurveys)
    RelativeLayout surveyTab;

    @Bind(R.id.RelativeSurveyMainFragmentProfile)
    RelativeLayout profileTab;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.survey_main_fragment);

        Log = Logger.getLogger(SurveyMainFragment.class);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setVisibility(View.GONE);

        ButterKnife.bind(this);


        surveyTab.setSelected(true);
        profileTab.setSelected(false);


        Intent i = getIntent();
        if (i != null) {
            surveyResponseObj = i.getParcelableExtra("SurveyResponseList");
        }

        if (surveyResponseObj != null) {

        }


        surveyListFragment = new SurveyListFragment();

        displayFirstTimeUserFragment();

    }

    @OnClick(R.id.RelativeSurveyMainFragmentSurveys)
    void surveysFragment() {
        surveyTab.setSelected(true);
        profileTab.setSelected(false);
    }

    @OnClick(R.id.RelativeSurveyMainFragmentProfile)
    void profileFragment() {
        surveyTab.setSelected(false);
        profileTab.setSelected(true);
    }

    protected void displayFirstTimeUserFragment() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (surveyListFragment.isAdded()) { // if the fragment is already in container
            ft.show(surveyListFragment);
        } else { // fragment needs to be added to frame container
            Bundle bundle = new Bundle();
            bundle.putParcelable("surveyResponse", surveyResponseObj);
            surveyListFragment.setArguments(bundle);
            ft.add(R.id.fragment_survey_place, surveyListFragment, "SurveyListFragment");
        }

        ft.commit();
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i("onSaved"+outState.toString());
//        outState.putInt("someVarA", someVarA);
//        outState.putString("someVarB", someVarB);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.i("onRestoreInstanceState" + savedInstanceState.toString());
        displayFirstTimeUserFragment();
//        someVarA = savedInstanceState.getInt("someVarA");
//        someVarB = savedInstanceState.getString("someVarB");
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}

这是survey_main_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:id="@+id/RelativeSurveysMainFragmentRoot"
    tools:context="com.example.app.PersonalityTestFlow.SurveyMainFragment">

    <include layout="@layout/tool_bar" />

    <include layout="@layout/content_survey_main" />

</android.support.design.widget.CoordinatorLayout>

这是调查的主要内容xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.example.app.PersonalityTestFlow.SurveyMainFragment"
        tools:showIn="@layout/survey_main_fragment">


<fragment
    android:id="@+id/fragment_survey_place"
    android:name="com.example.app.emptyFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/test"
    tools:layout="@layout/empty_fragment" />

 <RelativeLayout
        android:id="@+id/RelativeSurveysFragmentErrorLayout"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_alignParentBottom="true"
        android:background="@color/TransparentBlack">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="20dp"
                android:src="@drawable/tarih" />

            <com.example.CustomComponents.CustomTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:textColor="@color/White"
                app:typeface="Bariol"
                android:lineSpacingExtra="2dp"
                android:text="bla bla"
                android:textSize="24sp" />

        </LinearLayout>


    </RelativeLayout>
   </RelativeLayout>

surveyListFragment代码

public class SurveyListFragment extends BaseFragment implements  View.OnTouchListener {


    private Logger Log;
    private View view;


    @Bind(R.id.recyclerViewSurveyList)
    RecyclerView surveyListRecycler;

    ComplexRecyclerViewSurveyListAdapter SurveyListAdapter;


    private ArrayList<SurveyDataObj> surveyDataList;


    private BusinessService BService;

    private SurveyResponseObj surveyResponseObj;
    private SurveyMainFlow surveyMainFlow;






//    @Bind(R.id.RelativeSurveysFragmentRoot)
//    RelativeLayout rootLayout;


    private RelativeLayout errorLayout;

    private float startY;
    private float lastTranslate = 0.0f;
    private float moveFactorY = 0.0f;
    private android.widget.RelativeLayout.LayoutParams layoutParams;
    private boolean animOk = false;

    float dX, dY;

    private int rootHeight;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
//        if (container == null) {
//            return null;
//        }
        Log = Logger.getLogger(SurveyListFragment.class);
        view = inflater.inflate(R.layout.surveys_fragment, container, false);
        ButterKnife.bind(this, view);
        BService = ((PolenioApplication) getActivity().getApplication()).getBusinessService();
        surveyMainFlow = new SurveyMainFlow(getContext(), BService);


        Bundle bundle = this.getArguments();
        if (bundle != null) {
            surveyResponseObj = bundle.getParcelable("surveyResponse");
            if (surveyResponseObj == null) {
                surveyResponseObj = null;
            } else {
                surveyMainFlow.setSurveyResponseObj(surveyResponseObj);
            }
        } else {
            surveyResponseObj = null;
        }



        initLayout();
        setLayoutInfo();




        return view;
    }





    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }

    private void showErrorDialog() {
        rootHeight =  ((SurveyMainFragment) getActivity()).getRootLayout().getHeight();
//        surveyListRecycler.scrollToPosition(0);
        surveyListRecycler.smoothScrollToPosition(0);
        Log.i(surveyListRecycler.getChildAt(0).getHeight() + " " + surveyListRecycler.getChildAt(0).getWidth() + "root " + rootHeight + "errorLayout" + errorLayout.getHeight());

        errorLayout.getLayoutParams().height = rootHeight - (dp2px(10) + surveyListRecycler.getChildAt(0).getHeight());
        errorLayout.setVisibility(View.VISIBLE);
        TranslateAnimation anim = new TranslateAnimation(0.0f, 0.0f, rootHeight, 0.0f);
        anim.setDuration(300);
        anim.setFillAfter(true);
        errorLayout.startAnimation(anim);

        Log.i("errorLayout" + errorLayout.getHeight());


    }

    private void initLayout() {



        errorLayout = ((SurveyMainFragment) getActivity()).getErrorLayout();

        errorLayout.setVisibility(View.GONE);



        errorLayout.setOnTouchListener(this);


        if (surveyResponseObj != null) {
            surveyDataList = new ArrayList<>(surveyResponseObj.getData());
        } else {
            surveyDataList = new ArrayList<>();
        }
        SurveyListAdapter = new ComplexRecyclerViewSurveyListAdapter(getContext(), surveyDataList, new RecyclerViewListener() {
            @Override
            public void ItemClicked(Object item) {

                Log.i(((SurveyDataObj) item).getTitle());

                    showErrorDialog();

            }
        });

        surveyListRecycler.setAdapter(SurveyListAdapter);
        surveyListRecycler.setLayoutManager(new SnappingLinearLayoutManager(getContext()));
        surveyListRecycler.setItemAnimator(new DefaultItemAnimator());
        surveyListRecycler.setHasFixedSize(true);


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
//        outState.putString("dataGotFromServer", dataGotFromServer);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        dataGotFromServer = savedInstanceState.getString("dataGotFromServer");
    }


    @Override
    public void onSelectionUpdate(float val, float maxval) {
        Log.i("Main" + "Selection update: " + val + ", max: " + maxval);
    }

    @Override
    public void onValueSelected(float val, float maxval) {
        Log.i("Main" + "Selection complete: " + val + ", max: " + maxval);
    }

    public ArrayList<SurveyDataObj> getSurveyDataList() {
        return surveyDataList;
    }

    public void setSurveyDataList(ArrayList<SurveyDataObj> surveyDataList) {
        this.surveyDataList = surveyDataList;
    }


    @Override
    public boolean onTouch(final View view, MotionEvent event) {

        switch (event.getActionMasked()) {

            case MotionEvent.ACTION_DOWN:


                dY = view.getY() - event.getRawY();
                moveFactorY = event.getRawY();

                lastTranslate = view.getY();

                animOk = false;
                break;

            case MotionEvent.ACTION_MOVE:


                if (animOk && moveFactorY - event.getRawY() < 0) {
                    TranslateAnimation anim = new TranslateAnimation(0.0f, 0.0f, lastTranslate, dY + event.getRawY() - view.getY());
                    anim.setDuration(0);
                    anim.setFillAfter(true);
                    view.startAnimation(anim);
                }
                animOk = true;


                lastTranslate = dY + event.getRawY() - view.getY();

                break;
            case MotionEvent.ACTION_UP:

                if (event.getRawY() - moveFactorY > (rootHeight - view.getY()) / 3) {
                    Log.i("Action visibility");
                    TranslateAnimation anim = new TranslateAnimation(0.0f, 0.0f, lastTranslate, rootHeight);
                    anim.setDuration(300);
                    anim.setFillAfter(true);
                    view.startAnimation(anim);
                    anim.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            view.setAnimation(null);
                            view.setVisibility(View.GONE);
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });

                } else {
                    Log.i("Action animation null");
//                    view.getAnimation().cancel();
                    view.setAnimation(null);
//                    view.setVisibility(View.GONE);
                }
//                view.setAnimation(null);
                Log.i("Action up");

                break;
            case MotionEvent.ACTION_CANCEL:
                Log.i("Action cancel");
                break;
            case MotionEvent.ACTION_OUTSIDE:
                Log.i("Action outside");
                break;
            default:
                return false;
        }
        return true;
    }


}

这是我的崩溃日志

03-07 21:07:41.550 11477-11477/com.example.app E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 Process: com.example.app, PID: 11477
                                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.PersonalityTestFlow.SurveyMainFragment}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376)
                                                                     at android.app.ActivityThread.access0(ActivityThread.java:147)
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                     at android.os.Looper.loop(Looper.java:135)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5253)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at java.lang.reflect.Method.invoke(Method.java:372)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
                                                                  Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
                                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
                                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                                                                     at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916)
                                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
                                                                     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267)
                                                                     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129)
                                                                     at com.example.app.PersonalityTestFlow.SurveyMainFragment.onCreate(SurveyMainFragment.java:64)
                                                                     at android.app.Activity.performCreate(Activity.java:5975)
                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376) 
                                                                     at android.app.ActivityThread.access0(ActivityThread.java:147) 
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                     at android.os.Looper.loop(Looper.java:135) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5253) 
                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                     at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900) 
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695) 
                                                                  Caused by: java.lang.IllegalStateException: Fragment com.example.app.PersonalityTestFlow.SurveyListFragment did not create a view.
                                                                     at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2319)
                                                                     at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
                                                                     at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
                                                                     at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
                                                                     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:80)
                                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
                                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
                                                                     at android.view.LayoutInflater.parseInclude(LayoutInflater.java:916) 
                                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:802) 
                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
                                                                     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:267) 
                                                                     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:129) 
                                                                     at com.example.app.PersonalityTestFlow.SurveyMainFragment.onCreate(SurveyMainFragment.java:64) 
                                                                     at android.app.Activity.performCreate(Activity.java:5975) 
                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269) 
                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376) 
                                                                     at android.app.ActivityThread.access0(ActivityThread.java:147) 
                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                     at android.os.Looper.loop(Looper.java:135) 
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5253) 
                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                     at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900) 
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)

我通过更改片段解决了问题

<fragment
    android:id="@+id/fragment_survey_place"
    android:name="com.example.app.emptyFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/test" />

   <FrameLayout
        android:id="@+id/fragment_survey_place"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:layout_above="@+id/test"/>

到目前为止没有崩溃