android.support.v7.widget.RecyclerView не может быть преобразован в android.widget.ListView

#java #android #android-recyclerview

#java #Android #android-recyclerview

Вопрос:

Я получаю ошибку, показанную выше, я проверил, но в моем коде нет ссылки на ListView. Я использовал ListView раньше, но изменил его на RecycleView, и я получаю эту ошибку.

Это мой класс restaurantAdapter.

  public class restaurantAdapter extends 
 RecyclerView.Adapter<restaurantAdapter.viewHolder> {

private List<restaurant> restau;


public restaurantAdapter(List<restaurant> res) {

    this.restau = res;

 }

@NonNull
@Override
public restaurantAdapter.viewHolder onCreateViewHolder(@NonNull ViewGroup 
viewGroup, int i) {
    View view = 
LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.restaurant_view 
,viewGroup, false);
    return new viewHolder(view);
 }

 @Override
 public void onBindViewHolder(@NonNull restaurantAdapter.viewHolder 
 viewAdapter, int i) {
    restaurant pos = restau.get(i);

    viewAdapter.imageView.setImageResource(pos.getmImage());
    viewAdapter.textView.setText(pos.getmName());
    viewAdapter.textView1.setText(pos.getmCusine());
    viewAdapter.textView2.setText(pos.getmOffers());

 }

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

 public class viewHolder extends RecyclerView.ViewHolder {
    ImageView imageView;
    TextView textView;
    TextView textView1;
    TextView textView2;

    public viewHolder(@NonNull View itemView) {
        super(itemView);
        imageView = itemView.findViewById(R.id.restaurant_imageView);
        textView = itemView.findViewById(R.id.name_textView);
        textView1 = itemView.findViewById(R.id.cuisine_textView);
        textView2 = itemView.findViewById(R.id.offer_textView);
     }
   }
  

Это мой MainActivity.xml и у меня есть LinearLayout в моем restaurant_view.xml файл.

 <android.support.v4.widget.NestedScrollView 
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:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.v7.widget.RecyclerView
        android:id="@ id/offersRecycleView"
        android:layout_width="match_parent"
        android:layout_height="210dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </android.support.v7.widget.RecyclerView>

    <android.support.v7.widget.RecyclerView
        android:id="@ id/pickFavoriteRecycleView"
        android:layout_width="match_parent"
        android:layout_height="73dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/offersRecycleView" />


    <android.support.v7.widget.RecyclerView
        android:id="@ id/restaurantItem"
        android:layout_width="match_parent"
        android:layout_height="738dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:orientation="vertical"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/pickFavoriteRecycleView" 
   />


   </android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
  

