简体   繁体   中英

After take a picture and save it, sometimes the program crashes, doesn't return to the activity

I'm calling the function dispatchTakePictureIntent to take a picture. But after I take the picture and save it, the application doesn't returns correctly to the actual activity, the application crashes.

When my button is pressed I call

dispatchTakePictureIntent(1);

then:

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

my logcat error report:

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.

Here all my layouts:

Tab Host Layout:

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

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

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

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

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

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

Main Activity:

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

Another screen:

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

You have used same id for two different views.

Check your layout XML & use different ids for different views.

It says that you are using the same id for different views and that the id is 0x1. That looks suspiciously like

a) somewhere a view id is expected, something else is given or

b) the R.java is generated incorrectly. Please check if theres an id of 1 in there somewhere.

Did you check the value of

@android:id/tabs
@android:id/tabcontent

?

Another reason for this issue can be two (or more) layout files for the same layout stored under layout , layout-land , layout-w600dp etc. If these layouts have views of different types sharing same id, then you can get this exception two. To reproduce the exception you need to call function dispatchTakePictureIntent() , then rotate your device, then take picture and return back. Does it help?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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