java.lang.IllegalArgumentException:找不到id 0x7f0c005b的视图

时间:2015-09-04 04:22:14

标签: android android-fragments

我正在获得像

这样的Android运行时异常
  

java.lang.IllegalArgumentException:找不到ID为0x7f0c005b的视图

用于android中导航应用程序的片段活动。我得到OutOfMemory exception因为我在Fragment课程中添加了onDestroy()现在我在刷新后获得view not found

我的代码如下所示,

    public class VerticesFreehandFragment extends Fragment {

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

        DatabaseHandler dbHandler = new DatabaseHandler(getActivity());
        try {
            dbHandler.createDataBase();
        } catch (IOException ioe) {
            throw new Error("Unable to create database");
        }

        try {
            dbHandler.openDataBase();
            dbHandler.close();
        }catch(SQLException sqle){
            throw sqle;
        }

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

        final VerticesPoints imageView = (VerticesPoints)rootView.findViewById(R.id.imageView);
        imageView.setImage(ImageSource.resource(R.drawable.floor_plan));

        final VerticesPointsSecondFloor image = (VerticesPointsSecondFloor)rootView.findViewById(R.id.image);
        image.setImage(ImageSource.resource(R.drawable.floor_plan2));

        PointF center = new PointF(1226, 816);
        imageView.setScaleAndCenter(0.8f, center);
        image.setScaleAndCenter(0.8f, center);

        FloatingActionMenu actionMenu = (FloatingActionMenu) rootView.findViewById(R.id.menu);
        FloatingActionButton floor1 = (FloatingActionButton) rootView.findViewById(R.id.floor1);
        FloatingActionButton floor2 = (FloatingActionButton) rootView.findViewById(R.id.floor2);

        floor1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "Floor1", Toast.LENGTH_SHORT).show();
                imageView.setVisibility(View.VISIBLE);
                image.setVisibility(View.INVISIBLE);
            }
        });

        floor2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "Floor2", Toast.LENGTH_SHORT).show();
                imageView.setVisibility(View.INVISIBLE);
                image.setVisibility(View.VISIBLE);
            }
        });

        return rootView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        onDestroyView(getActivity());
    }

    public static void onDestroyView(Activity activity) {
        View rootView = null;

        try {
            rootView = ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
        } catch (Exception e) {
            Log.e("Cannot find root view", "activity");
        }

        if (rootView != null) {
            Log.d("unbindDrawables", "activity, rootView");
            unbindDrawables(rootView);
        }
    }

    /**
     * Utility method to unbind drawables when an activity is destroyed.  This
     * ensures the drawables can be garbage collected.
     */
    public static void unbindDrawables(View view) {
        if (view.getBackground() != null) {
            view.getBackground().setCallback(null);
        }

        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                unbindDrawables(((ViewGroup) view).getChildAt(i));
            }

            try {
                // AdapterView objects do not support the removeAllViews method
                if (!(view instanceof AdapterView)) {
                    ((ViewGroup) view).removeAllViews();
                }
            } catch (Exception e) {
                Log.w("Ignore Exception", e);
            }
        }
    }

}

上面的clas我的XML文件是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.cosmonet.murali.floormarker.Views.VerticesPoints
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:visibility="visible"/>

<com.cosmonet.murali.floormarker.Views.VerticesPointsSecondFloor
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:visibility="invisible"/>

<com.github.clans.fab.FloatingActionMenu
    android:id="@+id/menu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    fab:menu_fab_size="normal"
    fab:menu_labels_style="@style/menu_labels_style"
    fab:menu_labels_position="left"
    fab:menu_icon="@drawable/ic_up_arrow"
    fab:menu_openDirection="up"
    fab:menu_backgroundColor="@android:color/transparent">

    <com.github.clans.fab.FloatingActionButton
        android:id="@+id/floor2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_fab_star"
        fab:fab_size="mini"
        fab:fab_label="Second Floor" />

    <com.github.clans.fab.FloatingActionButton
        android:id="@+id/floor1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_floor1"
        fab:fab_size="mini"
        fab:fab_label="First Floor" />

</com.github.clans.fab.FloatingActionMenu>

先谢谢你

0 个答案:

没有答案