0

我正在尝试fragmentcameraPreview实例覆盖一个。布局的根fragment有一个 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>

片段根布局:

<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"/>

日志猫:

 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  
4

2 回答 2

0

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

我相信布局充气器只会递归地深入 25 步。


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

并且CameraPreviewFragment不是Context.

于 2014-05-28T06:47:48.207 回答
0

首先使用 R.layout.camerapreviewfragment 膨胀,但您正在创建的布局是 fragmentRoot_layout 和

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

CameraPreviewFragment 不是上下文

嘿结帐以获取有关工具的详细信息:上下文

于 2014-05-28T07:01:27.793 回答