使用ViewPager和FragmentStatePagerAdapter

时间:2017-11-02 11:35:09

标签: android android-fragments android-viewpager fragmentstatepageradapter

添加ViewPager和FragmentStatePagerAdapter之后,新的CrimePagerActivity确实启动了,我可以正常向前滑动,但是如果我尝试滑动到我来自的犯罪,应用程序会冻结一秒钟然后重新启动应用程序返回列表图。

例如,如果我从犯罪3开始,我可以向前移动到4(或向后移动到2),但如果我移回到3,它将冻结并崩溃。 请参阅下面的代码和堆栈跟踪:

CrimePagerActivity.java

 public class CrimePagerActivity extends AppCompatActivity {
    private ViewPager mViewPager;
    private List<Crime> mCrimes;

    private static final String EXTRA_CRIME_ID =
            "com.bignerdranch.android.crime_id";

    public static Intent newIntent(Context packageContext, UUID crimeID) {
        Intent intent = new Intent(packageContext, CrimePagerActivity.class);
        intent.putExtra(EXTRA_CRIME_ID, crimeID);
        return intent;
    }

    @Override
    public void onCreate(Bundle savedInstancestate) {
        super.onCreate(savedInstancestate);
        setContentView(R.layout.activity_crime_pager);

        UUID crimeId = (UUID) getIntent().getSerializableExtra(EXTRA_CRIME_ID);

        mViewPager = (ViewPager) findViewById(R.id.crime_view_pager);

        mCrimes = CrimeLab.get(this).getCrimes();
        FragmentManager fragmentManager = getSupportFragmentManager();

        mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {

            @Override
            public Fragment getItem(int position) {
                Log.d("Fragment", "Position is " + position);
                Crime crime = mCrimes.get(position);
                Log.d("Fragment getItem", "Title is " + crime.getTitle());
                Log.d("Fragment getItem", "Title is " + crime.isSolved());
                return CrimeFragment.newInstance(crime.getId());
            }

            @Override
            public int getCount() {
                return mCrimes.size();
            }
        });

        for (int i = 0; i < mCrimes.size(); i++) {
            if (mCrimes.get(i).getId().equals(crimeId)) {
                mViewPager.setCurrentItem(i);
                break;
            }
        }

    }
}

CrimeFragment.java

    public class CrimeFragment extends Fragment {
       private Crime mCrime;
    private EditText mTitleField;
    private Button mDateButton;
    private CheckBox mSolvedCheckBox;

    private static final String ARG_CRIME_ID = "crime_id";

    public static CrimeFragment newInstance (UUID crime_id) {
        Bundle args = new Bundle();
        args.putSerializable(ARG_CRIME_ID, crime_id);

        CrimeFragment fragment = new CrimeFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        UUID crimeId = (UUID) getArguments().getSerializable(ARG_CRIME_ID);
        mCrime = CrimeLab.get(getActivity()).getCrime(crimeId);

    }// END OF onCreate()

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_crime, container, false);
        mTitleField = (EditText) v.findViewById(R.id.crime_title);
        mTitleField.setText(mCrime.getTitle());
        mTitleField.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                //Intentionally left blank
            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                mTitleField.setText(s.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {
                //Intentionally left blank
            }
        });// END OF adding TextChangeListener to mTitleField

        mDateButton = (Button) v.findViewById(R.id.crime_date);
        mDateButton.setText(mCrime.getDate().toString());
        mDateButton.setEnabled(false);

        mSolvedCheckBox = (CheckBox) v.findViewById(R.id.crime_solved);
        mSolvedCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                mCrime.setSolved(isChecked);
            }
        });// END OF mSolvedCheckBox onCheckListener

        return v;
    }// END OF onCreateView()

}// END OF CrimeFragment class
  

