Java 已添加片段,尽管只添加了一次
我正在尝试制作一个由4个片段组成的活动。 每个碎片在刷击时被另一个碎片替换(不销毁) 当我向左滑动两次到达第三个片段时,应用程序终止,错误代码为 java.lang.IllegalStateException:已添加的片段:[4个片段]{8e8204d#3 id=0x7f080033 android:switcher:2131230771:3} 但问题是,我没有在包含片段的活动调用的第一个onCreate之后添加任何片段 谁能给我一个建议,我应该直接采取解决这个问题 我的主要活动代码如下Java 已添加片段,尽管只添加了一次,java,android,android-fragments,Java,Android,Android Fragments,我正在尝试制作一个由4个片段组成的活动。 每个碎片在刷击时被另一个碎片替换(不销毁) 当我向左滑动两次到达第三个片段时,应用程序终止,错误代码为 java.lang.IllegalStateException:已添加的片段:[4个片段]{8e8204d#3 id=0x7f080033 android:switcher:2131230771:3} 但问题是,我没有在包含片段的活动调用的第一个onCreate之后添加任何片段 谁能给我一个建议,我应该直接采取解决这个问题 我的主要活动代码如下 imp
import android.location.Location;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
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.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.List;
public class AnalysisActivity extends AppCompatActivity implements LocationFragment.OnFragmentInteractionListener, TypeFragment.OnFragmentInteractionListener, CountFragment.OnFragmentInteractionListener, SummaryFragment.OnFragmentInteractionListener {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
private LocationFragment locationFragment;
private TypeFragment typeFragment;
private CountFragment countFragment;
private SummaryFragment summaryFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_analysis);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
locationFragment = new LocationFragment();
typeFragment = new TypeFragment();
countFragment = new CountFragment();
summaryFragment = new SummaryFragment();
mSectionsPagerAdapter.addFragment(locationFragment, "LocationFragment");
mSectionsPagerAdapter.addFragment(summaryFragment, "SummaryFragment");
mSectionsPagerAdapter.addFragment(typeFragment, "TypeFragment");
mSectionsPagerAdapter.addFragment(countFragment, "CountFragment");
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, locationFragment)
.replace(R.id.container, summaryFragment)
.replace(R.id.container, typeFragment)
.replace(R.id.container, countFragment)
.commit();
}
@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_analysis, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
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 = null;
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1:
rootView = inflater.inflate(R.layout.fragment_location, container, false);
break;
case 2:
rootView = inflater.inflate(R.layout.fragment_type, container, false);
break;
case 3:
rootView = inflater.inflate(R.layout.fragment_count, container, false);
break;
case 4:
rootView = inflater.inflate(R.layout.fragment_summary, container, false);
break;
}
return rootView;
}
}
@Override
public void onInputLocationSent(CharSequence input) {
summaryFragment.updateEditText_summary_location(input);
}
@Override
public void onInputTypeSent(CharSequence input) {
summaryFragment.updateEditText_summary_type(input);
}
@Override
public void onInputCountSent(CharSequence input) {
summaryFragment.updateEditText_summary_count(input);
}
@Override
public void onInputSummaryLocationSent(CharSequence input) {
}
@Override
public void onInputSummaryTypeSent(CharSequence input) {
}
@Override
public void onInputSummaryCountSent(CharSequence input) {
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@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).
// return PlaceholderFragment.newInstance(position + 1);
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
}
}
导入android.location.location;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.Toolbar;
导入android.support.v4.app.Fragment;
导入android.support.v4.app.FragmentManager;
导入android.support.v4.app.FragmentPagerAdapter;
导入android.support.v4.view.ViewPager;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.EditText;
导入java.util.ArrayList;
导入java.util.List;
公共类AnalysisActivity扩展AppCompatActivity实现LocationFragment.OnFragmentInteractionListener、TypeFragment.OnFragmentInteractionListener、CountFragment.OnFragmentInteractionListener、SummaryFragment.OnFragmentInteractionListener{
/**
*将提供的{@link android.support.v4.view.PagerAdapter}
*每个部分的片段。我们使用
*{@link FragmentPagerAdapter}派生,它将保留
*已在内存中加载片段。如果这变得过于内存密集,则
*最好是换成一个
*{@link android.support.v4.app.FragmentStatePagerAdapter}。
*/
私人部门SPAGERAAdapter MSECTIONSPAGERAAdapter;
/**
*将承载节内容的{@link ViewPager}。
*/
私有视图寻呼机mViewPager;
私有位置片段LocationFragment;
私有类型片段类型片段;
私有CountFragment CountFragment;
私有摘要片段摘要片段;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_analysis);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
设置支持操作栏(工具栏);
//创建适配器,该适配器将为这三个函数中的每一个返回一个片段
//活动的主要部分。
mSectionsPagerAdapter=newsectionspageradapter(getSupportFragmentManager());
locationFragment=新的locationFragment();
typeFragment=新的typeFragment();
countFragment=新的countFragment();
summaryFragment=新的summaryFragment();
mSectionsPagerAdapter.addFragment(locationFragment,“locationFragment”);
addFragment(summaryFragment,“summaryFragment”);
addFragment(typeFragment,“typeFragment”);
addFragment(countFragment,“countFragment”);
//使用分区适配器设置ViewPager。
mViewPager=(ViewPager)findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
getSupportFragmentManager().beginTransaction()
.更换(R.id.容器,位置碎片)
.replace(R.id.container,summaryFragment)
.replace(R.id.container,typeFragment)
.replace(R.id.container,countFragment)
.commit();
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.menu\u分析,菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
/**
*包含简单视图的占位符片段。
*/
公共静态类占位符片段扩展了片段{
/**
*表示此文件节号的片段参数
*碎片。
*/
私有静态最终字符串ARG\u SECTION\u NUMBER=“SECTION\u NUMBER”;
公共占位符片段(){
}
/**
*返回给定节的此片段的新实例
*号码。
*/
公共静态占位符片段newInstance(int sectionNumber){
占位符片段=新占位符片段();
Bundle args=新Bundle();
参数Putin(参数段号,段号);
fragment.setArguments(args);
返回片段;
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图rootView=null;
开关(getArguments().getInt(参数节号)){
案例1:
rootView=充气机。充气(R.layout.fragment\u位置,容器,假);
打破
案例2:
rootView=充气机。充气(R.layout.fragment\u类型,容器,false);
打破
案例3:
rootView=充气机。充气(R.layout.fragment\u计数,容器,假);
打破
案例4:
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, locationFragment)
.replace(R.id.container, summaryFragment)
.replace(R.id.container, typeFragment)
.replace(R.id.container, countFragment)
.commit();