android.support.v7.widget.RecyclerView无法转换为android.widget.ListView

时间:2019-03-31 18:17:08

标签: 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,我的restaurant_view.xml文件中有一个LinearLayout。

<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 
     Text\nsome text"));
    choose.add(new chooseBest(R.drawable.ic_bike_black_24dp, "IT is 
     Text\nsome 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 
    Text\nsome text"));
    choose.add(new chooseBest(R.drawable.ic_bike_black_24dp, "IT is 
    Text\nsome 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 个答案:

答案 0 :(得分:0)

对我有用的是我清理项目,然后重建它,然后重新启动PC,不知道它为什么起作用,但是它起作用了。