简体   繁体   English

回收站视图的适配器导致崩溃和强制停止

[英]adapter of recycler view cause crash and force stop

when I use recycler view and adapter for that, the adapter doesn't let the app to install and it cause crash.当我为此使用回收器视图和适配器时,适配器不会让应用程序安装并导致崩溃。 I commented adapter in my main activity and started successfully but with adapter it doesn't work.我在我的主要活动中评论了适配器并成功启动,但使用适配器它不起作用。 please help me.请帮我。 thank you谢谢你

my adapter class is here:我的适配器 class 在这里:

public class AdapterFree extends RecyclerView.Adapter<AdapterFree.ViewHolder> {

Context context;
List<ModelFree> modelFrees;

public AdapterFree(Context context, List<ModelFree> modelFrees) {
    this.context = context;
    this.modelFrees = modelFrees;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_free, parent, false);
    return new AdapterFree.ViewHolder(view);
    //return new ViewHolder(view);
    //return new AdapterFree(context, modelFrees).new ViewHolder(view);
    /*ViewHolder vh = new ViewHolder(view);
    return vh;*/
    //here I tried a few codes but didn't help 
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    ModelFree free = modelFrees.get(position);
    DecimalFormat decimalFormat = new DecimalFormat("###,###");
    String price = decimalFormat.format(Integer.valueOf(free.getPrice()));
    holder.textPriceFree.setText(price + " " + "$");
    holder.textVisitFree.setText(free.getVisit());
    holder.textTitle.setText(free.getTitle());
    holder.textFreePrice.setText(free.getFree());
    //holder.imageFree.setImageResource(free.getImage());
    holder.imageFree.setImageResource(Integer.parseInt(free.getImage()));
}

@Override
public int getItemCount() {
    return modelFrees.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    LinearLayout linearLayout;
    CardView cardViewFree;
    ImageView imageFree;
    TextView textTitle, textVisitFree, textPriceFree, textFreePrice;
    Typeface typeface = Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/vaziri.ttf");

    public ViewHolder(@NonNull View itemView) {
        super(itemView);

        linearLayout = itemView.findViewById(R.id.linearLayout);
        cardViewFree = itemView.findViewById(R.id.cardViewFree);
        imageFree = itemView.findViewById(R.id.imageFree);
        textTitle = itemView.findViewById(R.id.textTitle);
        textTitle.setTypeface(typeface);
        textVisitFree = itemView.findViewById(R.id.textVisitFree);
        textVisitFree.setTypeface(typeface);
        textPriceFree = itemView.findViewById(R.id.textPriceFree);
        textPriceFree.setTypeface(typeface);
        textFreePrice = itemView.findViewById(R.id.textFreePrice);
        textFreePrice.setTypeface(typeface);
    }
}

} }

my main activity:我的主要活动:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

ImageView image1;
CoordinatorLayout coordinator;
DrawerLayout drawerLayout;
NavigationView navigationView;
Toolbar mainToolbar;
RecyclerView recyclerFree, recyclerOnly, recyclerVisit, recyclerSales;

AdapterFree adapterFree;
List<ModelFree> modelFreeList = new ArrayList<>();

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

    coordinator = findViewById(R.id.coordinator);
    drawerLayout = findViewById(R.id.drawerLayout);
    navigationView = findViewById(R.id.navigationView);
    mainToolbar = findViewById(R.id.main_toolbar);
    image1 = findViewById(R.id.image1);
    recyclerFree = findViewById(R.id.recyclerFree);
    recyclerOnly = findViewById(R.id.recyclerOnly);
    recyclerVisit = findViewById(R.id.recyclerVisit);
    recyclerSales = findViewById(R.id.recyclerSales);

    setSupportActionBar(mainToolbar);

    ActionBarDrawerToggle toggle;
    toggle = new ActionBarDrawerToggle(this, drawerLayout, mainToolbar, R.string.open, R.string.close);
    drawerLayout.addDrawerListener(toggle);
    toggle.syncState();

    navigationView.setNavigationItemSelectedListener(this);

    Picasso.get().load(R.drawable.lexuse).into(image1);

    setDataFree();
}

