动态更改片段 - java.lang.IllegalArgumentException:找不到id

时间:2016-04-09 23:08:45

标签: android android-fragments

我正在学习如何在Android中编程,而且我在更改片段方面遇到了一些问题。我尝试了几乎所有我在这里找到的解决方案,但他们没有工作。

这是我试图在屏幕上显示的布局:

fragment_result.xml

    <?xml version="1.0" encoding="utf-8"?>

    <FrameLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.example.ic.projeto_final.ResultFragment">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/resultView">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight=".90"
                android:gravity="center"
                android:orientation="horizontal" >

                <Button
                    android:layout_width="0dp"
                    android:layout_weight=".10"
                    android:layout_height="wrap_content"
                    android:text="setaesq - pegar cod ascii"
                    android:id="@+id/btnPrevious"
                />

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight=".80"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="15dp"
                        android:textAlignment="center"
                        android:textSize="25dp"
                        android:id="@+id/fileName" >
                    </TextView>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="15dp"
                        android:textAlignment="center"
                        android:textSize="25dp"
                        android:id="@+id/fileDistance" >
                    </TextView>

                    <ImageView
                        android:layout_marginTop="15dp"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/fileImage"
                    />

            </LinearLayout>

            <Button
                android:layout_width="0dp"
                android:layout_weight="0.1"
                android:layout_height="wrap_content"
                android:text="seta dir pegar cod ascii"
                android:id="@+id/btnNext"
                />

        </LinearLayout>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Fazer nova consulta"
            android:layout_gravity="center_horizontal"
            android:layout_alignParentBottom="true"
            android:id="@+id/btnNovaConsulta"/>
    </LinearLayout>

</FrameLayout>

课程相关:

ResultFragment.java

public class ResultFragment extends Fragment {   

public ResultFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_result, container, false);      

    TextView fileName = (TextView) rootView.findViewById(R.id.fileName);
    TextView fileDistance = (TextView) rootView.findViewById(R.id.fileDistance);

    ImageView imageName = (ImageView) rootView.findViewById(R.id.fileImage);

    Button btnPrevious = (Button) rootView.findViewById(R.id.btnPrevious);
    Button btnNext = (Button) rootView.findViewById(R.id.btnNext);

    return rootView;
}

}

这是我的mainActivity中试图更改显示的片段的部分:

MainActivityFragment.java

btnConsulta.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {


       /* Others pieces of code that not affect this part */  

                ResultFragment result = new ResultFragment();
                FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
                transaction.replace(R.id.resultView, result);
                transaction.addToBackStack(null);
                transaction.commit();
            }
        }

目前,我的logcat中显示的错误是:

java.lang.IllegalArgumentException: No view found for id 0x7f0a000f (com.example.ic.projeto_final:id/resultView) for fragment ResultFragment{143a9831 #0 id=0x7f0a000f}
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:886)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
        at android.app.BackStackRecord.run(BackStackRecord.java:834)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
        at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我还尝试使用getFragmentManager()而不是getChildFragmentManager(),将id与fragment_result.xml中的FrameLayout相关联,但它也不起作用。

如果我需要显示任何遗漏以解决问题,请告诉我

1 个答案:

答案 0 :(得分:0)

在你的主要活动xml文件中输入:

< FrameLayout
android:id = "@+id/resultView"
android:layout_width = "wrap_content"
android_layout_hight = "match_parent"
/>