gpt4 book ai didi

java - 如何修复 "no view found for id"?

转载 作者:行者123 更新时间:2023-12-01 10:33:34 27 4
gpt4 key购买 nike

该项目工作正常,但是当我添加“switchPage2(new limit_fragmentpageC());”时出现问题这是 logcat logcat

这是BaseFlashCardsFragment的代码

package com.flashcards;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
* A simple {@link Fragment} subclass.
* Use the {@link BaseFlashCardsFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class BaseFlashCardsFragment extends Fragment implements View.OnClickListener {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_POSITION = "position";

// TODO: Rename and change types of parameters
private int position;
private String mParam2;

public BaseFlashCardsFragment() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param position Parameter 1.
* @return A new instance of fragment BaseFlashCardsFragment.
*/
// TODO: Rename and change types and number of parameters
public static BaseFlashCardsFragment newInstance(int position) {
BaseFlashCardsFragment fragment = new BaseFlashCardsFragment();
Bundle args = new Bundle();
args.putInt(ARG_POSITION, position);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
position = getArguments().getInt(ARG_POSITION, 0);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = null;
int layoutId;
switch (position) {
case 0: layoutId = R.layout.limits_flashcardsfragment1; break;
case 1:layoutId = R.layout.limits_flashcardsfragment2; break;
case 2:layoutId = R.layout.limits_flashcardsfragment3; break;
default: layoutId = R.layout.limits_flashcardsfragment4;break;
}
rootView = inflater.inflate(layoutId, container, false);
rootView.findViewById(R.id.buttonterm).setOnClickListener(this);
rootView.findViewById(R.id.buttondefinition).setOnClickListener(this);
switchPage(new limits_fragmentpageA());
switchPage1(new limits_fragmentpageB());
switchPage2(new limits_fragmentpageC());
switchPage3(new limits_fragmentpageD());
return rootView;
}

@Override
public void onClick(View view) {
int viewId = view.getId();

if (viewId == R.id.buttonterm) {
switchPage(new limits_fragmentpageA());
}
else if (viewId == R.id.buttondefinition) {
switchPage(new limits_fragmentpageAA());
}

if (viewId == R.id.buttonterm) {
switchPage1(new limits_fragmentpageB());
}
else if (viewId == R.id.buttondefinition) {
switchPage1(new limits_fragmentpageBB());
}

if (viewId == R.id.buttonterm) {
switchPage2(new limits_fragmentpageC());
}
else if (viewId == R.id.buttondefinition) {
switchPage2(new limits_fragmentpageCC());
}

if (viewId == R.id.buttonterm) {
switchPage3(new limits_fragmentpageD());
}
else if (viewId == R.id.buttondefinition) {
switchPage3(new limits_fragmentpageDD());
}
}


private void switchPage (Fragment fragment){
android.support.v4.app.FragmentManager manager = getFragmentManager();
manager.beginTransaction()
.replace(R.id.activity_main_fragmentcontainer1, fragment)
.commit();
}
private void switchPage1 (Fragment fragment){
android.support.v4.app.FragmentManager manager = getFragmentManager();
manager.beginTransaction()
.replace(R.id.activity_main_fragmentcontainer2, fragment)
.commit();
}
public void switchPage2 (Fragment fragment){
android.support.v4.app.FragmentManager manager = getFragmentManager();
manager.beginTransaction()
.replace(R.id.activity_main_fragmentcontainer3, fragment)
.commit();
}
private void switchPage3 (Fragment fragment){
android.support.v4.app.FragmentManager manager = getFragmentManager();
manager.beginTransaction()
.replace(R.id.activity_main_fragmentcontainer4, fragment)
.commit();
}

}

这是limits_fragmentpageC的代码

package com.flashcards;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class limits_fragmentpageC extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
return inflater.inflate(R.layout.limits_fragment_flashcards_c, container, false);
}
}

这是 XML 布局,其中包括 activity_main_fragmentcontainer3

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp">
<include layout="@layout/include_toolbar" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:layout_marginTop="10dp"
android:text="Title 3"
android:textSize="22sp"
android:textStyle="bold" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
android:paddingLeft="16dp"
android:paddingRight="16dp">

<Button
android:id="@+id/buttonterm"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Term"
android:layout_marginRight="5dp"
android:background="@drawable/button_background"
android:layout_weight="1"/>

<Button
android:id="@+id/buttondefinition"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:text="Definition"
android:background="@drawable/button_background"
android:layout_weight="1"/>
</LinearLayout>

<FrameLayout
android:id="@+id/activity_main_fragmentcontainer3"
android:layout_width="match_parent"
android:layout_height="270dp"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="@drawable/framelayout_border">
</FrameLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="(Swipe to Navigate) \n (3 of 10 Cards)"
android:layout_marginTop="15dp"
android:textSize="20sp"
android:layout_gravity="center"/>

</LinearLayout>
</ScrollView>
<include layout="@layout/include_toolbarbottom" />
</RelativeLayout>

最佳答案

使用

getActivity().getSupportFragmentManager()

代替

getFragmentManager()

我很确定这会导致您的问题,因为我最近使用 getFragmentManager() 时有相同的日志输出。

在使用 Fragment 的支持库版本时,您应该始终使用 getSupportFragmentManager()

关于java - 如何修复 "no view found for id"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34950681/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com