Вот полный код MainActivity

  public class MainActivity extends AppCompatActivity {

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

    //Setting Header Recycle View
    ArrayList<offers> word = new ArrayList<>();

    word.add(new offers(R.drawable.offer_one, "Subscribe"));
    word.add(new offers(R.drawable.swiggy_coupons, "offers"));
    word.add(new offers(R.drawable.swiggy_offers7, "Subscribe"));
    word.add(new offers(R.drawable.swiggy_offer_3, "offers"));
    word.add(new offers(R.drawable.swiggy_paytm_offers_3, "offers"));
    word.add(new offers(R.drawable.swiggy_offer2, "offers"));

    offersAdapter adapter = new offersAdapter(word);

    RecyclerView recyclerView = findViewById(R.id.offersRecycleView);

    LinearLayoutManager layoutManager = new LinearLayoutManager(this, 
    LinearLayoutManager.HORIZONTAL, false);

    recyclerView.setLayoutManager(layoutManager);

    recyclerView.setAdapter(adapter);

    //Setting chooseBest recycle View

    ArrayList<chooseBest> choose = new ArrayList<>();

    choose.add(new chooseBest(R.drawable.ic_bike_black_24dp, "IT is 
     Textnsome text"));
    choose.add(new chooseBest(R.drawable.ic_bike_black_24dp, "IT is 
     Textnsome text"));
    choose.add(new chooseBest(R.drawable.ic_bike_black_24dp, "IT  Text some 
    text"));
    choose.add(new chooseBest(R.drawable.ic_bike_black_24dp, "IT some 
    text"));
    choose.add(new chooseBest(R.drawable.ic_bike_black_24dp, "IT is 
    Textnsome text"));
    choose.add(new chooseBest(R.drawable.ic_bike_black_24dp, "IT is 
    Textnsome text"));
    choose.add(new chooseBest(R.drawable.ic_bike_black_24dp, "IT  Text some 
    text"));
    choose.add(new chooseBest(R.drawable.ic_bike_black_24dp, "IT some 
     text"));

    chooseBestAdapter adapter1 = new chooseBestAdapter(choose);

    RecyclerView recyclerView1 = findViewById(R.id.pickFavoriteRecycleView);

    LinearLayoutManager layoutManager1 = new LinearLayoutManager(this, 
    LinearLayoutManager.HORIZONTAL, false);

    recyclerView1.setLayoutManager(layoutManager1);

    recyclerView1.setAdapter(adapter1);

    //Setting the Restaurant information View

    ArrayList<restaurant> restau = new ArrayList<>();

    restau.add(new restaurant(R.mipmap.ic_launcher_round,"Amar 
     Punjabi","North Indian","5% off on All"));
     restau.add(new restaurant(R.mipmap.ic_launcher_round,"Amar 
   Punjabi","North Indian","5% off on All"));
    restau.add(new restaurant(R.mipmap.ic_launcher_round,"Amar 
      Punjabi","North Indian","5% off on All"));
    restau.add(new restaurant(R.mipmap.ic_launcher_round,"Amar 
     Punjabi","North Indian","5% off on All"));
    restau.add(new restaurant(R.mipmap.ic_launcher_round,"Amar 
    Punjabi","North Indian","5% off on All"));
    restau.add(new restaurant(R.mipmap.ic_launcher_round,"Amar 
    Punjabi","North Indian","5% off on All"));
    restau.add(new restaurant(R.mipmap.ic_launcher_round,"Amar 
    Punjabi","North Indian","5% off on All"));
    restau.add(new restaurant(R.mipmap.ic_launcher_round,"Amar 
    Punjabi","North Indian","5% off on All"));
    restau.add(new restaurant(R.mipmap.ic_launcher_round,"Amar 
    Punjabi","North Indian","5% off on All"));
    restau.add(new restaurant(R.mipmap.ic_launcher_round,"Amar 
    Punjabi","North Indian","5% off on All"));
    restau.add(new restaurant(R.mipmap.ic_launcher_round,"Amar 
    Punjabi","North Indian","5% off on All"));
    restau.add(new restaurant(R.mipmap.ic_launcher_round,"Amar 
    Punjabi","North Indian","5% off on All"));

   restaurantAdapter restaurantAdapter = new restaurantAdapter(restau);

    RecyclerView recyclerView2 = findViewById(R.id.restaurantItem);

    LinearLayoutManager layoutManager2 = new 
LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);

    recyclerView2.setLayoutManager(layoutManager2);

    recyclerView2.setAdapter(restaurantAdapter);
  }
 }
  

Это ошибка.

 04-01 00:35:58.069 12248-12248/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.shoaib.swiggymock, PID: 12248
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shoaib.swiggymock/com.example.shoaib.swiggymock.MainActivity}: java.lang.ClassCastException: android.support.v7.widget.RecyclerView cannot be cast to android.widget.ListView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5443)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
 Caused by: java.lang.ClassCastException: android.support.v7.widget.RecyclerView cannot be cast to android.widget.ListView
    at com.example.shoaib.swiggymock.MainActivity.onCreate(MainActivity.java:80)
    at android.app.Activity.performCreate(Activity.java:6259)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5443) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618 

Комментарии:

1. опубликуйте свой XML-файл .. если вы говорите, что у вас нет listview ссылки. Ошибка указывает на исключение приведения типа, так что это должно быть оно.

2. @Sam.i отредактировал вопрос, пожалуйста, проверьте

3. также опубликуйте часть кода, в которой вы используете findViewbyID для recyclerview, вместе с переменными вашего recylcerview

4. Поделитесь также своим MainActivity полным кодом, пожалуйста

5. @Sam.it находится в MainActivity restaurantItem в третьем режиме рециркуляции

Ответ №1:

Что сработало для меня, так это то, что я очистил проект, затем перестроил его, а затем перезагрузил компьютер, не знаю, почему это сработало, но это сработало.