添加CheckBox会导致Inflate异常

时间:2016-03-16 08:55:04

标签: android android-checkbox inflate-exception

我在xml文件中为我的对话框片段定义了一个复选框。执行代码时会导致Inflate Exception。我不知道代码有什么问题。如果删除该复选框一切正常。如果我也使用单选按钮,也会出现同样的异常。

这是我的xml文件。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#6688AA">

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:orientation="horizontal"
    android:padding="1dp"
    android:weightSum="4">
    <!--<TextView-->
    <!--android:layout_width="wrap_content"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:text="Sample Photo"-->
    <!--android:layout_gravity="center"-->
    <!--android:textSize="18sp"/>-->
    <ImageView
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:src="@drawable/correct_face" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:src="@drawable/error_face_1" />

    <ImageView
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:src="@drawable/error_face_2" />


    <ImageView
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_weight="1"
        android:src="@drawable/error_face_3" />

</LinearLayout>

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/linearLayout"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_horizontal"
    android:gravity="center"
    android:text="Upload Your Photo"
    android:textSize="40sp" />

<Button
    android:id="@+id/button_camera"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignEnd="@+id/textView"
    android:layout_alignRight="@+id/textView"
    android:layout_alignTop="@+id/button_gallery"
    android:background="#917528"
    android:drawableLeft="@android:drawable/ic_menu_camera"
    android:text="Camera" />

<Button
    android:id="@+id/button_gallery"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView"
    android:layout_alignStart="@+id/textView"
    android:layout_below="@+id/textView"
    android:background="#917528"
    android:drawableLeft="@android:drawable/ic_menu_gallery"
    android:text="Gallery" />

<ImageView
    android:id="@+id/uploadImage"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_below="@+id/button_camera"
    android:layout_centerHorizontal="true" />

<TextView
    android:id="@+id/crop_hint"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/uploadImage"
    android:layout_centerHorizontal="true"
    android:text="Touch the image to crop"
    android:textStyle="italic"
    android:visibility="visible" />

<CheckBox
    android:id="@+id/checkbox_default"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button_gallery"
    android:layout_below="@+id/crop_hint"
    android:text="Checkbox"
     />

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:visibility="gone">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/sample" />

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/sample" />

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/sample" />

        <ImageView
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/sample" />
    </LinearLayout>
</HorizontalScrollView>

<RelativeLayout
    android:id="@+id/container_CropImageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:visibility="gone">

    <com.theartofdev.edmodo.cropper.CropImageView xmlns:custom="http://schemas.android.com/apk/res-auto"
        android:id="@+id/CropImageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        custom:fixAspectRatio="true"
        custom:guidelines="onTouch"
        custom:scaleType="fitCenter" />

    <Button
        android:id="@+id/button_crop_save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:text="Save" />

    <Button
        android:id="@+id/button_crop_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:text="Cancel" />


</RelativeLayout>

</RelativeLayout>

DialogFragment文件

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.wizard_upload_photo, container, false);
    Button cameraButton = (Button) rootView.findViewById(R.id.button_camera);
    Button galleryButton = (Button) rootView.findViewById(R.id.button_gallery);
    uploadImage = (ImageView) rootView.findViewById(R.id.uploadImage);
    cropHint = (TextView) rootView.findViewById(R.id.crop_hint);
    mDefault = (CheckBox) rootView.findViewById(R.id.checkbox_default);
    cameraButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            cameraImage = null;
            try {
                cameraImage = Utils.createCameraImageFile();
            } catch (IOException ex) {
                Log.d("DEBUG", "onClick: Camera file generate exception" + ex);
                Utils.showAlertDialog(getActivity(), "Error Starting the Camera Please Try Again");
            }
            // Continue only if the File was successfully created
            if (cameraImage != null) {
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cameraImage));
                startActivityForResult(cameraIntent, CAPTURE_IMAGE_FROM_CAMERA);
            }
        }
    });
    galleryButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent galleryIntent = new Intent();
            galleryIntent.setType("image/*");
            galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(galleryIntent, "Select Picture"), CAPTURE_IMAGE_FROM_GALLERY);
        }
    });


    cropImageContainer = (RelativeLayout) rootView.findViewById(R.id.container_CropImageView);
    cropImageView = (CropImageView) rootView.findViewById(R.id.CropImageView);
    mCropSave = (Button) rootView.findViewById(R.id.button_crop_save);
    mCropCancel = (Button) rootView.findViewById(R.id.button_crop_cancel);

    mDefault.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Toast.makeText(getContext(),"Default",Toast.LENGTH_LONG).show();
        }
    });

    return rootView;
}

Logcat消息

com.realeffecx.styleu E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.realeffecx.styleu, PID: 23411
        android.view.InflateException: Binary XML file line #98: Error inflating class android.widget.CheckBox
        at android.view.LayoutInflater.createView(LayoutInflater.java:626)
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
        at com.realeffecx.styleu.fragments.wizard_UploadPhoto.onCreateView(wizard_UploadPhoto.java:107)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1988)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1268)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:754)
        at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1614)
        at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:633)
        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
        at android.view.View.measure(View.java:17495)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
        at android.view.View.measure(View.java:17495)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:17495)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:17495)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2548)
        at android.view.View.measure(View.java:17495)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2285)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1396)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1595)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6637)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:814)
        at android.view.Choreographer.doCallbacks(Choreographer.java:614)
        at android.view.Choreographer.doFrame(Choreographer.java:584)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:800)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5602)
        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:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.constructNative(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at android.view.LayoutInflater.createView(LayoutInflater.java:600)
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
        at com.realeffecx.styleu.fragments.wizard_UploadPhoto.onCreateView(wizard_UploadPhoto.java:107)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1988)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1268)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:754)
        at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1614)
        at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:633)
        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
        at android.view.View.measure(View.java:17495)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
        at android.view.View.measure(View.java:17495)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:17495)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:17495)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2548)
        at android.view.View.measure(View.java:17495)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2285)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1396)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1595)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6637)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:814)
        at android.view.Choreographer.doCallbacks(Choreographer.java:614)
        at android.view.Choreographer.doFrame(Choreographer.java:584)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:800)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5602)
        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:1283)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
        at dalvik.system.NativeStart.main(Native Method)
        Cause

它几乎是教科书的东西,所以我不知道我做错了什么。 请帮我。 提前致谢

3 个答案:

答案 0 :(得分:0)

根据上面的布局xml代码,您在相对布局中的第一个视图似乎是错误的(假设您已经按文件粘贴)。应删除android:layout_below="@+id/linearLayout"。 我想知道你怎么编译?

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="Upload Your Photo"
android:textSize="40sp" />

检查一下。

答案 1 :(得分:0)

最后我解决了这个问题。 Inflate Exception是由InvokeTargetException引起的,而InvokeTargetException又是由ResourceNotFoundException引起的。错误发生在android支持库23.2中。我不清楚为什么,但谷歌已经重新命名并从他们的支持库中删除了一些xml文件,并使用的是向量而不是PNG。只需恢复到上一个​​库(23.1.1)或分别使用AppCombatRadioButton或AppCombatCheckBox而不是RadioButton和CheckBox来解决错误。如果其他人遇到同样的问题,您可以查看在Google代码https://code.google.com/p/android/issues/detail?id=202144

中打开的此问题

答案 2 :(得分:0)

在我的情况下,这是一个无效的属性:

android:textSize="?android:attr/textAppearanceSmall"

取下后一切正常。我猜这个错误应该更加明确。