简体   繁体   English

Android-使用相机拍照

[英]Android - Taking a picture with a camera

I am new to android, and I am trying to make my app take a picture from the camera. 我是android的新手,我正在尝试使我的应用从相机拍摄照片。 Looking at the example provided here , I tried to implement in my app in MainActivity.java as such: 查看此处提供的示例,我试图在MainActivity.java中的应用中实现如下:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); // line 17
}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

static final int REQUEST_IMAGE_CAPTURE = 1;

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

Here is my activity_main.xml : 这是我的activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/welcome" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_centerHorizontal="true"
        android:textSize="40dp" android:textStyle="bold" android:textColor="#990000"/>
    <GridView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numColumns="2"
        android:columnWidth="50dp"
        android:gravity="center_horizontal">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/picture_1"
                android:onClick="dispatchTakePictureIntent"/>
        </GridView>

</RelativeLayout>

I am getting the following error: 我收到以下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{---/.MainActivity}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
        at android.app.ActivityThread.access$800(ActivityThread.java:139)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5102)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
        at android.widget.AdapterView.addView(AdapterView.java:478)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:759)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
          at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegaeBase.java:228)
        at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
        at ---/.MainActivity.onCreate(MainActivity.java:17)
        at android.app.Activity.performCreate(Activity.java:5248)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2173)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5102)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

I am probably doing something wrong, so can anyone point out my mistake? 我可能做错了什么,所以有人可以指出我的错误吗? Thanks in advance. 提前致谢。

UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView UnsupportedOperationException:AdapterView中不支持addView(View,LayoutParams)

A subclass of AdapterView (in your case GRIDVIEW) can't have children manually added either in the layout file or added in code. AdapterView的子类(在您的情况下为GRIDVIEW)不能在布局文件中或在代码中手动添加子级。

Your code: 您的代码:

 <GridView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numColumns="2"
        android:columnWidth="50dp"
        android:gravity="center_horizontal">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/picture_1"
                android:onClick="dispatchTakePictureIntent"/>
        </GridView>

Don't do it ! 不要做

As this will call the addView method of GridView, throwing the exception. 因为这将调用GridView的addView方法,从而引发异常。 Instead use: 而是使用:

      <GridView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numColumns="2"
            android:columnWidth="50dp"
            android:gravity="center_horizontal"/>

Also, if you use the LayoutInflater.inflate method in the code of the Activity or the adapter(its getView method), don't pass the GridView as the second parameter. 另外,如果在Activity或适配器的代码(其getView方法)中使用LayoutInflater.inflate方法,请不要将GridView作为第二个参数传递。 For example, don't use: 例如,不要使用:

convertView  = inflator.inflate(R.layout.child, parent);

Instead use: 而是使用:

convertView  = inflator.inflate(R.layout.child, parent, false);

Why you are adding Button in GridView in yout xml ? 为什么要在xml的GridView中添加Button?

GridView is not designed for this purpose. GridView不是为此目的而设计的。 Put the button outside the GridView and try. 将按钮放在GridView外部,然后尝试。 Error is about that. 错误就是这样。

GridView can only be used to display views in Grid Pattern and it uses Adapter to display data. GridView只能用于以“网格模式”显示视图,并且它使用适配器来显示数据。

Use your grid View like this: 像这样使用网格视图:

 <GridView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:numColumns="2"
            android:columnWidth="50dp"
            android:gravity="center_horizontal"/>

GridView is used to display views in Grid Pattern and similarly uses Adapter to display data. GridView用于显示“网格模式”中的视图,并类似地使用Adapter显示数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM