【问题标题】:Logcat Throws errors when overlaying a Fragment with a SurfaceHolder使用 SurfaceHolder 覆盖 Fragment 时 Logcat 引发错误
【发布时间】:2014-05-28 06:44:56
【问题描述】:

我正在尝试用cameraPreview 实例覆盖fragmentfragment 布局的根有一个 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"/>

日志猫:

 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  

【问题讨论】:

    标签: java android xml android-fragments surfaceholder


    【解决方案1】:

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

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


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

    CameraPreviewFragment 不是Context

    【讨论】:

    • 好的,如果CameraPreviewFragment 没有上下文,并且要从扩展surfaceholder 的类中实例化一个对象,您应该提供一个上下文,如何解决?
    • @Amr no no,看看我的第一句话。
    • 好的,我将其修改为“View view = inflater.inflate(R.layout.camerapreviewfragment, null, false);”但不幸的是同样的错误
    • 然后发布片段的布局文件。
    • 请看fragment_root和mainActivtiy_layout都贴在上面
    【解决方案2】:

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

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

    CameraPreviewFragment 不是上下文

    嘿结帐this了解有关工具的详细信息:上下文

    【讨论】:

    • 当我添加“工具:上下文..”这一行时,我认为这很重要。但是知道按照您的建议删除了它,但我收到了同样的错误!你能告诉我这行“tools:context..”什么时候有用吗?
    • tools:context 行不是罪魁祸首。 @Troopers 你真的复制粘贴了我的答案。
    • @dcow 仅供参考,当我在另一个选项卡中打开问题时,当时没有答案,所以我在没有查看您的答案的情况下回答了这个问题.....而且现在您已经编辑了关于布局文件:D .....
    • 你也遇到过这种情况(;
    猜你喜欢
    • 2023-03-13
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多