private void setDataFree() {
    adapterFree = new AdapterFree(getApplicationContext(), modelFreeList);
    recyclerFree.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
    recyclerFree.setAdapter(adapterFree);

    modelFreeList.add(new ModelFree(1, String.valueOf(R.drawable.lexus2), "lexus2", "10", "10000$", "5$"));
    modelFreeList.add(new ModelFree(2, String.valueOf(R.drawable.lexus3), "lexus3", "20", "20000$", "6$"));
    modelFreeList.add(new ModelFree(3, String.valueOf(R.drawable.lexus3), "lexus3", "30", "30000$", "7$"));
    modelFreeList.add(new ModelFree(4, String.valueOf(R.drawable.lexus4), "lexus4", "40", "40000$", "8$"));
    modelFreeList.add(new ModelFree(5, String.valueOf(R.drawable.lexus5), "lexus5", "50", "50000$", "9$"));
    modelFreeList.add(new ModelFree(6, String.valueOf(R.drawable.lexusd), "lexus6", "60", "60000$", "5$"));

    adapterFree.notifyDataSetChanged();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.option_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    int optionId = item.getItemId();
    switch (optionId) {
        case R.id.page2:
            startActivity(new Intent(this, MainActivity2.class));
            break;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    drawerLayout.closeDrawer(GravityCompat.START);
    return true;
}

@Override
public void onBackPressed() {
    if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
        drawerLayout.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

} }

my model class:我的 model class:

public class ModelFree {

int id;
String image;
String title;
String visit;
String price;
String free;

public ModelFree(int id, String image, String title, String visit, String price, String free) {
    this.id = id;
    this.image = image;
    this.title = title;
    this.visit = visit;
    this.price = price;
    this.free = free;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getVisit() {
    return visit;
}

public void setVisit(String visit) {
    this.visit = visit;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

public String getFree() {
    return free;
}

public void setFree(String free) {
    this.free = free;
}

} }

xml layout that I write recycler view: xml 布局我写的回收站视图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.appcompat.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/purple_200"
    android:id="@+id/main_toolbar"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<ImageView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:id="@+id/image1"
    android:scaleType="fitXY"/>

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#e7e7e7">

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="60dp"
            app:cardCornerRadius="10dp"
            app:cardElevation="1dp"
            app:cardBackgroundColor="@color/teal_200"
            android:layout_marginLeft="60dp"
            android:layout_marginRight="60dp"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="10dp"
            android:id="@+id/cardCategory">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="horizontal"
                android:layoutDirection="rtl">

                <ImageView
                    android:layout_width="34dp"
                    android:layout_height="34dp"
                    android:src="@drawable/ic_baseline_format_list_bulleted_24"
                    app:tint="@color/cardview_light_background" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp"
                    android:textSize="18sp"
                    android:textColor="@color/white"
                    android:text="Category"/>

            </LinearLayout>

        </androidx.cardview.widget.CardView>

        <androidx.appcompat.widget.AppCompatTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="16sp"
            android:gravity="left"
            android:text="Special Discounts"
            android:textColor="@color/black"
            android:layout_marginLeft="15dp"/>

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/recyclerFree"
            android:layoutDirection="ltr"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="3dp"/>

        <androidx.appcompat.widget.AppCompatTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="16sp"
            android:gravity="left"
            android:text="Just Here"
            android:textColor="@color/black"
            android:layout_marginLeft="15dp"/>

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/recyclerOnly"
            android:layoutDirection="ltr"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="3dp"/>

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="180dp"
            android:layout_marginTop="10dp"
            android:scaleType="fitXY"
            android:src="@drawable/lexusa"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="140dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="15dp"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:layout_margin="1dp">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="fitXY"
                    android:src="@drawable/lexusb"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:layout_margin="1dp">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="fitXY"
                    android:src="@drawable/lexusc"/>

            </LinearLayout>

        </LinearLayout>

        <androidx.appcompat.widget.AppCompatTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="16sp"
            android:textColor="@color/black"
            android:gravity="left"
            android:text="Most visited"
            android:layout_marginLeft="15dp"/>

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layoutDirection="ltr"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="3dp"
            android:id="@+id/recyclerVisit"/>

        <androidx.appcompat.widget.AppCompatTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textSize="16sp"
            android:textColor="@color/black"
            android:gravity="left"
            android:text="Most sales"
            android:layout_marginLeft="15dp"/>

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layoutDirection="ltr"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="3dp"
            android:id="@+id/recyclerSales"/>

    </LinearLayout>

</androidx.core.widget.NestedScrollView>

my xml layout for items:我的 xml 项目布局:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="170dp"
android:layout_height="260dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clickable="true"
android:id="@+id/cardViewFree"
android:layout_margin="4dp"
android:orientation="vertical"
app:cardCornerRadius="2dp"
app:cardElevation="1dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/linearLayout"
    android:visibility="visible"
    android:orientation="vertical">

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

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:scaleType="centerInside"
            android:id="@+id/imageFree"
            android:src="@drawable/lexus2"/>

