【问题标题】:Android - Taking a picture with a cameraAndroid - 用相机拍照
【发布时间】:2014-12-30 04:06:53
【问题描述】:

我是 android 新手,我正在尝试让我的应用从相机中拍照。查看here 提供的示例,我尝试在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);
}
}

这是我的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>

我收到以下错误:

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)

我可能做错了什么,所以有人能指出我的错误吗?提前致谢。

【问题讨论】:

  • 在此处发布 full code 的 MainActivity 和 error。
  • 你能给我们指出行号17
  • @codePG 在第 17 行添加评论。
  • 谢谢,xml 似乎有问题。你能用activity_main.xml更新问题吗?
  • @codePG activity_main.xml 已添加。

标签: java android image android-layout


【解决方案1】:

UnsupportedOperationException: addView(View, LayoutParams) 不是 在 AdapterView 中支持

AdapterView 的子类(在您的情况下为 GRIDVIEW)不能在布局文件中手动添加子类或在代码中添加子类。

您的代码:

 <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>

不要这样做!

因为这会调用GridView的addView方法,抛出异常。而是使用:

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

另外,如果你在Activity或适配器的代码中使用了LayoutInflater.inflate方法(它的getView方法),不要将GridView作为第二个参数传递。例如,不要使用:

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

改为使用:

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

【讨论】:

    【解决方案2】:

    为什么要在你的 xml 中的 GridView 中添加 Button?

    GridView 不是为此目的而设计的。将按钮放在 GridView 之外并尝试。错误就是这样。

    GridView 只能用于在 Grid Pattern 中显示视图,它使用 Adapter 来显示数据。

    【讨论】:

      【解决方案3】:

      像这样使用你的网格视图:

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

      GridView 用于在 Grid Pattern 中显示视图,同样使用Adapter 显示数据。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多