Android Fragment找不到ID为0x7f040005的视图

时间:2015-03-20 11:11:37

标签: java android android-fragments android-studio

这是我的片段类。我需要从一个片段移动到另一个片段。点击处理程序后,我需要用另一个片段替换现有的片段。

fragment_forum.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_weight="1"
    android:layout_height="match_parent" >

    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/forumListView"
        android:clickable="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_margin="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp" />

</LinearLayout>

ForumFragment.java

package com.example.navigationdrawerexample;

import android.annotation.TargetApi;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.List;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)

public class ForumFragment extends Fragment {

    public String[] groups = {"Mobile Phone", "Desktop"};

    public String[][] children = {
            {"Android", "Apple iOS", "Windows Phone", "BlackBerry"},
            {"Windows", "iOS", "Linux"}
    };

    public ForumFragment() {
    }

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

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

        ExpandableListView listView = (ExpandableListView) rootView.findViewById(R.id.forumListView);

        listView.setAdapter(new CustomExpandableListAdapter());

        listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i2, long l) {
                Toast.makeText(getActivity(),"Clicked : "+i+" "+i2, Toast.LENGTH_SHORT).show();
                Fragment fragment = new NewPostFragment();
                FragmentManager manager = getActivity().getFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.layout.fragment_forum, fragment);
                transaction.addToBackStack(null);
                transaction.commit();
                return false;
            }
        });

        return rootView;
    }

    private class CustomExpandableListAdapter extends BaseExpandableListAdapter{

        @Override
        public int getGroupCount() {
            return groups.length;
        }

        @Override
        public int getChildrenCount(int i) {
            return children[i].length;
        }

        @Override
        public Object getGroup(int i) {
            return groups[i];
        }

        @Override
        public Object getChild(int i, int i2) {
            return children[i][i2];
        }

        @Override
        public long getGroupId(int i) {
            return i;
        }

        @Override
        public long getChildId(int i, int i2) {
            return i2;
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

        @Override
        public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
            TextView textView = new TextView(ForumFragment.this.getActivity());
            textView.setTextSize(20);
            textView.setText(getGroup(i).toString());
            return textView;
        }

        @Override
        public View getChildView(int i, int i2, boolean b, View view, ViewGroup viewGroup) {
            TextView textView = new TextView(ForumFragment.this.getActivity());
            textView.setText(getChild(i,i2).toString());
            return textView;
        }

        @Override
        public boolean isChildSelectable(int i, int i2) {
            return true;
        }
    }
}

fragment_new_post.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/txtTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Title"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:ems="10"
        android:id="@+id/txtDescription" />


</LinearLayout>

NewPostFragment.java

package com.example.navigationdrawerexample;

import android.annotation.TargetApi;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by svbhargav on 3/20/2015.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class NewPostFragment extends Fragment {

    public NewPostFragment() {

    }

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

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

        return rootView;
    }

}

当我在我的设备中运行此操作时,我收到了错误

java.lang.IllegalArgumentException: No view found for id 0x7f040005

并且该应用已重启。 请帮帮我。

1 个答案:

答案 0 :(得分:0)

您无法使用transaction.replace(R.layout.fragment_forum, fragment);替换Fragment,因为R.layout.fragment_forum是布局ID而不是containerViewId。所以你需要一个容器来容纳不同的Fragment,这个容器可能就像:

<FrameLayout 
  android:id="@+id/fragment_container"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" />

然后您可以使用transaction.add/replace(R.id.fragment_container, newfragment);

希望这有帮助!