"无法在父母或祖先中找到方法"用于相机

时间:2016-11-10 15:37:32

标签: android camera

我是Android世界的新手,我正在努力让我的相机在我的应用程序中单击按钮打开。我已经遵循了几个教程,仍然没有设法让它工作。

最后,我进入了一个可以运行应用程序并打开相机活动的阶段,但是当我单击按钮拍摄新照片时,我的应用程序失败了。

我现在已经尝试了各种教程,但似乎无法让它发挥作用。

任何人都可以从下面的代码中告诉我我需要做些什么才能使这项工作?

我收到以下错误:

java.lang.IllegalStateException: Could not find method dispatchTakePictureIntent(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button_add_ph'


public class CameraActivity extends Activity {

ImageView result;
static final int REQUEST_IMAGE_CAPTURE = 1;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera_activity);

    result = (ImageView)findViewById(R.id.imageView1);
    Button takePic = (Button)findViewById(button_add_ph);


}

public void dispatchTakePictureIntent(View view) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap = (Bitmap) extras.get("data");
        result.setImageBitmap(imageBitmap);
    }
}

和我的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">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:layout_marginTop="15dp"
            >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="visible"
                android:id="@+id/imageView1"
                android:layout_centerVertical="true" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:layout_marginTop="10dp"
            >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/notes"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="5dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAlignment="center"
                    android:text="Yo - no notes added for this photo this photo this photo this photo"
                    android:id="@+id/textView"
                    android:textSize="14dp"
                    android:layout_centerHorizontal="true"
                    android:maxLength="60"/>

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/button_edit"
                    android:text="edit note"
                    android:layout_below="@+id/textView"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="5dp"/>

            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/notes"
                android:layout_marginTop="25dp">

                <Button
                    android:layout_width="0dip"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:id="@+id/button_add_note"
                    android:text="Add note"/>

                <Button
                    android:layout_width="0dip"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:id="@+id/button_delete"
                    android:text="Delete picture"/>

                <Button
                    android:layout_width="0dip"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:id="@+id/button_add_ph"
                    android:onClick="dispatchTakePictureIntent"
                    android:text="Take picture" />
            </LinearLayout>

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/backBtn"
                android:onClick="buttonOnClick"
                android:text="Hold dog mund"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="10dp"/>

        </RelativeLayout>
    </LinearLayout>

我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.europcar.mob1exam">

    <uses-feature android:name="android.hardware.camera2" android:required="true"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/iconfaetter"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
                android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".CameraActivity">

        </activity>
    </application>

</manifest>

提前多多感谢

0 个答案:

没有答案