    </RelativeLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="start"
        android:id="@+id/textTitle"
        android:layout_margin="3dp"
        android:gravity="left"
        android:text="lexus2"
        android:textColor="@color/black"
        android:textSize="12dp"
        android:singleLine="true" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layoutDirection="ltr"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.4"
            android:gravity="center_vertical"
            android:layoutDirection="ltr"
            android:orientation="horizontal"
            android:padding="5dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="12dp"
                android:id="@+id/textVisitFree"
                android:text="10"
                android:textColor="@color/black"/>

            <ImageView
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_marginRight="5dp"
                android:src="@drawable/ic_baseline_visibility_24"
                app:tint="@color/black" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.6"
            android:gravity="center_vertical|right"
            android:padding="5dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/textPriceFree"
                android:gravity="center_vertical|right"
                android:text="123000$"
                android:textColor="@color/black"
                android:textSize="12sp"/>

        </LinearLayout>

    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textFreePrice"
        android:padding="1dp"
        android:layout_marginRight="5dp"
        android:gravity="right|center_vertical"/>

</LinearLayout>

</com.google.android.material.card.MaterialCardView> </com.google.android.material.card.MaterialCardView>

I found the problem and solved it.我发现了问题并解决了它。 the main problem was about MaterialCardView, so I changed my codes to this:主要问题是关于 MaterialCardView,所以我将代码更改为:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="170dp"
android:layout_height="260dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
app:cardCornerRadius="2dp"
app:cardElevation="1dp"
android:clickable="true"
android:id="@+id/cardViewFree"
android:layout_margin="4dp"
android:orientation="vertical"
android:theme="@style/Theme.MaterialComponents.DayNight"
style="@style/Widget.MaterialComponents.CardView">

<LinearLayout
      ............
</LinearLayout>

</com.google.android.material.card.MaterialCardView>

after that I changed the part of the codes of Model class to this:之后,我将 Model class 的部分代码更改为:

public class ModelFree {

int id;
int image;
String title;
String visit;
int price;
String free;

so I write some of variables to "int"所以我将一些变量写入“int”

of course I changed this method in MainActivity:当然我在 MainActivity 中改变了这个方法:

 private void setDataFree() {
    adapterFree = new AdapterFree(getApplicationContext(), modelFreeList);
    recyclerFree.setLayoutManager(new 
    LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, 
    false));
    recyclerFree.setAdapter(adapterFree);

    modelFreeList.add(new ModelFree(1, R.drawable.lexus2, "lexus2", "10", 
    100000, "5%"));
    modelFreeList.add(new ModelFree(2, R.drawable.lexus3, "lexus3", "20", 20000, 
    "6$"));
    modelFreeList.add(new ModelFree(3, R.drawable.lexus3, "lexus3", "30", 30000, 
    "7$"));
    modelFreeList.add(new ModelFree(4, R.drawable.lexus4, "lexus4", "40", 40000, 
    "8$"));
    modelFreeList.add(new ModelFree(5, R.drawable.lexus5, "lexus5", "50", 50000, 
    "9$"));
    modelFreeList.add(new ModelFree(6, R.drawable.lexusd, "lexus6", "60", 60000, 
    "5$"));


    adapterFree.notifyDataSetChanged();
}

finally I commented some lines in Adapter class because they caused error and I couldn't solve that:最后我在 Adapter class 中评论了一些行,因为它们导致了错误,我无法解决:

public class MyViewHolder extends RecyclerView.ViewHolder {

    LinearLayout linearLayout;
    CardView cardViewFree;
    ImageView imageFree;
    TextView textTitle, textVisitFree, textPriceFree, textFreePrice;
    //Typeface typeface = 
       Typeface.createFromAsset(itemView.getContext().getAssets(), 
        String.valueOf(R.font.times));

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);

        linearLayout = itemView.findViewById(R.id.linearLayout);
        cardViewFree = itemView.findViewById(R.id.cardViewFree);
        imageFree = itemView.findViewById(R.id.imageFree);
        textTitle = itemView.findViewById(R.id.textTitle);
        //textTitle.setTypeface(typeface);
        textVisitFree = itemView.findViewById(R.id.textVisitFree);
        //textVisitFree.setTypeface(typeface);
        textPriceFree = itemView.findViewById(R.id.textPriceFree);
        //textPriceFree.setTypeface(typeface);
        textFreePrice = itemView.findViewById(R.id.textFreePrice);
        //textFreePrice.setTypeface(typeface);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM