我是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)
我可能做错了什么,所以有人能指出我的错误吗?提前谢谢。
答案 0 :(得分:3)
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的代码或适配器(其getView方法)中使用LayoutInflater.inflate方法,则不要将GridView作为第二个参数传递。例如,不要使用:
convertView = inflator.inflate(R.layout.child, parent);
改为使用:
convertView = inflator.inflate(R.layout.child, parent, false);
答案 1 :(得分:1)
为什么要在yout xml中添加GridView中的Button?
GridView不是为此目的而设计的。将按钮放在GridView外面并尝试。错误与此有关。
GridView只能用于在网格图案中显示视图,它使用适配器显示数据。
答案 2 :(得分:1)
使用您的网格视图如下:
<GridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="2"
android:columnWidth="50dp"
android:gravity="center_horizontal"/>
GridView用于在网格模式中显示视图,同样使用Adapter
来显示数据。