使用SurfaceHolder覆盖Fragment时,Logcat会引发错误

时间:2014-05-28 06:44:56

标签: java android xml android-fragments surfaceholder

我正在尝试使用fragment实例覆盖cameraPreviewfragment布局的根有一个xml标记<surfaceview。当我运行应用程序时,它崩溃并抛出以下错误。知道为什么会这样吗?

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  

2 个答案:

答案 0 :(得分:0)

您使用错误的布局文件作为片段的根布局。您在getView()中膨胀的布局文件应该是指定要在片段中查看的视图的文件,而不是片段本身。

我相信布局的inflater只递归了25步。


您应该将false作为第三个参数添加到LayoutInflater.inflate

CameraPreviewFragment不是Context

答案 1 :(得分:0)

首先使用R.layout.camerapreviewfragment充气,但你要创建的布局是fragmentRoot_layout和

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

CameraPreviewFragment不是上下文

Hey checkout this了解有关工具的详细信息:context