[英]After take a picture and save it, sometimes the program crashes, doesn't return to the activity
我正在調用函數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>
Tab1:
<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>
Tab2:
<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>
Tab4:
<?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>
您對兩個不同的視圖使用了相同的id
。
檢查您的布局XML
並針對不同的視圖使用不同的ids
。
它表示您對不同的視圖使用相同的ID,且ID為0x1。 看起來可疑
a)在需要查看ID的地方,給出了其他東西,或者
b)R.java生成不正確。 請檢查某處的ID是否為1。
您是否檢查了
@android:id/tabs
@android:id/tabcontent
?
造成此問題的另一個原因可能是,同一布局的兩個(或多個)布局文件存儲在layout
, layout-land
, layout-w600dp
等下。如果這些布局具有共享相同id的不同類型的視圖,則可以得到此異常兩個。 要重現異常,您需要調用函數dispatchTakePictureIntent()
,然后旋轉設備,然后拍照並返回。 有幫助嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.