难以重现XML通胀错误

时间:2015-03-27 18:21:21

标签: android android-fragments android-xml android-inflate

这种崩溃是偶尔报道的,我无法解释为什么会发生这种情况或者自己重复它。我有一个活动容器,它在框架布局中托管一个片段,当用户遍历应用程序时会被替换,一个底部条形片段一直停留在那里,还有一个标准的工具栏和导航抽屉。当底部的条形/页脚片段(在我的代码中称为ActionsFragment),有时无法膨胀时会发生错误。

完整活动xml:

<RelativeLayout
android:id="@+id/main_parent_view"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true">

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/wholeScreen"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:id="@+id/screenMinusBar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1">

                    <FrameLayout
                        android:id="@+id/fragmentContainer"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:paddingTop="?attr/actionBarSize"/>

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentBottom="true"
                        android:scaleType="fitXY"
                        android:src="@drawable/action_fragment_shadow"/>
                </RelativeLayout>

                <fragment android:name="com.xyz.fragments.ActionsFragment"
                    android:id="@+id/actions_bottom_fragment"
                    android:layout_width="match_parent"
                    android:layout_height="56dp"/>
            </LinearLayout>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?colorPrimary"
                android:minHeight="?attr/actionBarSize"
                app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
                app:popupTheme="@style/Toolbar_Popup"/>

        </RelativeLayout>

        <com.devspark.robototextview.widget.RobotoTextView
            android:id="@+id/action_text_view_uid"
            style="?android:textAppearanceSmallInverse"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center_horizontal"
            android:gravity="center"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:textColor="@color/white"
            android:visibility="invisible"
            app:typeface="roboto_light"/>
    </FrameLayout>

    <!-- The navigation drawer -->
    <LinearLayout
        android:id="@+id/left_drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="start"
        android:background="@color/white">

        <ListView
            android:id="@+id/left_drawer_list"
            android:layout_width="@dimen/nav_drawer_width"
            android:layout_height="wrap_content"
            android:listSelector="@android:color/transparent"
            android:choiceMode="singleChoice"
            android:cacheColorHint="#0000"
            android:paddingBottom="8dp"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="0.5dp"
            android:background="@color/divider"
            android:paddingTop="8dp"/>

        <LinearLayout
            android:id="@+id/nav_list_settings"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_marginTop="8dp"
            android:clickable="true"
            android:background="@drawable/nav_item_selector"
            android:orientation="horizontal">

                <ImageView
                    android:layout_width="24dp"
                    android:layout_height="24dp"
                    android:layout_gravity="center"
                    android:background="@null"
                    android:layout_marginLeft="16dp"
                    android:src="@drawable/ic_nav_settings"/>

                <com.devspark.robototextview.widget.RobotoTextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_vertical"
                    android:minHeight="@dimen/listPreferredItemHeightSmall"
                    android:paddingLeft="32dp"
                    android:text="Help and Feedback"
                    android:textColor="@color/text"
                    android:textSize="14sp"
                    app:typeface="roboto_medium"/>
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@color/white"/>
    </LinearLayout>

</android.support.v4.widget.DrawerLayout>

ActionsFragment.java基本上只是在一个布局中有一些点击监听器和两个图像按钮,没什么太花哨的,这里是其中的大部分:

public class ActionsFragment extends Fragment {

private static final int ANIMATION_DURATION = 200;
private Context mContext;
private MainActivity mHostingActivity;
private int screenWidth, screenHeight;
private TextView mUIDTextView;
private BitmapFactory.Options mBitmapOptions;
private Bitmap qrBitmap;
private Animator mCurrentAnimator;
private ImageView mImageButton1;
private ImageButton mImageButton2;
private int mImageButtonHeight = 64;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_action_bar_bottom, container, false);
    mContext = getActivity();

    DisplayMetrics metrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
    switch (metrics.densityDpi) {
        case DisplayMetrics.DENSITY_MEDIUM:
            mImageButtonHeight = 48;
            break;
        case DisplayMetrics.DENSITY_HIGH:
            mImageButtonHeight = 72;
            break;
        case DisplayMetrics.DENSITY_XHIGH:
            mImageButtonHeight = 96;
            break;
        case DisplayMetrics.DENSITY_XXHIGH:
            mImageButtonHeight = 144;
            break;
    }

    assert view != null;
    mImageButton2 = (ImageButton) view.findViewById(R.id.action_btn_1);
    mImageButton1 = (ImageButton) view.findViewById(R.id.action_btn_2);

    mHostingActivity = (MainActivity) getActivity();

    setupMetrics();
    setup();
    return view;
}

