Fragment中的java.lang.NoClassDefFoundError

时间:2016-03-24 08:33:16

标签: android fragment

我有下一个代码:

import android.support.v4.app.Fragment; 
public class HomeFragment extends Fragment{
...
private class TabAdapter extends FragmentPagerAdapter {
        @Override
        public Fragment getItem(@Tab int position) {
            switch (position) {
                case TAB1:
                    return SearchFragment.newInstance();
                ...
            }
        }
}
...
}

import android.location.Location;
import android.os.Bundle; 
import android.widget.AbsListView;
import android.widget.ListView;
public class SearchFragment extends Fragment{
...
        public static SearchFragment newInstance() {
            SearchFragment fragment = new SearchFragment ();
            return fragment;
        }
        private class C implements ListView.OnScrollListener{
        ...
        }
        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            C c = new C();
        }
...
}

当我在Android 4.3(< 5)中执行此代码时,我收到错误java.lang.NoClassDefFoundError:xx.xx.SearchFragment $ C

在Android 5和6中都可以

Logcat输出:

FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: xx.xx.SearchFragment$C
    at xx.xx.HomeFavoriteFragment.<init>(SearchFragment.java:170)
    at xx.xx.SearchFragment.newInstance(SearchFragment.java:43)
    at xx.xx.HomeFragment.<init>(HomeFragment.java:344)
    at xx.xx.HomeFragment.newInstance(HomeFragment.java:72) 
    at com.google.android.gms.common.internal.zzk.zzk(Unknown Source) 

2 个答案:

答案 0 :(得分:1)

尝试在SearchFragment.java中导入支持片段,如下所示:

import android.support.v4.app.Fragment;

默认情况下,编译器可能使用android.app.Fragment

答案 1 :(得分:1)

此错误的解决方案: 添加编译'com.android.support:multidex:1.0.0'

Android studio java.lang.NoClassDefFoundError