如何修复“找不到id的视图”?

时间:2016-01-22 15:52:18

标签: java android android-fragments

该项目工作正常,但是当我添加“switchPage2(new limits_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>

2 个答案:

答案 0 :(得分:1)

使用

getActivity().getSupportFragmentManager()

取代

getFragmentManager()

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

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

答案 1 :(得分:0)

首先不要在布局中多次使用此行

xmlns:android="http://schemas.android.com/apk/res/android"

第二,你使用支持片段,所以你需要从活动中使用getSupportFragmentManager(),但因为你想在片段中使用片段,你需要使用getchildfragmentmanager()