我有一份很好的NBA球队名单并正确填写了球队名单,但是当我点击列表中的项目时,它应该给另一个片段充气,以显示所选球队的描述,但是当我做的时候点击无法找到fragment_descriptions id。
我为团队使用了两个List Fragment,另一个用于Players,为此我使用ViewPager和Placeholder Fragment(由Android Studio自动生成),我认为这是因为fragment_description的布局何时不会膨胀:/
例外:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: es.iesnervion.ejercicio52, PID: 7432
java.lang.IllegalArgumentException: No view found for id 0x7f0c0098 (es.iesnervion.ejercicio52:id/fragment_descriptions) for fragment DescriptionFragment{8f04b31 #4 id=0x7f0c0098}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1107)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:541)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
XML
<?xml version="1.0" encoding="utf-8"?>
<!-- Pagina 1 referente a Equipos -->
<FrameLayout 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:id="@+id/fragment_team_movil"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".View.Activity.MainActivity">
tools:showIn="@layout/activity_main">
</FrameLayout>
MainActivity
@Override
public void onTeamSelected(Team team) {
DescriptionFragment descriptionFragment = (DescriptionFragment) getSupportFragmentManager()
.findFragmentById(R.id.fragment_descriptions);
if (descriptionFragment != null) {
descriptionFragment.updateDescriptionView(team);
} else {
DescriptionFragment newDescriptionFragment = new DescriptionFragment();
Bundle args = new Bundle();
args.putParcelable(DescriptionFragment.ARG_TEAM, team);
newDescriptionFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_descriptions, newDescriptionFragment);
//Añade a la pila el fragment
transaction.addToBackStack(null);
transaction.commit();
}
}
protected void onResume() {
super.onResume();
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
if (manager != null)
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), manager);
else {
manager = new PlayerManager();
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);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Edited Cambiar a false para eliminar el boton atras
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setVisibility(View.INVISIBLE);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent it = new Intent(MainActivity.this, AddPlayer.class);
startActivityForResult(it, AddPlayer.ADDPLAYER_REQUEST);
}
});
}
PlaceholderFragment
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
rootView = inflater.inflate(R.layout.fragment_teams, container, false);
// Comprobación del tipo de pantalla en la que está, para ello llamamos a el fragment team
// (el .xml que deberia de salir en caso de que sea un movil y si no esta nulo significa
// que es un movil, si de lo contrario sale una table se iniciará el otro .xml de sw620dp
if (rootView.findViewById(R.id.fragment_team_movil) != null) {
// Comprobamos si viene de un estado onStoped osea que sea la primera vez que se
// ejecuta o no
if (savedInstanceState != null)
return rootView;
// Creamos una instancia del fragment lista y lo añadimos en la actividad destinada
// para el movil.
ListTeams list = new ListTeams();
Intent it = new Intent();
it = getActivity().getIntent();
if (manager != null)
it.putExtra(PlayerManager.PLAYERMANAGER_NAME_KEY, manager);
list.setArguments(it.getExtras());
// Con FragmentManager podremos interactuar entre el fragment_movil y la clase list
// gracias a esto pondremos todos las propiedades preparada de ese
// fragment en el fragment
getActivity().getSupportFragmentManager().beginTransaction().add(R.id.fragment_team_movil, list)
.commit();
}
如果您需要更多信息,请告诉我或访问Ejercicio5.2 Github和Issue #5页。
谢谢:)我整天都在犯错:(
答案 0 :(得分:0)
我快速浏览了github上的项目,我认为你应该用这一行替换这一行
transaction.replace(R.id.fragment_descriptions, newDescriptionFragment);
< br /> transaction.replace(R.id.fragment_team_movil, newDescriptionFragment);
在您的 MainActivity.java
MainActivity - line 209