java—在实现带有片段的tablayout时遇到noclassdeffounderror,并且在片段中希望使用回收器视图

7qhs6swi  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(277)

希望有一个tablayout活动从家庭活动的选项菜单和我的tablayout调用,这是有一个片段,为不同的优惠,这是要提供的形式卡布局的帮助下,回收视图给用户,但没有得到预期的结果,因为得到一个错误,如this:-

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.cako, PID: 6202
    java.lang.NoClassDefFoundError: Failed resolution of: Lcom/example/cako/PgrAdapter;
        at com.example.cako.OfferActivity.onCreate(OfferActivity.java:38)
        at android.app.Activity.performCreate(Activity.java:8000)
        at android.app.Activity.performCreate(Activity.java:7984)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.cako.PgrAdapter" on path: DexPathList[[zip file "/data/app/~~yjOBRW4J2sTIznph4kB9uA==/com.example.cako-638ACQ6zcRU7b9vz0JKplw==/base.apk"],nativeLibraryDirectories=[/data/app/~~yjOBRW4J2sTIznph4kB9uA==/com.example.cako-638ACQ6zcRU7b9vz0JKplw==/lib/x86, /system/lib, /system_ext/lib, /product/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at com.example.cako.OfferActivity.onCreate(OfferActivity.java:38) 
        at android.app.Activity.performCreate(Activity.java:8000) 
        at android.app.Activity.performCreate(Activity.java:7984) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
I/Process: Sending signal. PID: 6202 SIG: 9

实用性是:-

package com.example.cako;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;

import com.google.android.material.tabs.TabLayout;

public class OfferActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_offer);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarForofferActivity);
        setSupportActionBar(toolbar);

        ActionBar ab = getSupportActionBar();
        assert ab != null;
        ab.setDisplayHomeAsUpEnabled(true);

        TabLayout tab=(TabLayout)findViewById(R.id.tabLayoutInOffers);
        tab.setSelectedTabIndicatorColor(getResources().getColor(R.color.colorAccent));

        final ViewPager viewPager=(ViewPager)findViewById(R.id.viewPagerInOfferActivity);

        PgrAdapter adapter=new PgrAdapter(getSupportFragmentManager(),tab.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab));
        tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}

class PgrAdapter extends FragmentStatePagerAdapter {
    int count;

    public PgrAdapter(@NonNull FragmentManager fm, int count) {
        super(fm);
        this.count = count;
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new CakoOffers();
            case 1:
                return new PaymentOffers();
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return count;
    }
}

cako提供1标签的范围:-

package com.example.cako;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class CakoOffers extends Fragment {
    String[] offer;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.layout_for_fragment_cako_offers, container, false);
        RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.recyclerViewForCakoOffers);
        recyclerView.setAdapter(new RecyclerAdapterForCakeOffers(getContext(), offer));
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        return v;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        offer = new String[]{"50%", "10%"};
    }
}

class RecyclerAdapterForCakeOffers extends RecyclerView.Adapter<RecyclerAdapterForCakeOffers.Holder> {
    Context co;
    String[] discount;

    public RecyclerAdapterForCakeOffers(Context context, String[] discount) {
        co = context;
        this.discount = discount;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(co).inflate(R.layout.layout_for_fragment_cako_offers, parent, false);
        return new Holder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        holder.discountText.setText(discount[position]);
//        holder.button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Toast.makeText(co, "yes", Toast.LENGTH_SHORT).show();
//            }
//        });
    }

    @Override
    public int getItemCount() {
        return discount.length;
    }

    class Holder extends RecyclerView.ViewHolder {
        Button button;
        TextView discountText;

        public Holder(@NonNull View itemView) {
            super(itemView);
            discountText = (TextView) itemView.findViewById(R.id.textViewForPercentageDiscountByCako);
            button = (Button) itemView.findViewById(R.id.borderlessButtonInCakoOffers);
        }
    }
}

卡科碎片布局图offer:-

package com.example.cako;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class CakoOffers extends Fragment {
    String[] offer;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.layout_for_fragment_cako_offers, container, false);
        RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.recyclerViewForCakoOffers);
        recyclerView.setAdapter(new RecyclerAdapterForCakeOffers(getContext(), offer));
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        return v;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        offer = new String[]{"50%", "10%"};
    }
}

class RecyclerAdapterForCakeOffers extends RecyclerView.Adapter<RecyclerAdapterForCakeOffers.Holder> {
    Context co;
    String[] discount;

    public RecyclerAdapterForCakeOffers(Context context, String[] discount) {
        co = context;
        this.discount = discount;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(co).inflate(R.layout.layout_for_fragment_cako_offers, parent, false);
        return new Holder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        holder.discountText.setText(discount[position]);
//        holder.button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Toast.makeText(co, "yes", Toast.LENGTH_SHORT).show();
//            }
//        });
    }

    @Override
    public int getItemCount() {
        return discount.length;
    }

    class Holder extends RecyclerView.ViewHolder {
        Button button;
        TextView discountText;

        public Holder(@NonNull View itemView) {
            super(itemView);
            discountText = (TextView) itemView.findViewById(R.id.textViewForPercentageDiscountByCako);
            button = (Button) itemView.findViewById(R.id.borderlessButtonInCakoOffers);
        }
    }
}

回收器视图的卡片布局,以显示offers:-

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/constraint"
    android:layout_width="match_parent"
    android:background="#046180"
    android:padding="1dp"
    android:layout_height="wrap_content">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@android:color/white"
        app:cardElevation="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout

            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/textView4"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:adjustViewBounds="true"
                android:background="@drawable/left_curved_rect_for_coupons"
                android:gravity="center_vertical"
                android:scaleType="centerCrop"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/ic_baseline_help_outline_24" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_marginTop="16dp"
                android:background="@drawable/right_curved_rect_for_coupons"
                android:gravity="center_vertical"
                android:padding="10dp"
                android:text="Cako Special"
                android:textStyle="bold"
                app:layout_constraintStart_toEndOf="@+id/textView4"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/textViewForPercentageDiscountByCako"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:text="50% Off"
                android:textColor="#364702"
                android:textSize="16sp"
                android:textStyle="bold"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView4" />

            <TextView
                android:id="@+id/textViewForDynamicProductDetailInLoved"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="2dp"
                android:text="Get Flat 50% off on your first ever purchase of cake on our App. "
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textViewForPercentageDiscountByCako" />

            <Button
                android:id="@+id/borderlessButtonInCakoOffers"
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentEnd="true"
                android:layout_marginStart="4dp"
                android:text="MORE DETAILS"
                android:textColor="#046180"
                android:textSize="10sp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textViewForDynamicProductDetailInLoved" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

提供的活动。xml:-

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constraint"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".OfferActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="@+id/constraint">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbarForofferActivity"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize" />

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabLayoutInOffers"
            android:layout_width="409dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/appBar">

            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:icon="@drawable/ic_discountcoupon"
                android:text="Cako Offers" />

            <com.google.android.material.tabs.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:icon="@drawable/ic_discountpayment"
                android:text="Coupons" />
        </com.google.android.material.tabs.TabLayout>

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/viewPagerInOfferActivity"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </com.google.android.material.appbar.AppBarLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

建造。gradle:-

apply plugin: 'com.android.application'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.0"

    defaultConfig {
        applicationId "com.example.cako"
        minSdkVersion 19
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation 'de.hdodenhof:circleimageview:3.1.0'
    android.defaultConfig.vectorDrawables.useSupportLibrary = true
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'com.google.android.material:material:1.2.1'
    testImplementation 'junit:junit:4.12'
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题