簡體   English   中英

找不到片段ID的視圖

[英]No view found for id for fragment

嘗試從我的主要活動中打開相機時,我總是收到錯誤消息。 我不確定我設置了什么錯誤來接收錯誤java.lang.IllegalArgumentException:找不到ID的視圖。 解決該問題的任何方法將不勝感激。

MainActivity. Java


public class MainActivity extends Activity {

private static final int SELECT_PICTURE = 0;
private static final int IMAGE_CAPTURE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean titled = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.activity_main);
    if(titled){
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar_main);
    }

    ((ImageButton) findViewById(R.id.button1))
            .setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {

                    // in onCreate or any event where your want the user to
                    // select a file
                    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);

                    //check if there is app to receive intent
                    PackageManager packageManager = getPackageManager();
                    List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
                    @SuppressWarnings("unused")
                    boolean isIntentSafe = activities.size() > 0;

                    startActivityForResult(Intent.createChooser(intent,
                            "Select Picture"), SELECT_PICTURE);
                }
            });

    ((ImageButton) findViewById(R.id.button2))
            .setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    getFragmentManager().beginTransaction()
                            .replace(R.id.container, Camera2BasicFragment.newInstance())
                            .commit();
                }
            });
}


public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            Intent intent = new Intent(this, EditImage.class);
            intent.setType("/*image");
            intent.setData(selectedImageUri);
            intent.putExtra("EditMode", false);
            startActivity(intent);
        }
        else if (requestCode == IMAGE_CAPTURE)
        {
            //File dir = Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
            //File output = new File(dir, "camerascript.png");
            //String cPath = output.getAbsolutePath();
            Intent Camera2BasicFragment  = new Intent(this, EditImage.class);
            Camera2BasicFragment.setType("/*image");
            Camera2BasicFragment.setData(data.getData());
            //if edit mode is 1, intent is from camera
            Camera2BasicFragment.putExtra("EditMode", true);
            startActivity(Camera2BasicFragment);
        }
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}


Camera2BasicFragment.java


public class Camera2BasicFragment extends Fragment implements View.OnClickListener {
/**
 * Conversion from screen rotation to JPEG orientation.
 */
private static final SparseIntArray ORIENTATIONS = new SparseIntArray();

static {
    ORIENTATIONS.append(Surface.ROTATION_0, 90);
    ORIENTATIONS.append(Surface.ROTATION_90, 0);
    ORIENTATIONS.append(Surface.ROTATION_180, 270);
    ORIENTATIONS.append(Surface.ROTATION_270, 180);
}

/**
 * Tag for the {@link Log}.
 */
private static final String TAG = "Camera2BasicFragment";

/**
 * Camera state: Showing camera preview.
 */
private static final int STATE_PREVIEW = 0;

/**
 * Camera state: Waiting for the focus to be locked.
 */
private static final int STATE_WAITING_LOCK = 1;
/**
 * Camera state: Waiting for the exposure to be precapture state.
 */
private static final int STATE_WAITING_PRECAPTURE = 2;
/**
 * Camera state: Waiting for the exposure state to be something other than precapture.
 */
private static final int STATE_WAITING_NON_PRECAPTURE = 3;
/**
 * Camera state: Picture was taken.
 */
private static final int STATE_PICTURE_TAKEN = 4;

/**
 * {@link TextureView.SurfaceTextureListener} handles several lifecycle events on a
 * {@link TextureView}.
 */
private final TextureView.SurfaceTextureListener mSurfaceTextureListener
        = new TextureView.SurfaceTextureListener() {

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture texture, int width, int height) {
        openCamera(width, height);
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture texture, int width, int height) {
        configureTransform(width, height);
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture texture) {
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture texture) {
    }

};

/**
 * ID of the current {@link CameraDevice}.
 */
private String mCameraId;

/**
 * An {@link AutoFitTextureView} for camera preview.
 */
private AutoFitTextureView mTextureView;

/**
 * A {@link CameraCaptureSession } for camera preview.
 */

private CameraCaptureSession mCaptureSession;
/**
 * A reference to the opened {@link CameraDevice}.
 */

private CameraDevice mCameraDevice;
/**
 * The {@link android.util.Size} of camera preview.
 */

private Size mPreviewSize;

/**
 * {@link CameraDevice.StateCallback} is called when {@link CameraDevice} changes its state.
 */
private final CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {

    @Override
    public void onOpened(CameraDevice cameraDevice) {
        // This method is called when the camera is opened.  We start camera preview here.
        mCameraOpenCloseLock.release();
        mCameraDevice = cameraDevice;
        createCameraPreviewSession();
    }

    @Override
    public void onDisconnected(CameraDevice cameraDevice) {
        mCameraOpenCloseLock.release();
        cameraDevice.close();
        mCameraDevice = null;
    }

    @Override
    public void onError(CameraDevice cameraDevice, int error) {
        mCameraOpenCloseLock.release();
        cameraDevice.close();
        mCameraDevice = null;
        Activity activity = getActivity();
        if (null != activity) {
            activity.finish();
        }
    }

};


static class CompareSizesByArea implements Comparator<Size> {

    @Override
    public int compare(Size lhs, Size rhs) {
        //

public static class ErrorDialog extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Activity activity = getActivity();
        return new AlertDialog.Builder(activity)
                .setMessage("This device doesn't support Camera2 API.")
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        activity.finish();
                    }
                })
                .create();
    }

}

}


activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_color"
tools:context=".MainActivity" >

<ImageButton
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/startApp"
    android:src="@drawable/gallery_icon"
    android:background="@android:color/transparent"
    android:text="@string/startApp" />

<ImageButton
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/button1"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/startApp"
    android:src="@drawable/lense_icon"
    android:background="@android:color/transparent"
    android:text="@string/startApp" />

</RelativeLayout>

fragment_camera2_basic.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.example.android.camera2basic.AutoFitTextureView
    android:id="@+id/texture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true" />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true"
    android:layout_below="@id/texture"
    android:background="#4285f4">

    <Button
        android:id="@+id/picture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/picture" />

    <ImageButton
        android:id="@+id/info"
        android:contentDescription="@string/description_info"
        style="@android:style/Widget.Material.Light.Button.Borderless"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|right"
        android:padding="20dp"
        android:src="@drawable/ic_action_info" />

</FrameLayout>

</RelativeLayout>

您是否已為Android分配使用Android攝像頭的權限?

請注意,您ImageButton聽眾正在尋求增加Camera2BasicFragment在任何地方居住片段在R.id.container

((ImageButton) findViewById(R.id.button2))
        .setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                getFragmentManager().beginTransaction()
                        .replace(R.id.container, Camera2BasicFragment.newInstance())
                        .commit();
            }
        });

但是,您在MainActivity視圖層次結構中沒有定義這樣的容器FrameLayout 您的activity_main.xml布局文件僅列出了兩個ImageButton

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM