Java 当我将FrameLayout强制转换为ImageButton时,无法将其强制转换为FrameLayout

Java 当我将FrameLayout强制转换为ImageButton时,无法将其强制转换为FrameLayout,java,android,android-layout,android-fragments,Java,Android,Android Layout,Android Fragments,我有一个片段,带有一个图像按钮和一个框架布局。创建片段时,它会向framelayout添加一个cameraview。下面是片段的xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="mat

我有一个
片段
,带有一个
图像按钮
和一个
框架布局
。创建片段时,它会向framelayout添加一个cameraview。下面是片段的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageButton
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/watchButton"
        android:src="@drawable/wearc"
        android:scaleType="fitCenter"
        android:padding="0dp"
        android:onClick="onPic"
        android:contentDescription="@string/watch" />

    <FrameLayout
        android:id="@+id/camera_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="31.2dp">
    </FrameLayout>

</LinearLayout>
package com.coralapps.face2face;

import android.app.Fragment;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toast;

public class CameraFragment extends Fragment {

    android.hardware.Camera mCamera;
    private CameraView mCameraView = null;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.camera_fragment, container, false);
        setupCamera(v);
        return v;
    }

    public void setupCamera(View v){
        try{
            mCamera = Camera.open();//you can use open(int) to use different cameras
        } catch (Exception e){
            Log.d("ERROR", "Failed to get camera: " + e.getMessage());
        }

        if(mCamera != null) {
            mCameraView = new CameraView(getActivity(), mCamera);
            FrameLayout camera_view = (FrameLayout) v.findViewById(R.id.camera_view);
            camera_view.addView(mCameraView);
        }
    }
}
我得到的错误是:

android.support.v7.widget.AppCompatImageButton cannot be cast to android.widget.FrameLayout at com.coralapps.face2face.CameraFragment.setupCamera(CameraFragment.java:41)
但在第41行,我链接到R.id.camera_视图,这显然是一个框架布局,而不是一个Imagebutton

我做错什么了吗


编辑:当我删除
imagebutton

bro时,这确实有效,运行这些代码时没有发现错误!但我改变了这一行

   public void setupCamera(View v){
    try{
        mCamera = Camera.open();//you can use open(int) to use different cameras
    } catch (Exception e){
        Log.d("ERROR", "Failed to get camera: " + e.getMessage());
    }

    if(mCamera != null) {
        //change
       // mCameraView = new CameraView(getActivity(), mCamera);
        FrameLayout camera_view = (FrameLayout) v.findViewById(R.id.camera_view);
        camera_view.addView(mCameraView);
    }
}

我认为问题在于,您在
onCreateView
中调用
setupCamera(View v)
,并将尚未充气的视图作为参数传入。相反,您应该从创建视图后调用的
onResume()
中获取并操作视图。所以你应该调用
setupCamera(v)来自
onResume()
方法。

代码似乎很好。尝试重建项目:转到Build->rebuild Projects,但是我错过了mCameraView的声明?