android.content.res.Resources $ NotFoundException用于不同的设备或用户

时间:2017-05-25 07:42:12

标签: android android-resources

在Developer Console中,我可以看到来自不同设备的多个崩溃报告。

堆栈跟踪:

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
  at android.app.ActivityThread.access$900(ActivityThread.java:154)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:234)
  at android.app.ActivityThread.main(ActivityThread.java:5526)
  at java.lang.reflect.Method.invoke(Native Method:0)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.content.res.Resources$NotFoundException: 
  at android.content.res.Resources.getValue(Resources.java:1358)
  at android.content.res.Resources.loadXmlResourceParser(Resources.java:2787)
  at android.content.res.Resources.getLayout(Resources.java:1172)
  at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
  at ru.neverdark.csm.abs.AbsTabFragment.onCreateView(AbsTabFragment.java:46)
  at ru.neverdark.csm.fragments.MapTabFragment.onCreateView(MapTabFragment.java:122)
  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)

我的代码:

// AbsTabFragment
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(mLayoutId, container, false); // line 46
        View prevTab = view.findViewById(R.id.prev_tab);
        View nextTab = view.findViewById(R.id.next_tab);

        OnPrevNextClickListener clickListener = new OnPrevNextClickListener();

        if (prevTab != null) {
            prevTab.setOnClickListener(clickListener);
        }

        if (nextTab != null) {
            nextTab.setOnClickListener(clickListener);
        }

        return view;
    }

    protected void setData(int layoutId, OnTabNaviListener listener) {
        mLayoutId = layoutId;
        mTabNaviCallback = listener;
    }

MapTabFragment

public class MapTabFragment extends AbsTabFragment implements OnMapReadyCallback {
    public MapTabFragment() {
        // Required empty public constructor
    }

    public static MapTabFragment getInstance(OnTabNaviListener listener, OnMapFragmentListener callback) {
        MapTabFragment fragment = new MapTabFragment();
        fragment.setData(R.layout.fragment_map_tab, listener);
        fragment.mCallback = callback;
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.v(TAG, "onCreateView: ");
        View view = super.onCreateView(inflater, container, savedInstanceState); // line 122

        mDistance = (DataCard) view.findViewById(R.id.distance);
        mAverageSpeed = (DataCard) view.findViewById(R.id.average_speed);
        mActivityTime = (DataCard) view.findViewById(R.id.activity_time);
        mTotalTime = (DataCard) view.findViewById(R.id.total_time);
        mAntenna = (Antenna) view.findViewById(R.id.antenna);

        mDistance.setTitleNote(R.string.km);
        mAverageSpeed.setTitleNote(R.string.kmch);

        SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map_fragment);
        mapFragment.getMapAsync(this);
        return view;
    }

创建片段:

mMapTabFragment = MapTabFragment.getInstance(this, this);
// .. fragments used in ViewPager with CustomAdapter class:

    private class CustomAdapter extends FragmentPagerAdapter {
        CustomAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            TABS tab = TABS.values()[position];

            if (tab == TABS.MAP) {
                return mMapTabFragment;
            } else if (tab == TABS.COMPASS) {
                return mCompassTabFragment;
            } else if (tab == TABS.INFO) {
                return mInfoTabFragment;
            }

            return null;
        }

        @Override
        public int getCount() {
            return TABS.values().length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            TABS tab = TABS.values()[position];
            return getString(tab.getTitle());
        }
    }

我尝试重现问题,但没有成功。哪里错了?如果需要更多代码 - 我已准备好提供它。

fragment_map_tab.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"                                                                                                                                         
    xmlns:dataCard="http://schemas.android.com/apk/res-auto"                                                                                                                                                       
    xmlns:tools="http://schemas.android.com/tools"                                                                                                                                                                 
    android:layout_width="match_parent"                                                                                                                                                                            
    android:layout_height="match_parent"                                                                                                                                                                           
    tools:context="ru.neverdark.csm.fragments.MapTabFragment">                                                                                                                                                     

    <LinearLayout
        android:id="@+id/first_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:orientation="horizontal">

        <ru.neverdark.widgets.DataCard
            android:id="@+id/total_time"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            dataCard:title="@string/total_time"
            dataCard:value="@string/zero_time" />

        <ru.neverdark.widgets.DataCard
            android:id="@+id/activity_time"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:layout_weight="1"
            dataCard:title="@string/clean_time"
            dataCard:value="@string/zero_time" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/second_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/first_row"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:orientation="horizontal">

        <ru.neverdark.widgets.DataCard
            android:id="@+id/distance"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            dataCard:title="@string/distance" />

        <ru.neverdark.widgets.DataCard
            android:id="@+id/average_speed"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:layout_weight="1"
            dataCard:title="@string/average_speed" />
    </LinearLayout>

    <fragment
        android:id="@+id/map_fragment"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/second_row" />

    <ru.neverdark.widgets.Antenna
        android:id="@+id/antenna"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignTop="@+id/map_fragment"
        android:layout_margin="12dp" />

    <ImageView
        android:id="@+id/next_tab"
        style="@style/NextTabButton" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

这是因为当mLayoutId被调用时,字段0返回onCreateView,因为片段不会持久化它。您应该将mLayoutId放在Bundle中并将其作为参数传递给片段:

public static MapTabFragment getInstance(OnTabNaviListener listener, OnMapFragmentListener callback) {
    MapTabFragment fragment = new MapTabFragment();
    Bundle args = new Bundle();
    args.putInt("mLayoutId", R.layout.fragment_map_tab);
    fragment.setArguments(args);
    ...
    return fragment;
}

来自AbsTabFragment

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(getArguments().getInt("mLayoutId"), container, false);
    ...
}