Ich habe eine fragment
mit einer imagebutton
und einer framelayout
. Wenn Sie das Fragment erstellen, wird dem Bildlayout eine Kameraperspektive hinzugefügt. hier die xml des Fragments:ImageButton kann nicht in FrameLayout umgewandelt werden, während ich ein Framelayout auf ihn projitiere
<?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>
Und hier das Java-Skript i für das Fragment verwenden:
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);
}
}
}
Der Fehler, den ich bekommen:
android.support.v7.widget.AppCompatImageButton cannot be cast to android.widget.FrameLayout at com.coralapps.face2face.CameraFragment.setupCamera(CameraFragment.java:41)
Aber auf der Leitung 41, i Link zu R.id.camera_view, und das ist eindeutig ein Framelayout, kein Imagebutton.
Mache ich hier etwas falsch?
Edit: Dies funktioniert, wenn ich die imagebutton
entfernen.
Code scheint gut.Versuchen Sie, Ihr Projekt neu zu erstellen: gehen Sie zu Build-> Projekt neu erstellen – chipopo