我正在调用该函数dispatchTakePictureIntent
来拍照。但是在我拍照并保存后,应用程序无法正确返回到实际活动,应用程序崩溃。
当我的按钮被按下时,我打电话
dispatchTakePictureIntent(1);
然后:
public void dispatchTakePictureIntent(int actionCode) {
try {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = createImageFile();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(takePictureIntent, actionCode);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, actionCode);*/
}
public File createImageFile() throws IOException {
// Create an image file name
final String nomeForm = "teste";
String imageFileName = nomeForm +".jpeg";
File path = getExternalFilesDir(Environment.DIRECTORY_DCIM);
File image = new File(path,imageFileName );
// String getAbsolutePath = image.getAbsolutePath();
Toast.makeText(this, "Image saved to:\n" + image, Toast.LENGTH_LONG).show();
return image;
}
我的 logcat 错误报告:
java.lang.RuntimeException: Unable to stop activity {com.example.sform/com.example.sform.mainRisco}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sform/com.example.sform.risco1}: java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.widget.CompoundButton$SavedState instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/0x1. Make sure other views do not use the same id.
这是我所有的布局:
标签主机布局:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/layout_riscos">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</TabHost>
选项卡1:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/risco1"
>
<RelativeLayout
android:id="@+id/geral_risco1"
android:layout_width="match_parent"
android:layout_height="739dp"
android:orientation="vertical"
android:screenOrientation="nosensor" >
<Button
android:id="@+id/pelicula_risco1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="22dp"
android:layout_marginTop="36dp"
android:text="Take a Picture ;)"
android:textSize="30dp" />
<ImageView
android:id="@+id/imageView_risco1"
android:layout_width="9000dp"
android:layout_height="500dp"
android:layout_alignLeft="@+id/pelicula"
android:layout_marginTop="100dp"
android:layout_alignParentTop="true" >
</ImageView>
<RelativeLayout
android:id="@+id/riscoQ1_risco1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
</RelativeLayout>
</RelativeLayout>
</ScrollView>
选项卡2:
<TextView android:text="risco 2"
android:padding="15dip"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/text_risco2"/>
</RelativeLayout>
Tab3:
<TextView android:text="risco 3"
android:padding="15dip"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/text_risco3"/>
</RelativeLayout>
选项卡4:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/geral_risco4">
<TextView android:text="risco 4"
android:padding="15dip"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/text_risco4"/>
</RelativeLayout>
Tab5:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/geral_risco5" >
<TextView android:text="risco 5"
android:padding="15dip"
android:textSize="18dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/text_risco5"/>
</RelativeLayout>
主要活动:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:screenOrientation="nosensor"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
</RelativeLayout>
另一个画面:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:screenOrientation="nosensor"
android:id="@+id/scroll_tela_form"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/info2"
>
<EditText
android:id="@+id/dataAnalise"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="false"
android:layout_alignWithParentIfMissing="false"
android:layout_below="@+id/textView3"
android:layout_centerInParent="false"
android:layout_centerVertical="false"
android:layout_toLeftOf="@+id/getDate"
android:ems="10" />
<TextView
android:id="@+id/horaAnaliseFixo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/horaAnalise"
android:layout_below="@+id/dataAnalise"
android:layout_marginTop="8dp"
android:text="Hora de Inicio da Análise"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/nomeForm"
android:layout_marginTop="40dp"
android:text="Data da Análise"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/nomeForm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="35dp"
android:layout_toLeftOf="@+id/getDate"
android:layout_toRightOf="@+id/textView3"
android:text="NomeForm"
android:textSize="34dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/horaAnalise"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/horaAnaliseFixo"
android:layout_toLeftOf="@+id/getDate"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/getDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/horaAnaliseFixo"
android:layout_alignParentRight="true"
android:layout_marginRight="41dp"
android:text="Get Date" />
<Button
android:id="@+id/getTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/getDate"
android:layout_alignParentBottom="true"
android:text="Get Time" />
</RelativeLayout>
</ScrollView>