0

I am trying to overlay afragment with cameraPreview instance. The root of the fragment layout has a xml tag <surfaceview. When I run the App it crashes and throws the below errors. any idea why that happens?

Class_Extends_Fragment:

    public class CameraPreviewFragment extends Fragment {

private CustomCameraView mCameraPreview = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    mCameraPreview = new CustomCameraView(this.getActivity());
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    View view = inflater.inflate(R.layout.camerapreviewfragment, null);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    SurfaceView sv = (SurfaceView) getView().findViewById(R.id.surfaceView);

}

MainActivity_layout:

<?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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.fragmentwithcamerasurface.MainActivity"
tools:ignore="MergeRootFrame">
    <fragment
        android:name="com.example.fragmentwithcamerasurface.CameraPreviewFragment"
        android:id="@+id/fragment00ID"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </fragment>

fragmentRoot_layout:

<FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android"    
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:ignore="MergeRootFrame"
android:background="#ffff00">

<!--logcat complains about this line-->
    <com.example.fragmentwithcamerasurface.CustomCameraView
    android:id="@+id/surfaceView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"/>

logCat:

 05-28 09:34:13.670: E/AndroidRuntime(11327): java.lang.RuntimeException: Unable to  
 start activity   
 ainActivity}: android.view.InflateException: Binary XML file line #11: Error  
 inflating class com.example.fragmentwithcamerasurface.CustomCameraView
 05-28 09:34:13.670: E/AndroidRuntime(11327): java.lang.RuntimeException: Unable to 
 start activity  
 ainActivity}: android.view.InflateException: Binary XML file line #11: Error   
 inflating class com.example.fragmentwithcamerasurface.CustomCameraView
 05-28 09:34:13.670: E/AndroidRuntime(11327):   at  
Amrmsmb
  • 1
  • 27
  • 104
  • 226

2 Answers2

0

You're using the wrong layout file as your fragment's root layout. The layout file you inflate in getView() should be the file specifying the views you want to see inside the fragment, not the fragment itself.

I believe the layout inflater only goes 25 steps deep recursively.


You should add false as a third argument to LayoutInflater.inflate.

And CameraPreviewFragment is not a Context.

dcow
  • 7,765
  • 3
  • 45
  • 65
0

First of all inflating with R.layout.camerapreviewfragment but the layout you are creating is fragmentRoot_layout and

tools:context="com.example.fragmentwithcamerasurface.CameraPreviewFragment"  

CameraPreviewFragment is not a context

Hey checkout this for details about tools:context

Toppers
  • 559
  • 3
  • 11
  • when i added the line "tools:context.." i thought it is important. but knw ideleted it as you suggested but i receive the same error! would you please tell me when this line "tools:context.." is useful? – Amrmsmb May 28 '14 at 07:07
  • The `tools:context` line is not the culprit. @Troopers you literally copy-pasted my answer. – dcow May 28 '14 at 07:10
  • @dcow FYI the moment I opened the question in an another tab that time there was no answer for this so I answered the question without checking out your answer.....and also now you have edited about the layout file so :D ..... – Toppers May 28 '14 at 07:24
  • Had that before you too (; – dcow May 28 '14 at 07:27