当我开始新活动时,我的应用程序给了我Java lang noclassdeffounderror。它可以与更高版本的android api一起正常工作,但是当我使用android api 19或android kitkat 4.4时,它无法正常工作。我检查了很多答案,但都没有用
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.bls.msparescustomerapp"
minSdkVersion 19
targetSdkVersion 28
versionCode 8
versionName "2.9"
multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
renderscriptTargetApi 19
renderscriptSupportModeEnabled true
}
buildTypes {
release {
multiDexKeepFile file('multidex-config.txt')
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
implementation 'com.romandanylyk:pageindicatorview:0.0.7'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:design:28.0.0'
implementation 'com.google.guava:guava:24.1-jre'
implementation 'com.android.volley:volley:1.1.1'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'org.greenrobot:eventbus:3.1.1'
implementation 'com.android.support:multidex:1.0.3'
implementation "com.daimajia.swipelayout:library:1.2.0@aar"
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
implementation 'com.airbnb.android:lottie:2.7.0'
implementation 'com.google.guava:guava:27.1-jre'
implementation 'com.cepheuen.elegant-number-button:lib:1.0.2'
implementation 'com.kofigyan.stateprogressbar:stateprogressbar:1.0.0'
implementation 'com.irozon.sneaker:sneaker:2.0.0'
implementation 'com.southernbox:ParallaxRecyclerView:1.0.1'
implementation 'com.iarcuschin:simpleratingbar:0.1.5'
implementation 'com.getbase:floatingactionbutton:1.10.1'
implementation 'com.jackandphantom.android:blurimage:1.2.0'
implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.google.firebase:firebase-database:16.1.0'
implementation 'com.google.firebase:firebase-auth:16.2.1'
implementation 'net.alhazmy13.MediaPicker:libary:2.4.4'
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.crashlytics.sdk.android:crashlytics:2.9.9'
//Paging items
implementation "android.arch.lifecycle:extensions:1.1.0"
implementation "android.arch.lifecycle:viewmodel:1.1.0"
这是清单。我的所有活动在很多问题中都得到了明确的声明。但是这个错误对我来说并没有解决。
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_app_logo"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_app_logo_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning"
tools:replace="android:icon">
<activity android:name=".activities.AllProductsActivity"></activity>
<activity android:name=".activities.BrandItemsActivity" />
<activity android:name=".activities.HomeBottomNavActivity" />
<activity android:name=".activities.CatalogItems" />
<activity android:name=".activities.Catalog" />
<activity android:name=".activities.BrandCompoItems" />
<activity android:name=".activities.ItemDetailsActivity" />
<activity android:name=".activities.SearchItems" />
<activity android:name=".activities.CheckOut" />
<activity android:name=".activities.SignUp" />
<activity
android:name=".activities.HomeActivity"
android:label="@string/title_activity_home"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar" />
<activity android:name=".activities.LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.ChatActivity" />
以下是堆栈跟踪。
W/System.err: java.lang.NoClassDefFoundError: com.bls.msparescustomerapp.activities.AllProductsActivity
at com.bls.msparescustomerapp.fragments.HomeFragment.createView(HomeFragment.java:171)
at com.bls.msparescustomerapp.adapters.newHomeAdapters.HomeItemsRecyclerAdapter$HomeProductsLoadMoreViewHolder$1.onClick(HomeItemsRecyclerAdapter.java:143)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
HomeHomeItemsRecyclerAdapter类
public class HomeItemsRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private List<AllProductsModel> allProductsModelList;
private HomeAdapterListner homeAdapterListner;
public final int SLIDER_VEIW = 1;
public final int FLASH_SALE_VIEW = 2;
public final int CATEGORY_VIEW = 3;
public final int PRODUCTS_VIEW = 4;
public final int LOAD_MORE_VIEW = 5;
private final String TAG = "TEST_DEBUG";
private HomeItemClickListner homeItemClickListner;
private InquirePriceListener inquirePriceListener;
public HomeItemsRecyclerAdapter(Context context, List<AllProductsModel> allProductsModelList, HomeAdapterListner homeAdapterListner, HomeItemClickListner homeItemClickListner, InquirePriceListener inquirePriceListener) {
this.context = context;
this.allProductsModelList = allProductsModelList;
this.homeAdapterListner = homeAdapterListner;
this.homeItemClickListner = homeItemClickListner;
this.inquirePriceListener = inquirePriceListener;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
if (i == SLIDER_VEIW) {
CustomHomeSliderViewBinding customHomeSliderViewBinding = DataBindingUtil.inflate(LayoutInflater.from(context),
R.layout.custom_home_slider_view, viewGroup, false);
return new HomeSliderViewHolder(customHomeSliderViewBinding);
} else if (i == FLASH_SALE_VIEW) {
CustomHomeDiscountViewBinding customHomeDiscountViewBinding = DataBindingUtil.inflate(LayoutInflater.from(context),
R.layout.custom_home_discount_view, viewGroup, false);
return new HomeDiscountItemsViewHolder(customHomeDiscountViewBinding);
} else if (i == CATEGORY_VIEW) {
CustomHomeCategoryViewBinding customHomeCategoryViewBinding = DataBindingUtil.inflate(LayoutInflater.from(context),
R.layout.custom_home_category_view, viewGroup, false);
return new HomeCategoriesViewHolder(customHomeCategoryViewBinding);
} else if (i == LOAD_MORE_VIEW) {
HomeFragmentLoadMoreViewBinding homeFragmentLoadMoreViewBinding = DataBindingUtil.inflate(LayoutInflater.from(context),
R.layout.home_fragment_load_more_view, viewGroup, false);
return new HomeProductsLoadMoreViewHolder(homeFragmentLoadMoreViewBinding.getRoot());
} else {
CustomHomeViewBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context),
R.layout.custom_home_view, viewGroup, false);
return new HomeProductsListViewHolder(binding);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
if (viewHolder.getItemViewType() == SLIDER_VEIW) {
HomeSliderViewHolder homeSliderViewHolder = (HomeSliderViewHolder) viewHolder;
homeSliderViewHolder.onBind(i);
} else if (viewHolder.getItemViewType() == FLASH_SALE_VIEW) {
HomeDiscountItemsViewHolder homeDiscountItemsViewHolder = (HomeDiscountItemsViewHolder) viewHolder;
homeDiscountItemsViewHolder.onBind(i);
} else if (viewHolder.getItemViewType() == CATEGORY_VIEW) {
HomeCategoriesViewHolder homeCategoriesViewHolder = (HomeCategoriesViewHolder) viewHolder;
homeCategoriesViewHolder.onBind(i);
} else if (viewHolder.getItemViewType() == LOAD_MORE_VIEW) {
HomeProductsLoadMoreViewHolder homeProductsLoadMoreViewHolder = (HomeProductsLoadMoreViewHolder) viewHolder;
homeProductsLoadMoreViewHolder.onBind(i);
} else if (viewHolder.getItemViewType() == PRODUCTS_VIEW) {
HomeProductsListViewHolder homeProductsListViewHolder0 = (HomeProductsListViewHolder) viewHolder;
homeProductsListViewHolder0.onBind(i);
}
}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return SLIDER_VEIW;
} else if (position == 1) {
return FLASH_SALE_VIEW;
} else if (position == 2) {
return CATEGORY_VIEW;
} else if (position == allProductsModelList.size() + 3) {
return LOAD_MORE_VIEW;
} else {
return PRODUCTS_VIEW;
}
}
@Override
public int getItemCount() {
if (allProductsModelList == null)
return 3;
else
return allProductsModelList.size() + 4;
}
public class HomeProductsLoadMoreViewHolder extends RecyclerView.ViewHolder {
public HomeProductsLoadMoreViewHolder(@NonNull View itemView) {
super(itemView);
}
private void onBind(int position) {
Button loadMore = itemView.findViewById(R.id.btn_load_more);
loadMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
homeAdapterListner.createView(LOAD_MORE_VIEW, itemView, position-4);
}
});
}
}
public class HomeProductsListViewHolder extends RecyclerView.ViewHolder {
CustomHomeViewBinding binding;
public HomeProductsListViewHolder(CustomHomeViewBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
public void onBind(int position) {
if (position == allProductsModelList.size() + 3) {
return;
}
AllProductsModel allProductsModel = allProductsModelList.get(position - 3);
binding.tvProductName.setText(allProductsModel.getProductName());
if (allProductsModel.getPrice().equals("0.00")) {
binding.tvProductPrice.setText("Inquire price");
binding.tvProductPrice.setOnClickListener(v->{
inquirePriceListener.inquirePrice();
});
} else {
binding.tvProductPrice.setText("KSh: " + allProductsModel.getPrice());
}
Glide.with(context)
.load(context.getResources().getString(R.string.imageUrl) + allProductsModel.getImageName())
.placeholder(context.getResources().getDrawable(R.drawable.no_image))
.into(binding.ivProductImage);
binding.cardView.setOnClickListener(v -> {
homeItemClickListner.onHomeItemClick(allProductsModelList.get(position-3));
});
}
}
public class HomeSliderViewHolder extends RecyclerView.ViewHolder {
CustomHomeSliderViewBinding customHomeSliderViewBinding;
public HomeSliderViewHolder(CustomHomeSliderViewBinding customHomeSliderViewBinding) {
super(customHomeSliderViewBinding.getRoot());
this.customHomeSliderViewBinding = customHomeSliderViewBinding;
}
public void onBind(int position) {
homeAdapterListner.createView(SLIDER_VEIW, customHomeSliderViewBinding.getRoot(), position);
}
}
private class HomeDiscountItemsViewHolder extends RecyclerView.ViewHolder {
CustomHomeDiscountViewBinding customHomeDiscountViewBinding;
public HomeDiscountItemsViewHolder(CustomHomeDiscountViewBinding customHomeDiscountViewBinding) {
super(customHomeDiscountViewBinding.getRoot());
this.customHomeDiscountViewBinding = customHomeDiscountViewBinding;
}
public void onBind(int position) {
homeAdapterListner.createView(FLASH_SALE_VIEW, customHomeDiscountViewBinding.getRoot(), position);
}
}
private class HomeCategoriesViewHolder extends RecyclerView.ViewHolder {
CustomHomeCategoryViewBinding customHomeCategoryViewBinding;
public HomeCategoriesViewHolder(CustomHomeCategoryViewBinding customHomeCategoryViewBinding) {
super(customHomeCategoryViewBinding.getRoot());
this.customHomeCategoryViewBinding = customHomeCategoryViewBinding;
}
public void onBind(int position) {
homeAdapterListner.createView(CATEGORY_VIEW, customHomeCategoryViewBinding.getRoot(), position);
}
}
}
HomeFragment
public class HomeFragment extends Fragment implements HomeAdapterListner, HomeItemClickListner, InquirePriceListener {
private FragmentHomeBinding fragmentHomeBinding;
private FetchData fetchData;
private LinearLayoutManager linearLayoutManager;
private List<AllProductsModel> allProductsModelList;
int pageCoutner = 0;
Handler handler;
Runnable runnable;
private List<SaleItemsResultModel> saleItemsResultModels = new ArrayList<>();
private SaleItemsRecyclerViewAdapter saleItemsRecyclerViewAdapter;
private List<BrandComponentsRecordModel> brandComponentsRecordModelList = new ArrayList<>();
private BrandComponentsRecyclerViewAdapter brandComponentsRecyclerViewAdapter;
public HomeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fragmentHomeBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.fragment_home, container,
false);
View view = fragmentHomeBinding.getRoot();
initObj(view);
return view;
}
@SuppressLint("NewApi")
private void initObj(View view) {
handler = new Handler();
linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayout.VERTICAL);
fragmentHomeBinding.homeRecyclerView.setLayoutManager(linearLayoutManager);
fetchData = new FetchData(getContext());
fetchData.getAllItems();
fetchData.getSaleItems();
fetchData.getBrandComponent();
fragmentHomeBinding.ivLogout.setOnClickListener(v -> {
SharedPrefHelper.clearAll(getContext());
getActivity().startActivity(new Intent(getActivity(), LoginActivity.class));
getActivity().finish();
});
}
@Subscribe
public void onEvent(ApiResult apiResult) {
fragmentHomeBinding.noDataLayout.setVisibility(View.GONE);
fragmentHomeBinding.homeRecyclerView.setVisibility(View.VISIBLE);
fragmentHomeBinding.homeProgressBar.setVisibility(View.GONE);
if (apiResult.getObject() == null) {
fragmentHomeBinding.noDataLayout.setVisibility(View.VISIBLE);
fragmentHomeBinding.homeRecyclerView.setVisibility(View.GONE);
return;
}
if (apiResult.getMethodName().equals("allProductsResponse")) {
AllProductsResponseModel responseModel = (AllProductsResponseModel) apiResult.getObject();
allProductsModelList = responseModel.getAllProductsModelList();
HomeItemsRecyclerAdapter homeItemsRecyclerAdapter = new HomeItemsRecyclerAdapter(getContext(), allProductsModelList, this, this, this);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int i) {
switch (homeItemsRecyclerAdapter.getItemViewType(i)) {
case 1:
return 2;
case 2:
return 2;
case 3:
return 2;
case 4:
return 1;
default:
return 2;
}
}
});
fragmentHomeBinding.homeRecyclerView.setLayoutManager(gridLayoutManager);
fragmentHomeBinding.homeRecyclerView.setAdapter(homeItemsRecyclerAdapter);
}
if (apiResult.getMethodName().equals("salesItems")) {
SaleItemsApiResponseModel saleItemsApiResponseModel = (SaleItemsApiResponseModel) apiResult.getObject();
saleItemsResultModels = saleItemsApiResponseModel.getSaleItemsResultModels();
}
if (apiResult.getMethodName().equals("brandComponents")) {
BrandComponentsApiResponseModel brandComponentsApiResponseModel = (BrandComponentsApiResponseModel) apiResult.getObject();
brandComponentsRecordModelList = brandComponentsApiResponseModel.getBrandComponentsRecordModels();
brandComponentsRecyclerViewAdapter.notifyDataSetChanged();
}
}
@Override
public void onResume() {
super.onResume();
EventBus.getDefault().register(this);
}
@Override
public void onPause() {
handler.removeCallbacks(runnable);
EventBus.getDefault().unregister(this);
super.onPause();
}
@Override
public void createView(int viewType, View view, int position) {
if (viewType == 1) {
createViewPager(view);
}
if (viewType == 2) {
createSaleItemsView(view);
}
if (viewType == 3) {
createCategoryView(view);
}
if (viewType == 5) {
try {
String lastItemId = allProductsModelList.get(position).getId();
startActivity(new Intent(getActivity(), AllProductsActivity.class)
.putExtra(getResources().getString(R.string.last_item_id), lastItemId));
} catch (Resources.NotFoundException e) {
e.printStackTrace();
} catch (NoClassDefFoundError e) {
e.printStackTrace();
}
}
}
private void createCategoryView(View view) {
RecyclerView categoryRecyclerView = view.findViewById(R.id.rv_category_items);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
categoryRecyclerView.setLayoutManager(linearLayoutManager);
if (brandComponentsRecordModelList.size() != 0) {
brandComponentsRecyclerViewAdapter = new BrandComponentsRecyclerViewAdapter(getContext(), brandComponentsRecordModelList);
categoryRecyclerView.setAdapter(brandComponentsRecyclerViewAdapter);
} else {
categoryRecyclerView.setVisibility(View.GONE);
TextView catTitle = view.findViewById(R.id.tv_category_title);
catTitle.setVisibility(View.GONE);
}
}
private void createSaleItemsView(View view) {
RecyclerView saleItemsRecyclerView = view.findViewById(R.id.rv_discount_items);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
saleItemsRecyclerView.setLayoutManager(linearLayoutManager);
if (saleItemsResultModels.size() != 0) {
saleItemsRecyclerViewAdapter = new SaleItemsRecyclerViewAdapter(getContext(), saleItemsResultModels);
saleItemsRecyclerView.setAdapter(saleItemsRecyclerViewAdapter);
} else {
saleItemsRecyclerView.setVisibility(View.GONE);
TextView saleTitle = view.findViewById(R.id.tv_discount_title);
saleTitle.setVisibility(View.GONE);
}
}
private void createViewPager(View view) {
ViewPager viewPager = view.findViewById(R.id.viewPager);
PageIndicatorView pageIndicatorView = view.findViewById(R.id.pageIndicatorView);
ArrayList<String> imagesList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
imagesList.add(allProductsModelList.get(i).getImageName());
}
pageIndicatorView.setCount(imagesList.size());
HomePagerAdapter homePagerAdapter = new HomePagerAdapter(imagesList, getContext(), "details");
viewPager.setAdapter(homePagerAdapter);
runnable = new Runnable() {
@Override
public void run() {
if (homePagerAdapter.getCount() == pageCoutner) {
pageCoutner = 0;
} else {
pageCoutner++;
}
viewPager.setCurrentItem(pageCoutner, true);
handler.postDelayed(runnable, 3000);
}
};
handler.postDelayed(runnable, 3000);
}
@Override
public void onHomeItemClick(AllProductsModel allProductsModel) {
String productData = new Gson().toJson(allProductsModel);
getActivity().startActivity(new Intent(getActivity(), ItemDetailsActivity.class).putExtra(getString(R.string.product_details), productData)
.putExtra(getResources().getString(R.string.data_from), getContext().getResources().getString(R.string.from_home_fragment)));
}
@Override
public void inquirePrice() {
WhatsAppDialog whatsAppDialog = new WhatsAppDialog();
whatsAppDialog.show(getChildFragmentManager(), "");
}
}