堆栈错误跟踪:       11-02 13:23:23.906 4592-4592 / com.bignerdranch.android.criminalintent E / AndroidRuntime:FATAL EXCEPTION:main                                                                                              过程:com.bignerdranch.android.criminalintent,PID:4592                                                                                              java.lang.StackOverflowError:堆栈大小为8MB                                                                                                  at com.bignerdranch.android.criminalintent.CrimeFragment $ 1.beforeTextChanged(CrimeFragment.java:0)                                                                                                  在android.widget.TextView.sendBeforeTextChanged(TextView.java:8268)                                                                                                  在android.widget.TextView.setText(TextView.java:4356)                                                                                                  在android.widget.TextView.setText(TextView.java:4313)                                                                                                  在android.widget.EditText.setText(EditText.java:84)                                                                                                  在android.widget.TextView.setText(TextView.java:4288)                                                                                                  在com.bignerdranch.android.criminalintent.CrimeFragment $ 1.onTextChanged(CrimeFragment.java:67)                                                                                                  在android.widget.TextView.sendOnTextChanged(TextView.java:8318)                                                                                                  在android.widget.TextView.setText(TextView.java:4459)                                                                                                  在android.widget.TextView.setText(TextView.java:4313)                                                                                                  在android.widget.EditText.setText(EditText.java:84)                                                                                                  在android.widget.TextView.setText(TextView.java:4288)                                                                                                  在com.bignerdranch.android.criminalintent.CrimeFragment $ 1.onTextChanged(CrimeFragment.java:67)                                                                                                  在android.widget.TextView.sendOnTextChanged(TextView.java:8318)                                                                                                  在android.widget.TextView.setText(TextView.java:4459)                                                                                                  在android.widget.TextView.setText(TextView.java:4313)                                                                                                  在android.widget.EditText.setText(EditText.java:84)                                                                                                  在android.widget.TextView.setText(TextView.java:4288)                                                                                                  在com.bignerdranch.android.criminalintent.CrimeFragment $ 1.onTextChanged(CrimeFragment.java:67)                                                                                                  在android.widget.TextView.sendOnTextChanged(TextView.java:8318)                                                                                                  在android.widget.TextView.setText(TextView.java:4459)                                                                                                  在android.widget.TextView.setText(TextView.java:4313)                                                                                                  在android.widget.EditText.setText(EditText.java:84)                                                                                                  在android.widget.TextView.setText(TextView.java:4288)                                                                                                  在com.bignerdranch.android.criminalintent.CrimeFragment $ 1.onTextChanged(CrimeFragment.java:67)                                                                                                  在android.widget.TextView.sendOnTextChanged(TextView.java:8318)                                                                                                  在android.widget.TextView.setText(TextView.java:4459)                                                                                                  在android.widget.TextView.setText(TextView.java:4313)                                                                                                  在android.widget.EditText.setText(EditText.java:84)                                                                                                  在android.widget.TextView.setText(TextView.java:4288)                                                                                                  在com.bignerdranch.android.criminalintent.CrimeFragment $ 1.onTextChanged(CrimeFragment.java:67)                                                                                                  在android.widget.TextView.sendOnTextChanged(TextView.java:8318)                                                                                                  在android.widget.TextView.setText(TextView.java:4459)                                                                                                  在android.widget.TextView.setText(TextView.java:4313)                                                                                                  在android.widget.EditText.setText(EditText.java:84)                                                                                                  在android.widget.TextView.setText(TextView.java:4288)                                                                                                  在com.bignerdranch.android.criminalintent.CrimeFragment $ 1.onTextChanged(CrimeFragment.java:67)                                                                                                  在android.widget.TextView.sendOnTextChanged(TextView.java:8318)                                                                                                  在android.widget.TextView.setText(TextView.java:4459)                                                                                                  在android.widget.TextView.setText(TextView.java:4313)                                                                                                  在android.widget.EditText.setText(EditText.java:84)                                                                                                  在android.widget.TextView.setText(TextView.java:4288)                                                                                                  在com.bignerdranch.android.criminalintent.CrimeFragment $ 1.onTextChanged(CrimeFragment.java:67)                                                                                                  在android.widget.TextView.sendOnTextChanged(TextView.java:8318)                                                                                                  在android.widget.TextView.setText(TextView.java:4459)                                                                                                  在android.widget.TextView.setText(TextView.java:4313)                                                                                                  在android.widget.EditText.setText(EditText.java:84)                                                                                                  在android.widget.TextView.setText(TextView.java:4288)                                                                                                  在com.bignerdranch.android.criminalintent.CrimeFragment $ 1.onTextChanged(CrimeFragment.java:67)                                                                                                  在android.widget.TextView.sendOnTextChanged(TextView.java:8318)                                                                                                  在android.widget.TextView.setText(TextView.java:4459)                                                                                                  在android.widget.TextView.setText(TextView.java:4313)                                                                                                  在android.widget.EditText.setText(EditText.java:84)                                                                                                  在android.widget.TextView.setText(TextView.java:4288)                                                                                                  在com.bignerdranch.android.criminalintent.CrimeFragment $ 1.onTextChanged(CrimeFragment.java:67)                                                                                                  在android.widget.TextView.sendOnTextChanged(TextView.java:8318)                                                                                                  在android.widget.TextView.setText(TextView.java:4459)                                                                                                  在android.widget.TextView.setText(TextView.java:4313)                                                                                                  在android.widget.EditText.setText(EditText.java:84)                                                                                                  在android.widget.TextView.setText(TextView.java:4288)                                                                                               at com.bignerdranch.android.criminalintent.CrimeFragment $ 1.onTextChanged(C       11-02 13:23:23.998 4592-4592 / com.bignerdranch.android.criminalintent E / JavaBinder:!!!失败的粘合剂交易! (包裹尺寸= 1896744)       11-02 13:23:24.007 4592-4592 / com.bignerdranch.android.criminalintent E / AndroidRuntime:错误报告崩溃                                                                                              android.os.TransactionTooLargeException:数据包大小1896744字节                                                                                                  在android.os.BinderProxy.transactNative(原生方法)                                                                                                  在android.os.BinderProxy.transact(Binder.java:505)                                                                                                  在android.app.ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:4427)                                                                                                  at com.android.internal.os.RuntimeInit $ UncaughtHandler.uncaughtException(RuntimeInit.java:90)                                                                                                  at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)                                                                                                  在java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)

