我想将一些字符串数据从 4 个片段传递到 1 个包含活动的片段。此包含活动正在使用 TabLayout 和 ViewPager。我想制作在包含活动中声明的字符串数组,填充来自片段的这些字符串数据。所以我在每个片段中制作了 4 个 OnFragmentInteractionListener 接口,并在包含活动时覆盖了它们。但它没有用。我收到许多这样的错误。
--------- beginning of crash
05-20 04:31:14.572 2495-2495/com.example.jsfish_lt.test E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jsfish_lt.test, PID: 2495
java.lang.StackOverflowError: stack size 8MB
at android.content.res.XmlBlock.newParser(XmlBlock.java:71)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2797)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2776)
at android.content.res.Resources.getLayout(Resources.java:1165)
at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
at com.example.jsfish_lt.test.containing.02_Fragment.onCreateView(02_Fragment.java:52)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:186)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.example.jsfish_lt.test.containing.02_Fragment.onCreateView(02_Fragment.java:52)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:186)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.example.jsfish_lt.test.containing.02_Fragment.onCreateView(02_Fragment.java:52)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:186)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.example.jsfish_lt.test.containing.02_Fragment.onCreateView(02_Fragment.java:52)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWra
05-20 04:31:14.596 2495-2505/com.example.jsfish_lt.test I/art: WaitForGcToComplete blocked for 6.392ms for cause Background
05-20 04:31:14.711 2495-2501/com.example.jsfish_lt.test W/art: Suspending all threads took: 26.019ms
05-20 04:31:14.750 2495-2505/com.example.jsfish_lt.test I/art: Background partial concurrent mark sweep GC freed 156819(21MB) AllocSpace objects, 18(61MB) LOS objects, 3% free, 124MB/128MB, paused 596us total 147.945ms
05-20 04:31:14.801 2495-2495/com.example.jsfish_lt.test D/Error: ERR: exClass=java.lang.StackOverflowError
05-20 04:31:14.801 2495-2495/com.example.jsfish_lt.test D/Error: ERR: exMsg=stack size 8MB
05-20 04:31:14.801 2495-2495/com.example.jsfish_lt.test D/Error: ERR: file=XmlBlock.java
05-20 04:31:14.801 2495-2495/com.example.jsfish_lt.test D/Error: ERR: class=android.content.res.XmlBlock
05-20 04:31:14.801 2495-2495/com.example.jsfish_lt.test D/Error: ERR: method=newParser line=71
05-20 04:31:14.852 2495-2495/com.example.jsfish_lt.test D/Error: ERR: stack=java.lang.StackOverflowError: stack size 8MB
at android.content.res.XmlBlock.newParser(XmlBlock.java:71)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2797)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2776)
at android.content.res.Resources.getLayout(Resources.java:1165)
at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
at com.example.jsfish_lt.test.containing.02_Fragment.onCreateView(02_Fragment.java:52)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:186)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.example.jsfish_lt.test.containing.02_Fragment.onCreateView(02_Fragment.java:52)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:186)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.example.jsfish_lt.test.containing.02_Fragment.onCreateView(02_Fragment.java:52)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:186)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:746)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.example.jsfish_lt.test.containing.02_Fragment.onCreateView(02_Fragment.java:52)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.vie
05-20 04:31:14.856 2495-2495/com.example.jsfish_lt.test D/Error: ERR: TOTAL BYTES WRITTEN: 11660880
05-20 04:31:14.856 2495-2495/com.example.jsfish_lt.test E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 11660972)
05-20 04:31:14.872 2495-2495/com.example.jsfish_lt.test E/AndroidRuntime: Error reporting crash
android.os.TransactionTooLargeException: data parcel size 11660972 bytes
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:503)
at android.app.ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:4425)
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:90)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
05-20 04:31:14.872 2495-2495/com.example.jsfish_lt.test I/Process: Sending signal. PID: 2495 SIG: 9
任何帮助将不胜感激。
包含活动.java
package com.example.jsfish_lt.test.containing;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import com.example.jsfish_lt.test.R;
public class SurveyActivity extends AppCompatActivity
implements Survey_01_Fragment.OnFragmentInteractionListener, Survey_02_Fragment.OnFragmentInteractionListener, Survey_03_Fragment.OnFragmentInteractionListener, Survey_04_Fragment.OnFragmentInteractionListener {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
public void onFragmentInteraction(Uri uri) {
}
public static String surveyResult[] = new String[14];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_survey);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
@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_survey, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onFragmentInteraction(int fg_id, String string1, String string2, String string3, String string4) {
switch (fg_id){
case 1:
surveyResult[0] = string1;
surveyResult[1] = string2;
surveyResult[2] = string3;
surveyResult[3] = string4;
break;
case 3:
surveyResult[7] = string1;
surveyResult[8] = string2;
surveyResult[9] = string3;
surveyResult[10] = string4;
break;
}
}
@Override
public void onFragmentInteraction(int fg_id, String string1, String string2, String string3) {
switch (fg_id){
case 2:
surveyResult[4] = string1;
surveyResult[5] = string2;
surveyResult[6] = string3;
break;
case 4:
surveyResult[11] = string1;
surveyResult[12] = string2;
surveyResult[13] = string3;
Log.i("SurveyResult : ", surveyResult.toString());
break;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position){
case 0:
return Survey_01_Fragment.newInstance(position+1);
case 1:
return Survey_02_Fragment.newInstance(position+1);
case 2:
return Survey_03_Fragment.newInstance(position+1);
case 3:
return Survey_04_Fragment.newInstance(position+1);
}
return null;
}
@Override
public int getCount() {
// Show 3 total pages.
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
String title = "";
switch (position) {
case 0:
title = "111";
break;
case 1:
title = "222";
break;
case 2:
title = "333";
break;
case 3:
title = "444";
break;
}
return title;
}
}
}
Survey_01_Fragment.java
package com.example.jsfish_lt.test.containing;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import com.example.jsfish_lt.test.R;
public class Survey_01_Fragment extends Fragment {
private OnFragmentInteractionListener mListener;
private static final String ARG_SECTION_NUMBER = "section_number";
private static final String ARG_NICKNAME = "NICKNAME";
private static final String ARG_AGE = "AGE";
private static final String ARG_GENDER = "GENDER";
private static final String ARG_JOB = "JOB";
private EditText mNicknameView;
private EditText mAgeView;
private String mGenderSpinner;
private String mJobSpinner;
public Survey_01_Fragment() {
// Required empty public constructor
}
public static Survey_01_Fragment newInstance(int sectionNumber) {
Survey_01_Fragment fragment = new Survey_01_Fragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_01, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.survey_number, getArguments().getInt(ARG_SECTION_NUMBER)));
mNicknameView = (EditText)rootView.findViewById(R.id.survey_01_et_nickname);
mAgeView = (EditText)rootView.findViewById(R.id.survey_01_et_age);
Spinner spGender = (Spinner)rootView.findViewById(R.id.survey_01_sp_gender);
ArrayAdapter<CharSequence> spGenderAdapter = ArrayAdapter.createFromResource(getActivity(), R.array.gender, android.R.layout.simple_spinner_item);
spGenderAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spGender.setAdapter(spGenderAdapter);
spGender.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
mGenderSpinner = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
mGenderSpinner = "";
}
});
Spinner spJob = (Spinner)rootView.findViewById(R.id.survey_01_sp_job);
ArrayAdapter<CharSequence> spJobAdapter = ArrayAdapter.createFromResource(getActivity(), R.array.job, android.R.layout.simple_spinner_item);
spJobAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spJob.setAdapter(spJobAdapter);
spJob.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
mJobSpinner = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) { mJobSpinner = ""; }
});
Button btPrev = (Button)rootView.findViewById(R.id.survey_01_bt_prev);
Button btNext = (Button)rootView.findViewById(R.id.survey_01_bt_next);
btNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int id = 1;
if(mListener != null)
mListener.onFragmentInteraction(id, mNicknameView.getText().toString(), mAgeView.getText().toString(), mGenderSpinner, mJobSpinner);
}
});
return rootView;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(int fg_id, String string1, String string2, String string3, String string4);
}
}
public class SurveyActivity extends AppCompatActivity
implements Survey_01_Fragment.OnFragmentInteractionListener, Survey_02_Fragment.OnFragmentInteractionListener, Survey_03_Fragment.OnFragmentInteractionListener, Survey_04_Fragment.OnFragmentInteractionListener {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
public void onFragmentInteraction(Uri uri) {
}
public static String surveyResult[] = new String[14];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_survey);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
@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_survey, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onFragmentInteraction(int fg_id, String string1, String string2, String string3, String string4) {
switch (fg_id){
case 1:
surveyResult[0] = string1;
surveyResult[1] = string2;
surveyResult[2] = string3;
surveyResult[3] = string4;
break;
case 3:
surveyResult[7] = string1;
surveyResult[8] = string2;
surveyResult[9] = string3;
surveyResult[10] = string4;
break;
}
}
@Override
public void onFragmentInteraction(int fg_id, String string1, String string2, String string3) {
switch (fg_id){
case 2:
surveyResult[4] = string1;
surveyResult[5] = string2;
surveyResult[6] = string3;
break;
case 4:
surveyResult[11] = string1;
surveyResult[12] = string2;
surveyResult[13] = string3;
Log.i("SurveyResult : ", surveyResult.toString());
break;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position){
case 0:
return 01_Fragment.newInstance(position+1);
case 1:
return 02_Fragment.newInstance(position+1);
case 2:
return 03_Fragment.newInstance(position+1);
case 3:
return 04_Fragment.newInstance(position+1);
}
return null;
}
@Override
public int getCount() {
// Show 3 total pages.
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
String title = "";
switch (position) {
case 0:
title = "111";
break;
case 1:
title = "222";
break;
case 2:
title = "333";
break;
case 3:
title = "444";
break;
}
return title;
}
}
}
public class Survey_01_Fragment extends Fragment {
private OnFragmentInteractionListener mListener;
private static final String ARG_SECTION_NUMBER = "section_number";
private static final String ARG_NICKNAME = "NICKNAME";
private static final String ARG_AGE = "AGE";
private static final String ARG_GENDER = "GENDER";
private static final String ARG_JOB = "JOB";
private EditText mNicknameView;
private EditText mAgeView;
private String mGenderSpinner;
private String mJobSpinner;
public Survey_01_basicinfoFragment() {
// Required empty public constructor
}
public static Survey_01_Fragment newInstance(int sectionNumber) {
Survey_01_Fragment fragment = new Survey_01_Fragment ();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}