@Override
public void onStart(){
    super.onStart();
    mUIDTextView = (TextView) getActivity().findViewById(R.id.action_text_view_uid);
    mUIDTextView.setText("User Number:\n"
            + App.getDatabaseHandler().getTableLoggedUserID() + "\n");
}

private void setupMetrics() {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        // the results will be higher than using the activity context object or the getWindowManager() shortcut
        WindowManager wm = (WindowManager) mContext.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(displayMetrics);
        screenWidth = displayMetrics.widthPixels;
        screenHeight = displayMetrics.heightPixels;
    }

 private void setup() {
        try {
            if (App.isUserLoggedIn()) {

                User user = App.getDatabaseHandler().getUser();
                String uid = user.getUID();
                File imgFile = new File(uid);

                //if the image exists load it from memory
                if (imgFile.exists()) {
                    mBitmapOptions = new BitmapFactory.Options();
                    mBitmapOptions.inSampleSize = 1;
                }

                int newWidth = screenWidth / 3;
                newWidth = newWidth * 2;
                int newHeight = screenHeight / 3;
                newHeight = newHeight * 2;

                //create a qr for the image button
                if (!imgFile.exists()) {
                    String id = user.getId();
                    BarcodeGenerator b = new BarcodeGenerator(id, newWidth, newHeight, mContext);
                    b.createqr();
                }

                bm = BitmapFactory.decodeFile(user.getQr(), mBitmapOptions);
                mImageButton1.setImageBitmap(Bitmap.createScaledBitmap(bm, mImageButtonHeight, mImageButtonHeight, false));
                mImageButton1.invalidate();

                getActivity().onContentChanged();

                mImageButton1.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        //Launch activity
                    }
                });
            } else {
                // If the user is not logged in, they are returned to the Login screen of the application.
                Intent login = new Intent(mContext.getApplicationContext(), LoginActivity.class);
                login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(login);
                getActivity().finish();
            }
        } catch (Exception e) {
        }
    }
}

错误的堆栈跟踪:

    android.view.InflateException: Binary XML file line #54: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
    at android.app.ActivityThread.access$700(ActivityThread.java:159)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:176)
    at android.app.ActivityThread.main(ActivityThread.java:5419)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #54: Error inflating class fragment
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
    at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228)
    at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
    at com.xyz.activities.MainActivity.onCreate(MainActivity.java:232)
    at android.app.Activity.performCreate(Activity.java:5372)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
    ... 11 more
Caused by: java.lang.IllegalStateException: Fragment com.xyz.fragments.ActionsFragment did not create a view.
    at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2189)
    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
    at android.support.v7.app.ActionBarActivity.onCreateView(ActionBarActivity.java:547)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)

SO上的类似错误引用了xml片段必须具有id的事实,我的确如此,并且应该在活动中的setContentView()之前调用super.OnCreate()。

有什么建议吗?

编辑:

fragment_action_bar_bottom.xml

<RelativeLayout
    android:id="@+id/action_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical">

    <ImageButton
        android:id="@+id/action_btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:background="@drawable/img_btn_1"/>

    <ImageButton
        android:id="@+id/action_btn_2"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:padding="8dp"
        android:background="@null"
        android:scaleType="centerInside"/>

</RelativeLayout>

0 个答案:

没有答案