请协助。

2 个答案:

答案 0 :(得分:1)

mViewPager.setCurrentItem(i)由fragmentStatePagerAdapeter自动完成。移除此循环语句。它导致将视图添加到自身,这可能是无限递归的原因。

 for (int i = 0; i < mCrimes.size(); i++) {
        if (mCrimes.get(i).getId().equals(crimeId)) {
            mViewPager.setCurrentItem(i);
            break;
        }
    }

同样在更新textview之前删除listner然后添加以防止stackoverflow错误

TextWatcher textWatcher = new TextWatcher(){

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        //Intentionally left blank
    }

    @Override
    public void onTextChanged(CharSequence s, int i, int i1, int i2) {
        mTitleField.removeTextChangedListener(textWatcher);
        mTitleField.setText(s.toString());
        mTitleField.addTextChangedListener(txtwt);
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

};
mTitleField.addTextChangedListener(textWatcher);

答案 1 :(得分:0)

J Ramesh的评论是正确的。

我删除了onTextChanged方法中的代码并修复了问题。

在那之前,我确实尝试过Anonymous&#39;回答但没有奏效。

如果没有for循环,ViewPager显示的第一个片段始终是列表中的第一个项目。

列表(在启动器活动中)使用RecyclerView,每个ViewHolder都有一个启动新的CrimePagerActivity的onclick监听器。

ViewHolder只有一个Crime对象,而不是列表。

我认为在将ViewHolder绑定到它的布局时,适配器可以将犯罪对象的位置与实际犯罪对象一起传递。然后,ViewHolder的onClickListener可以将其添加到它用于启动CrimePageActivity的intent中,以便ViewPager可以在没有for循环的情况下从正确的Crime / Fragment开始。 已编辑:我刚刚实施了此更改并且有效。 P.S我两周前开始学习android dev,所以我问你是否可以放纵我的基本方法。

非常感谢你的帮助。我想要或赞成你评论,但不知道如何。这个论坛的新用户。